数据库--精选题库

1、查询和更新操作
1)找出所有客户、代理商和商品都在同一城市的三元组(cid, aid, pid)。
Select cid,aid,pid from customers,agents,products
Where customers.city=agents.city and agents.city=products.city;

2)找出所有客户、代理商和商品不都在同一城市(可能有两个在同一城市)的三元组(cid, aid, pid)。
Select cid,aid,pid from customers,agents,products
Where not (customers.city=agents.city and agents.city=products.city);

3)找出所有在同一城市的代理商的aid对。
Select a1.aid,a2.aid from agents a1, agents a2 where a1,aid<a2.aid and a1.city=a2.city;
4)找出同时订购了商品p01和p07的客户的cid值。(若找出客户的cname呢?)
Select o1.cid from orders o1, orders o2
Where o1.cid=o2.cid and o1.pid=’p01’ and o2.pid=’p07’;
5)统计各个产品的销售总量。
Select pid, sum(qty)
from orders
group by pid;
6)当某个代理商所订购的某样产品的总量超过1000时,打印出所有满足条件的产品和代理商的ID以及这个总量。
Select aid, pid, sum(qty)
from orders
group by aid, pid
having sum(qty)>1000;
7)找出订购了产品p05的顾客的名字。
方法一:
Select cname
from customers, orders
where customers.cid=orders.cid and orders.pid=’p05’;

		方法二:
			Select cname from customers
			Where cid in(select cid from orders where pid=’p05’);

		方法三:
			Select cname from customers
			Where exists
				(select * from orders where cid=customers.cid and pid=’p05’);

8)检索满足以下条件的顾客-代理商的姓名对(cname,aname),其中的顾客cname通过代理商aname订了货。
Select cname,aname
from customers, orders, agents
where customers.cid=orders.cid and orders.aid=agents.aid;

9)找出至少被两个顾客订购的产品的pid值。
方法一:
Select pid
From orders
Group by pid
Having count(distinct cid)>=2;

方法二:
Select o1.pid
From orders o1,orders o2
Where o1.pid=o2.pid and o1.cid<>o2.cid;
10)在customers表中插入一个新行。
Insert into customers(cid,cname,city) values (‘c007’,’WinDix’,’Dallas’);
11)检索customers表中discnt值为空的行。
Select * from customers where discnt is null;
12)检索客户以及他们订购商品的详细信息。(用外联接)
Select customers.,orders.
From customers left outer join orders on customers.cid=orders.cid;
13)检索有关住在Duluth或Dallas的代理商的所有信息。(要求使用IN谓词实现)
Select * from agents where city in(‘Duluth’,’Dallas’);
14)找出通过住在Duluth或Dallas的代理商订货的所有顾客的姓名和折扣率。(要求使用IN谓词实现)
Select cname, discnt
From customers
Where cid in(
Select cid from orders where aid in(
Select aid from agents where city in(‘Duluth’,’Dallas’)));
15)求所有满足以下条件的顾客的cid值:该顾客的discnt的值小于任一住在Duluth的顾客的discnt值。
方法一:
Select cid from customers where discnt <any (select discnt from customers where city=’Duluth’) ;

方法二:
Select cid from customers where discnt<
(select max(discnt) from customers where city=’Duluth’);
16)检索没有通过代理商a05订货的所有顾客的名字。
提示:可以使用not in 或 <>all方式实现。
方法一:
Select cname from customers where cid not in(
Select cid from orders where aid=’a05’);

		方法二:
			Select cname from customers where not exists
				(select * from orders where cid=customers.cid and aid=’a05’);

17)检索一个包含顾客所在的或者代理商所在的城市的名称。(使用UNION实现)
Select city from customers
Union
Select city from agents;
18)在orderrs表中插入一个新行。
Insert in to orders(ordno,month,cid,aid,pid) values (1107,’aug’,‘c006’,’a04’,’p01’);
19)创建一个名为swcusts的表,它包含住在西南部的所有顾客,并向该表中插入所有来自Dallas或Austin的顾客。
Create table swcusts(cid char(4) not null, cname varchar(13),
city varchar(20), discnt real);
Insert into swcusts select * from customers where city in(‘Dallas’,’Austin’);

20)将所有住在New York的代理商的佣金率提高10%。
Update agents
Set percent=percent*1.1
Where city=’New York’;
21)用customers表中最新的discnt值来更新swcusts表中的各行discnt的值。
Update swcusts set discnt=(select discnt from customers where cid=swcusts.cid);
22)删除所有住在New York的代理商。
Delete from agents
Where city=’New York’;
23)创建一个agentorders视图,它扩展了表orders的行,包括订货的代理商的详细信息。
create view agentorders
as
select ordno,month,cid,orders.aid,pid,qty,dollars,aname,city,per
from orders,agents
where orders.aid=agents.aid;
24)利用agentorders视图查询代理商Brown的所有订单信息
Select ordno,motn,cid,pid,qty,dollars
From agentorders
Where aname=’Brown’;
25)创建cacities视图,该视图列出表customers和表agents中所有配对的城市,其中该顾客通过该代理商订购了商品。

26)创建custs视图
create view custs as select * from customers where discnt<=15.0 with check option;
27)对custs视图进行更新操作。
Update custs set discnt=discnt+4;

  • 10
    点赞
  • 85
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值