数据库SQL查询作业二--水手船

一、创建三张表

①Sailors(sid char(10),sname char(20),rating int,age int),

其中sid是主关键字,sid表示水手的编号,sname表示水手的姓名,rating表示水手的级别,age表示水手的年龄。

②Boats(bid char(10),bname char(20),color char(10)),

其中bid表示船的编号是主关键字,bname是船的名字,color是船的颜色

③Reserves(sid char(10),bid char(10),rdate date),

Reserves中记录水手在哪天定了那只船,其中sid是指向Sailors的外关键字,bid是指向Boats的外关键字,(sid,bid,rdate)合起来构成Reserves的主关键字。

1.查找定了103号船的水手
select s.*, r.bid
from sailors s, reserves r
where s.sid = r.sid and r.bid = '103';

2.查找定了红色船水手的姓名
select s.sname
from sailors s
where s.sid in (select sid from reserves where bid in (select bid from boats where color = 'red'));

3.查找定了红色船而没有定绿色船的水手姓名
select distinct s.sname
from sailors s
where s.sid in (select sid from reserves where bid in (select bid from boats where color = 'red'))
and s.sid not in (select sid from reserves where bid in (select bid from boats where color = 'green'));

4.查找没有定过船的水手信息
select s.*
from sailors s
where s.sid not in (select sid from reserves);

5.查找定过船而没有定过红色船的水手信息
select s.*
from sailors s
where exists (select * from reserves r where s.sid = r.sid)
and not exists (select * from reserves r, boats b where b.bid = r.bid and s.sid = r.sid and b.color = 'red');

6.查找没有定过红色船的水手信息
select s.*
from sailors s
where not exists (select * from reserves r where s.sid = r.sid)
and not exists (select * from reserves r, boats b where b.bid = r.bid and s.sid = r.sid and b.color = 'red');

7.查找定过所有船的水手姓名和编号
select s.sname, s.sid
from sailors s
where not exists (select * from boats b where not exists (select * from reserves r where s.sid = r.sid));

8.查找年龄最大的水手姓名和年龄
select s.sname, s.age
from sailors s
where s.age >= all(select age from sailors);

9.统计水手表中每个级别组的平均年龄和级别组
select s.rating, avg(s.age)
from sailors s
group by s.rating;

10.统计水手表中每个人数不少于2人的级别组中年满18岁水手的平均年龄和级别组
select s.rating, avg(s.age)
from sailors s
where s.age > 18
group by s.rating having count(s.rating) >= 2;

11.统计水手表中每个级别组的人数
select s.rating, count(s.rating)
from sailors s
group by s.rating;

12.统计水手表中人数最少的级别组及人数
select s.rating, count(s.rating)
from sailors s
group by s.rating
having count(s.rating) <= all(select count(rating) from sailors group by rating);

13.查找定过船而没有定过相同的船的水手姓名
select s.sname
from sailors
where s.sid in (select sid from reserves)
and s.sid not in (select sid from reserves group by bid having count(bid) >= 2);

14.将年龄小于30的水手级别+1
update sailors
set rating=rating+1
where age < 30;

15.删除名字叫lubber的水手的定船信息.
delete from reserves where sid = (select sid from sailors where sname = 'lubber'); 
  • 4
    点赞
  • 65
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
create table sailors( sid char(10) primary key, sname char(20), rating int, age int); create table boats( bid char(10) primary key, bname char(20), color char(10)); create table reserves( sid char(10) , bid char(10) , rdate date, primary key(sid,bid,rdate), foreign key (sid) references sailors(sid) on delete cascade, foreign key (bid) references boats(bid) on delete cascade); insert into sailors(sid,sname,rating,age) values("22","dustin",7,45) ("29","brustus",1,33), ("31","lubber",8,56), ("32","andy",8,26), ("58","rusty",10,35), ("64","horatio",7,35), ("71","zorba",10,35), ("74","horatio",9,35), ("85","art",3,26), ("86","john",1,17), ("95","bob",3,64), ("96","frodo",3,26), ("98","tom",3,17); insert into boats(bid,bname,color) values("101","A","red"), ("102","B","green"), ("103","C","blue"), ("104","D","white") ("105","E","red"), ("106","F","blue"), ("107","G","green"); insert into reserves(sid,bid,rdata) values("22","101","2010-01-08"), ("22","102","2010-01-09"), ("29","103","2010-01-09"), ("31","102","2010-02-11"), ("22","104","2010-03-08"), ("22","103","2010-03-10"), ("32","105","2010-03-11"), ("32","106","2010-03-18"), ("32","102","2010-03-19"), ("58","104","2010-03-20"), ("64","105","2010-03-20"), ("95","101","2010-04-02"), ("85","102","2010-04-05"), ("22","101","2010-04-07"), ("22","105","2010-05-01"), ("22","106","2010-06-18"), ("22","107","2010-07-09"), ("31","106","2010-08-06"), ("32","105","2010-08-06"), ("29","104","2010-08-07"), ("64","103","2010-09-05"), ("58","102","2010-09-09"), ("64","104","2010-11-03"), ("64","105","2010-11-04"), ("31","106","2010-12-0

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值