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

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

1.查询在20XX年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.查询名称为“家电市场”的客户在20XX年3月18日购买的产品名称和数量。 SQL标准语句: select 产品名称,数量 from CP,XSS,CPXSB

where CP.产品编号=CPXSB.产品编号 and XSS.客户编号=CPXSB.客户编号 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.客户编号

实验六

查询在20XX年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.产品编号 )

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

select 产品名称,数量 from CP,CPXSB 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.客户编号 )

查询销售量大于所有20XX年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.客户编号 ) )

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

select 客户名称 from XSS where not exists (

select * from CP where exists (

select * from CPXSB x

where x.产品编号=CP.产品编号 and x.客户编号='000001' and not exists (

select * from CPXSB y

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

and 客户编号 !='000001'

实验七

(1)启动企业管理器,在产品销售数据库CPXS中创建价格小于2000的产品视图VIEW_CP_PRICE2000,

要求加密并保证对该视图的更新都要符合价格小于2000这个条件, 写出创建过程和对应的T-SQL语句: create view VIEW_CP_PRICE2000 with encryption as

select* from CP where 价格<2000

with check option

(2)打开查询分析器,用T-SQL语句创建各客户购买产品的情况VIEW_GMQK视图,包括客户编号、

客户名称、产品编号、产品名称、价格,购买日期、购买数量。 create view VIEW_GMQK as

select XSS.客户编号,客户名称,CP.产品编号,产品名称,价格,销售日期 as '购买日期',数量 as'购买数量'

from CP,XSS,CPXSB

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

(3)创建分区视图:在CPXS数据库中创建CP1和CP2两个表,CP1表中为编号小于等于’100010’

产品数据,CP1表中为编号大于‘100010’产品数据,以分区列为产品编号, 创建可更新的分区视图VIEW_CP12。 create table CP1

(产品编号 char(6) primary key check(产品编号<='100010'), 产品名称 char(30) not null, 价格 real, 库存量 int,

产品简列 varchar(50) )

create table CP2

(产品编号 char(6) primary key check(产品编号>'100010'), 产品名称 char(30) not null, 价格 real, 库存量 int,

产品简列 varchar(50) )

create view VIEW_CP12 as

select * from CP1 union all

select * from CP2

2、查询视图

联系合同范文客服:xxxxx#qq.com(#替换为@)