1.
创建一个名为
CustomersWithOrders
的视图,其中包含
Customers表中的所有列,但仅仅是那些已下订单的列。提示:可以在 Orders表上使用 JOIN
来仅仅过滤所需的顾客,然后使用
SELECT
来确保拥有正确的数据。
CREATE VIEW CustomersWithOrders AS
SELECT Customers.cust_id,
Customers.cust_name,
Customers.cust_address,
Customers.cust_city,
Customers.cust_state,
Customers.cust_zip,
Customers.cust_country,
Customers.cust_contact,
Customers.cust_email
FROM Customers
JOIN Orders ON Customers.cust_id = Orders.cust_id;
SELECT * FROM CustomersWithOrders;
2.
下面的
SQL
语句有问题吗?(尝试在不运行的情况下指出。)
CREATE VIEW OrderItemsExpanded AS
SELECT order_num,
prod_id,
quantity,
item_price,
quantity*item_price AS expanded_price
FROM OrderItems
ORDER BY order_num;
视图中不允许使用 ORDER BY。
视图的使用方式与表类似,如果需要排序数据,请在从视图中检索数据的 SELECT 中使用 ORDER BY。