sql server问题,下面的语句怎么写啊,请高手解答??谢啦。

[复制链接]
查看11 | 回复2 | 2011-7-12 19:37:19 | 显示全部楼层 |阅读模式
商品销售数据库Article(商品号 char(4),商品名char(16),单价 Numeric(8,2),库存量 int) Customer(顾客号char (4),顾客名 char (8),性别 char(2),年龄 int)OrderItem(顾客号 char(4),商品号 char(4),数量 int, 日期 date) 1 用SQL建立三个表,须指出该表的实体完整性和参照完整性,对性别和年龄指出用户自定义的约束条件。(性别分成男女,年龄从10到100)。顾客表的数据用插入语句输入数据,其它两表可用任意方式输入数据。create table OrderItem (顾客号 char(4),商品号 char(4),日期 datetime,数量 smallint,primary key (顾客号,商品号,日期),foreign key (商品号) references Article (商品号), foreign key (顾客号) references Custommer(顾客号) );2 检索定购商品号为‘0001’的顾客号和顾客名。select distinct 顾客号 from OrderItemwhere 商品号='0001' or 商品号='0002';3 检索定购商品号为‘0001’或‘0002’的顾客号。select distinct 顾客号 from OrderItemwhere 商品号='0001' or 商品号='0002'; 4 检索至少定购商品号为‘0001’和‘0002’的顾客号。(用交的方法) select 顾客号 from OrderItem where 商品号='0001' and 顾客号 in( select 顾客号 from OrderItem where 商品号='0002'); 5 检索至少定购商品号为‘0001’和‘0002’的顾客号。(用自表连接方法) select X. 顾客号 from OrderItem X,OrderItem Y where X.顾客号=Y.顾客号 and X.商品号='0001' and Y. 商品号='0002'; 6 检索没定购商品的顾客号和顾客名。select 顾客号,顾客名 from Custommer where 顾客号 not in (select 顾客号 from OrderItem); 7 检索一次定购商品号‘0001’商品数量最多的顾客号和顾客名。 select 顾客号,顾客名 from Custommer where 顾客号 in(select 顾客号 from OrderItem where 商品号='0001'and 数量=(select MAX(数量)from OrderItem where 商品号='0001'));8 检索男顾客的人数和平均年龄。select count(*) 人数,avg(年龄) 平均年龄 from Custommer where 性别='男';9 检索至少订购了一种商品的顾客数。select count(distinct 顾客号) from OrderItem;10 检索订购了商品的人次数。select count( 顾客号) from OrderItem;select count(distinct 顾客号) from OrderItem; 11 检索顾客张三订购商品的总数量及每次购买最多数量和最少数量之差。 select sum( 数量),MAX(数量)-MIN(数量) from OrderItem,Custommer where OrderItem.顾客号=Custommer.顾客号 and 顾客名='张三';12 检索至少订购了3单商品的顾客号和顾客名及他们定购的商品次数和商品总数量,并按商品总数量降序排序。

回复

使用道具 举报

千问 | 2011-7-12 19:37:19 | 显示全部楼层
看着头晕...
回复

使用道具 举报

千问 | 2011-7-12 19:37:19 | 显示全部楼层
.看着就晕了 就给0分.......这0分不值得我完你这题...
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行