【数据库 关系代数和SQL】经典查询总结

SQL语句例题:

  1. 同时选修08305001和08305002的课的同学姓名,学号

    select X.xh,S.xm from E X,E Y,S where X.xh=Y.xh and X.kh='08305001' and Y.kh='08305002' and S.xh=X.xh;

  2. 至少使用零件编号P3和P5的工程编号JNO(和上一题同类型)

    select distinct X.jno from SPJ X,SPJ Y where X.jno=Y.jno and X.pno='P3' and Y.pno='P5';

  3. 驾驶了所有汽车的驾驶员姓名和驾驶证号

    分为三个步骤:

    1. 查找驾驶员驾驶汽车的所有记录

      select * from BC where dno=D.dno and cno=C.cno;

    2. 查找驾驶员没驾驶的汽车的记录(在所有汽车里面找没有驾驶记录的车)

      select * from C where not exists(select...);

    3. 查找驾驶了所有汽车的驾驶员

      select dname,dno from D where not exists(select * from C where not exists(select * from D,BC where D.dno=BC.dno and ));

  4. 全部驾驶员都驾驶过的汽车的车牌号,车类型

    分为三个步骤:

    1. 查找驾驶员驾驶汽车的记录

      select * from BC where dno=D.dno and cno=C.cno;

    2. 查找没驾驶全部车的驾驶员(如果驾驶了全部车 那应该任何一个not exists都不会满足 所以不会出现在这里)

      select * from D where not exists(select...);

    3. 查找所有驾驶员都驾驶过的汽车

      select cname,cno from C where not exists(select * from D where not exists(select * from BC where dno=D.dno and cno=C.cno));

  5. 查询2018年为超过10个部门服务的车牌号,车型,服务的部门数量,按照部门数量降序,如果数量相同按照车牌号升序

    select cno,ctype,count(distinct(bno)) as b_number from C,BC where year(rq)='2018' and C.cno=BC.cno group by cno having b_number>10 order by b_number desc,cno asc;

  6. 更新部门B09订购的’2020-10-6’的车,改为所有车中价格最高的车

    update BC set CNO=SOME(select price from C where price=MAX(select price from C)) where rq='2020-10-6' and bno='B09';

  7. 不使用编号P3零件的工程编号和姓名

    select jno,jname from J where not exists(select * from SPJ where SPJ.jno=J.jno and SPJ.pno='P3');

  8. 至少不使用P3和P5零件的工程编号JNO

    select jno from J where jno not in(select jno from spj where pno in('p3','p5'));

  9. 使用所有零件的工程名称

    三步拆解:

    1. SPJ中所有供应记录

      select * from SPJ where J.jno=SPJ.jno and P.pno=SPJ.pno;

    2. 没被全部用到的零件的记录集合

      select * from P where not exists(select...)

    3. 使用了全部零件的工程集合

      select jname from J where not exists(select * from P where not exists(select * from SPJ where J.jno=SPJ.jno and P.pno=SPJ.pno));

  10. 查询至少使用了一种“东方配件厂“的零件的工程编号

    select distinct jno from SPJ where sno=ANY(select sno from S where sname="东方配件厂");

  11. WAN同学没有选修的课号

    SQL中 否定和没有 先一个EXISTS 里面一个NOT EXISTS

    select cno from C where exists( select * from S where exists S.sname='WAN' and not exists( select * from SC where SC.sno=S.sno and C.cno=SC.cno));

  12. 至少使用了供应商编号为S1所供应的全部零件的工程编号JNO

    三步拆解:

    1. SPJ中所有的S1使用的零件

      select * from SPJ Z where Z.pno=Y.pno;

    2. 没用到全部的S1零件的记录

      select * from SPJ Y where Y.sno='S1' and not exists(select...);

    3. 用到了全部的零件的记录

      select distinct jno from SPJ X where not exists(select * from SPJ Y where Y.sno='S1' and not exists(select * from SPJ Z where Z.jno=X.jno and Z.pno=Y.pno));

数据库关系代数

  1. 所有和包含关系 至少有 全部 所有…(用除法)

  2. 不 没有… 否定和非的关系 用差(减)

  3. 同时选两门课 同时有两条记录(用笛卡尔积 根据列序号选择)

数据库SQL语句

  1. 聚合函数 GROUP BY和HAVING

  2. 所有xxx NOT EXISTS(NOT EXISTS)

    把EXISTS理解为 在自查询中找得到的 NOT EXISTS就是在自查询中找不到的

  3. 最大最小 MAX MIN ANY ALL

  4. 修改某个地方的值

  5. 不 没有… 一个NOT EXISTS

一定要记得 GROUP BY 和 HAVING

  • 5
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

妮妮学姐

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值