第5章练习题-SQL基础教程

5.1 创建出满足下述三个条件的视图。使用 Product(商品)表作为参照表,假设表中包含初始状态的 8 行数据
条件 1:销售单价大于等于 1000 日元
条件 2:登记日期是 2009 年 9 月 20 日
条件 3:包含商品名称、销售单价和登记日期三列
对该视图执行 SELECT 语句的结果如下所示

第5章练习题-SQL基础教程

create view practice(product_name,sale_price,regist_date)
as
select product_name,sale_price,regist_date
from product
where sale_price >= 1000 and regist_date = '2009-9-20'
group by product_name;

5.2 向习题 5.1 中创建的视图 ViewPractice5_1 中插入如下数据,会得到什么样的结果呢

insert into practice
values
('刀子', 300, '2009-11-02');

视图和表需要同时进行更新,因此通过汇总得到的视图无法进行更新

5.3 请根据如下结果编写 SELECT 语句,其中 sale_price_all 列为全部商品的平均销售单价
第5章练习题-SQL基础教程

select product_id,product_name,product_type,sale_price,
	(select avg(sale_price)
	 from product) as sale_price_all
from product;

5.4 请根据习题 5.1 中的条件编写一条 SQL 语句,创建一幅包含如下数据的视图(名称为 AvgPriceByType)
第5章练习题-SQL基础教程

create view practice2 as
select product_id,product_name,product_type,sale_price,
	(select avg(sale_price)
	 from product p2
	 where p1.product_type = p2.product_type
     group by p1.product_type
    ) as avg_sale_price
from product p1;