sql語句關系代數練習


sql語句關系代數練習

對於表結構:

product(maker,model,type)

maker:表示生產廠商

model:生產的產品型號

type:產品類型,有pc laptop printer三種

pc(model,speed,ram,hd,price)

表示型號,速度,內存大小,硬盤大小,價格

laptop(model,speed,ram,hd,screen,price)

表示型號,速度,內存大小,硬盤大小,屏幕大小和價格

printer(model,color,type,price)

model:打印機型號;

color:是否彩色, T 彩色,F 黑白

type:類型,ink-jet 表示噴墨, laser 表示激光;

price:單價

這是一套難度大得不溜秋的題嚶嚶嚶,本人水平有限,對語句的理解還不夠深,語句不一定都是最優解,有的題目有多種解,如有疑問錯誤歡迎指出~

查詢生產pc也生產laptop的廠商

select product.maker
from product 
join laptop
on product.model = laptop.model
intersect
select product.maker
from product 
join pc
on product.model = pc.model

查詢生產型號為2001的廠商信息及該型號所屬產品的種類

select maker,type from product where model='2001'

查詢廠商A 生產的PC中price大於900的產品型號

select product.model
from product
inner join pc
on product.model=pc.model
where pc.price>900 and product.maker='A'

查詢廠商B生產的所有產品的型號和價格

select product.maker,printer.price
from product
inner join printer
on product.model = printer.model
where printer.type = 'ink-jet'

分類統計廠商(maker)A生產的各種產品的數量

select type,count(model) from product where maker='A' group by type

查詢所有彩色激光打印機的生產商及型號

select a.maker,a.model
from product a
inner join printer b
on a.model = b.model
where b.color = 'T' and b.type = 'laser'

找出生產產品型號最多的廠商

select top 1 maker from product group by maker order by count(*) desc

查詢出哪個生產廠商的筆記本電腦的硬盤容量至少100G

select a.maker
from product a
inner join laptop b
on a.model = b.model
group by a.maker
having min(b.hd)>=100

找出那些既出售筆記本電腦又出售PC的廠商

select product.maker
from product
inner join pc
on product.model = pc.model
group by product.maker
intersect
select product.maker
from product
inner join laptop
on product.model = laptop.model

查詢具有同樣處理速度和同樣內存大小的PC對

顯示滿足條件的pc對的型號,同樣的pc對只出現1次,如001 與 002 符合條件, 則僅出現001 002,不出現002 001

select a.model,b.model
from pc a,pc b
where a.speed=b.speed and a.ram=b.ram and a.model<>b.model and a.model<b.model

查詢至少生產三種不同處理速度電腦(含pc和laptop)的廠商

select maker from
(
	select maker, product.model, speed from 
	product join pc on product.model = pc.model 
	union
	select maker, product.model, speed from 
	product join laptop on product.model = laptop.model
) as a
group by maker
having count(distinct speed) >= 3;

統計出pc,laptop,printer三種產品的不同型號數量,並按數量從大到小排序

select type, count(model) as c from
(
	select type, product.model as model from 
	product join pc on product.model = pc.model 
	union
	select type, product.model as model
	from product join laptop on product.model = laptop.model
	union 
	select product.type, product.model as model
	from product join printer on product.model = printer.model
) as a
group by type
order by c desc;

有客戶有1500元錢,買laptop或pc,要求硬盤容量不小於80,請給出可能的產品型號,生產廠商

select product.maker,product.model from product
inner join laptop on product.model = laptop.model where laptop.hd >=80 and laptop.price <=1500
union
select product.maker,product.model from product
inner join pc on product.model = pc.model where pc.hd >=80 and pc.price <=1500

找出至少生產5種不同型號產品的廠商

select maker from product
group by maker
having count(*)>=5

完成功能使得:廠商A的產品升級,所有pc速度提高100,硬盤容量增加50,價格上調300

Update pc SET pc.speed = pc.speed +100,pc.hd=pc.hd+50,pc.price=pc.price+300
FROM product INNER JOIN pc ON product.model=pc.model
where product.maker='A'

完成功能:廠商E新增一種pc產品,型號1090,速度200,內存128,硬盤160,價格800

insert into pc values ("1090",200,128,160,800);

insert into product values ("E","1090","pc");

完成功能:廠商C生產的laptop,型號2013停產,需要從相關關系種去除

delete a
from product a
inner join laptop b
on a.model = b.model
where a.maker='C' and a.model = '2013'

完成功能:廠商A生產的產品1001型號變更為1091,其余信息不變

Update b SET b.model = '1091'
FROM product a INNER JOIN pc b
ON a.model=b.model
where a.maker='A' and b.model = '1001';

Update a SET a.model = '1091'
FROM product a 
where a.model = '1001';

廠商H破產,所有其生產的產品需移除

delete b from product a inner join pc b
on a.model = b.model
where a.maker = 'H';
delete b from product a inner join laptop b
on a.model = b.model
where a.maker = 'H';
delete b from product a inner join printer b
on a.model = b.model
where a.maker = 'H';

delete a from product a
where a.maker ='H';


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM