数据库原理与应用课程 实验指导书(附答案) 联系客服

发布时间 : 星期四 文章数据库原理与应用课程 实验指导书(附答案)更新完毕开始阅读

数据库原理与应用课程实验指导书

insert into CP

values('','微波炉',1000,100,NULL)

insert into CP

values('','抽油烟机',1200,50,NULL)

alter table CP

add 产品简列 varchar(50)

update CP

set 价格=价格*0.8

delete from CP

where 价格*0.9<1500

实验四: 1.

select 产品编号,产品名称,价格 from CP

select 客户编号 as 'Customer id' ,客户名称 as 'Customer name' from XSS

where 地区='南京'

select 产品编号,产品名称,评价= case

when 价格<1000 then '廉价产品'

when 价格>=1000 and 价格<2000 then '一般产品' when 价格>=2000 and 价格<3000 then '昂贵产品' else '很昂贵产品' end from CP

select 产品编号,产品名称,产品总值=价格*库存量 from CP

select 客户编号 from CPXSB

group by 客户编号

having count(产品编号)>=1

37

select * from CP

where 价格>1000 and 价格<2000

select * from CP

where 产品名称 like '冰箱_A' 2.

select 总价格=sum(价格) from CP

select 产品编号 ,sum(销售额) 销售额 from CPXSB

where 销售日期='2004-3-18' group by 产品编号

select 客户编号 from CPXSB

group by 客户编号

having count(产品编号)>=2

实验五

1.查询在2004年3月18日有销售的产品名称(不允许重复)。 SQL标准语句:

select distinct 产品名称 from CP,CPXSB

where CP.产品编号=CPXSB.产品编号 and 销售日期='2004-3-18'

T-SQL扩展语句: select distinct 产品名称 from CP inner join CPXSB

on CP.产品编号=CPXSB.产品编号 and 销售日期='2004-3-18'

2.查询名称为“家电市场”的客户在2004年3月18日购买的产品名称和数量。 SQL标准语句: select 产品名称,数量 from CP,XSS,CPXSB

where CP.产品编号=CPXSB.产品编号 and XSS.客户编号=CPXSB.客户编号

38

数据库原理与应用课程实验指导书

and 销售日期='2004-3-18' and 客户名称='家电市场'

T-SQL扩展语句: select 产品名称,数量 from CP inner join CPXSB

on CP.产品编号=CPXSB.产品编号 and 销售日期='2004-3-18' inner join XSS

on XSS.客户编号=CPXSB.客户编号 and 客户名称='家电市场'

3.查找所有产品情况及销售他们的销售日期、购买他们的客户编号和数量, 若产品没有销售记录,也要包括其情况。 select CP.*,销售日期, 客户编号,数量 from CP left outer join CPXSB

on CP.产品编号=CPXSB.产品编号

4.查找所有购买情况,若客户没有购买产品,也要包括其情况。 select XSS.*,产品编号,销售日期,数量,销售额 from CPXSB right join XSS

on XSS.客户编号=CPXSB.客户编号

实验六

查询在2004年3月18日没有销售的产品名称(不允许重复)。 用IN子查询:

select distinct 产品名称 from CP

where 产品编号 not in

(select 产品编号 from CPXSB where 销售日期='2004-3-18')

用EXISTS子查询:

select distinct 产品名称 from CP

where not exists (select * from CPXSB

where 销售日期='2004-3-18'and CP.产品编号=CPXSB.产品编号 )

查询名称为“家电市场”的客户在2004年3月18日购买的产品名称和数量。 用IN子查询:

select 产品名称,数量 from CP,CPXSB

39

where 客户编号 in

( select 客户编号 from XSS where 客户名称='家电市场' )

and 销售日期='2004-3-18' and CP.产品编号=CPXSB.产品编号

用EXISTS子查询:

select 产品名称,数量 from CP,CPXSB

where CP.产品编号=CPXSB.产品编号 and 销售日期='2004-3-18' and exists (

select * from XSS where 客户名称='家电市场'and XSS.客户编号=CPXSB.客户编号 )

查询销售量大于所有2004年3月18日销售的各产品销售数量的产品编号。 用ALL谓词:

select 产品编号 from CPXSB group by 产品编号 having sum(数量)>all

(select sum(数量) from CPXSB where 销售日期='2004-3-18' group by 产品编号 )

查询购买了所有产品的客户的名称。 select 客户名称 from XSS

where not exists (

select * from CP where not exists (

select * from CPXSB

where 产品编号=CP.产品编号 and 客户编号=XSS.客户编号 ) )

查询购买了客户编号为“”的客户购买的所有产品的客户的名称。

40