Product数据库


基与产品的数据库模式,完成下列查询:
Product(maker,model,type);
PC(model,speed,ram,hd,price);
Laptop(model,speed,ram,hd,screen,price);
Printer(model,color,type,price);
要求每题的答案应当至少使用一个子查询,并且要求使用两种不同的方法写出每个查询(例如使用不同的运算符:exists,in,all,any等)。

建表

创建、使用数据库

create database product;
use product;

在这里插入图片描述

建表

create table product
(maker char(20),
model char(10),
type char(20)
);

在这里插入图片描述

插入数据

insert into product
values('A','01','PC'),
('A','01','Laptop'),
('B','02','Laptop'),
('B','02','Printer'),
('B','03','Printer'),
('B','04','PC'),
('C','01','PC'),
('C','01','Laptop'),
('C','01','Printer'),
('C','02','Printer'),
('C','03','Laptop'),
('C','04','PC'),
('C','04','Printer');

在这里插入图片描述

建表

create table PC
(model char(10),
speed double,
ram int,
hd int,
price double
);

在这里插入图片描述

插入数据

insert into PC
values('01',2.1,512,80,3000),
('01',2.1,1024,160,3500),
('01',2.2,1024,160,3600),
('01',3.2,2048,200,3700),
('02',2.2,512,200,3800),
('02',3.3,2048,320,5000),
('02',3.2,2048,330,6000),
('03',2.3,1024,250,4800),
('03',3.2,1024,250,5100),
('04',2.2,1024,250,3900),
('04',3.3,1024,80,3800),
('04',3.3,1024,250,5500);

在这里插入图片描述

建表

create table Laptop
(model char(10),
speed double,
ram int,
hd int,
price double
);
插入
insert into Laptop
values('01',2.1,512,80,2000),
('01',2.1,1024,160,2500),
('01',2.2,1024,160,2600),
('01',3.2,2048,200,2700),
('02',2.2,512,200,2800),
('02',3.3,2048,320,4000),
('02',3.2,2048,330,5000),
('03',2.3,1024,250,3800),
('03',3.2,1024,250,4100),
('04',2.2,1024,250,2900),
('04',3.3,1024,80,2800),
('04',3.3,1024,250,4500);

在这里插入图片描述

建表

create table Printer
(model char(10),
color boolean,
type char(10),
price double
);

插入数据

insert into printer
values('01',true,'激光',1000),
('02',false,'激光',1000),
('03',true,'喷墨',3000),
('04',false,'喷墨',2000);

在这里插入图片描述

1)找出速度在3.0以下的PC制造商。

select distinct(maker)
from product
where model in
(select model
from PC
where speed<3.0);

在这里插入图片描述

select distinct(maker)
from product
where exists
(select *
from PC,product
where PC.model=product.model and speed<3.0);

在这里插入图片描述

2)找出价格最高的打印机的相关信息。

select * from Printer
where price>=all
(select price from Printer);

在这里插入图片描述

select * from Printer
where price=
(select max(price) from Printer);

在这里插入图片描述

3)找出速度比任何一台PC都慢的笔记本电脑。

select * from Laptop
where speed <all
(select speed from PC);

在这里插入图片描述

select * from Laptop
where speed <
(select min(speed) from PC);

在这里插入图片描述

4)找出价格最高的产品(PC,笔记本电脑或者打印机)的型号。

union的介绍https://www.runoob.com/sql/sql-union.html

select model from 
(select model,price from pc union 
select model,price from printer union 
select model,price from laptop) a 
where a.price in(select max(price) from
(select price from pc union 
select price from printer union 
select price from laptop)b);

在这里插入图片描述

select model from
(select model,price from pc union  
select model,price from laptop union 
select model,price from printer)b  
where b.price>=all(
select price from pc union 
select price from laptop union 
select price from printer);

在这里插入图片描述

5)找出价格最低的彩色打印机的制造商。

select distinct(maker) from Product,Printer
where Printer.price<=all
(select price from printer
where color=1 )and Product.model=Printer.model;

在这里插入图片描述

select distinct(maker) from Product,Printer
where Printer.price=
(select min(price) from printer
where color=1 )and Product.model=Printer.model;

在这里插入图片描述

6)找出RAM容量最小而PC中速度最快者的制造商。

select distinct(maker) from product,pc
where pc.model=product.model and
ram =(select min(ram) from pc) and
speed =(select max(speed) from pc where ram =(select min(ram) from pc));

在这里插入图片描述

碎碎念

我个人水平一般,也没学视图什么的,感觉4、6题还是蛮难的,网上的题型很多,这两题的答案也是借鉴了不少大佬的解决办法。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值