1.查询每个工程项目及其零件使用情况。(两表连接)

命令:select J.*,SPJ.* from J,SPJ where J.jno = SPJ.jno

或者:select J.jno,J.jname,J.city,SPJ.sno,SPJ.pno,SPJ.qty from J,SPJ where J.jno = SPJ.jno(自然连接)

2.查询每个工程项目及其零件使用情况,包括没有使用零件的工程项目。(两表外连接)

命令:select J.jno,J.jname,J.city,SPJ.sno,SPJ.pno,SPJ.qty from J LEFT OUTER JOIN SPJ on( J.jno = SPJ.jno )

3.查询使用供应商S1供应的零件P1的工程号和工程名(两种方法:连接,嵌套)

命令:select J.jno,jname from J,SPJ where J.jno = SPJ.jno and SPJ.sno = 'S1' and SPJ.pno = 'P1'(连接)

select jno,jname from J where jno in (select jno from SPJ where sno = 'S1' and pno = 'P1')(嵌套)

4.查询查询每个工程项目的工程号、工程名、使用的零件名、零件的供应商名及数量(多表连接)

命令:select J.jno,J.jname,SPJ.pno,S.sname,SPJ.qty from J,SPJ,S where J.jno = SPJ.jno and S.Sno = SPJ.sno

5.查询与供应商“东方红”在同一城市的供应商名(同表嵌套)

命令:select sname from S where city in (select city from S where sname = '东方红') and sname <> '东方红'

6.查询与供应商“精益”在同一城市的工程项目名(异表嵌套)

命令:select jname from J where city in (select city from S where sname = '精益')

7.查询供应了“螺丝刀”的供应商号和供应商名(两种方法:连接,嵌套)

命令:select DISTINCT S.sno,S.sname from S,P,SPJ where P.pname = '螺丝刀' and P.pno = SPJ.pno and SPJ.sno = S.sno(连接)

select sno,sname from S where sno in (select sno from SPJ where pno in (select pno from P where pname = '螺丝刀'))(嵌套)

8.查询使用零件P1且数量超过300的所有工程的工程号和工程名(带group by的两表嵌套)

命令:select jno,jname from J where jno in (select jno from SPJ where pno = 'P1' group by jno having SUM(qty) > 300)

9.查询每种颜色的零件中重量大于该颜色零件平均重量的零件信息(两种方法:相关子查询,基于派生表的查询)

命令:select * from P x where weight > (select AVG(weight) from P y where x.color = y.color)(相关子查询)

select pno,pname,color,weight from P,(select color,AVG(weight) from P group by color) as avg_p(avg_color,avg_weight) where P.color = avg_color and P.weight > avg_p.avg_weight (基于派生表的查询)

10.查询使用了S2供应的P3的工程号与使用了S3供应的P3的工程号的并集。(两种方法:集合查询,非集合查询)

命令:select jno from SPJ where sno = 'S2' and pno = 'P3' union select jno from SPJ where sno = 'S3' and pno = 'P3'(集合查询)

select DISTINCT jno from SPJ where sno = 'S2' and pno = 'P3' or ( sno = 'S3' and pno = 'P3')(非集合查询)

11.查询使用了S2供应的P3的工程号与使用了S3供应的P3的工程号的交集。

命令:select jno from SPJ where sno = 'S2' and pno = 'P3' intersect select jno from SPJ where sno = 'S3' and pno = 'P3'

12.查询使用了S2供应的P3的工程号与使用了S3供应的P3的工程号的差集。

命令:select jno from SPJ where sno = 'S2' and pno = 'P3' EXCEPT select jno from SPJ where sno = 'S3' and pno = 'P3'

13.找出没有使用天津供应商生产的红色零件的工程号。(减法查询)?

命令:select jno from J where jno not in ( select jno from SPJ where pno in ( select pno from P where color = '红') and  sno in (select sno from S where city = '天津') )

14.查询至少使用了供应商S1供应的全部零件的工程号。(除法查询)?

命令:

select DISTINCT jno

from SPJ SPJ_1

where not exists

(select *

from SPJ SPJ_2

where SPJ_2.sno = 'S1' and

not exists

(select *

from SPJ SPJ_3

where SPJ_3.jno = SPJ_1.jno and

SPJ_3.pno = SPJ_2.pno and SPJ_3.sno = 'S1'

)

)

15.查询给所有工程都供应了零件的供应商号。(除法查询)

命令: select sno

from S

where not exists

(select *

from J

where not exists

(select *

from SPJ

where SPJ.sno = S.sno and SPJ.jno = J.jno))

16.查询使用了所有零件的工程号。

命令:select jno from J where not exists ( select * from P where not exists ( select * from SPJ where pno = P.pno and jno = J.jno ))

17.查询所有工程都使用了的零件号。

命令:select pno from P where not exists ( select * from J where not exists (select * from SPJ where jno = J.jno and pno = P.pno))

18.查询供应了所有零件的供应商号。

命令:select sno from S where not exists (select * from P where not exists ( select * from SPJ where pno = P.pno and sno = S.sno))

19.查询所有供应商都供应了的零件号。

命令:select pno from P where not exists ( select * from S where not exists (select * from SPJ where sno = S.sno and pno = P.pno))

20.查询使用了所有供应商的零件的工程。

命令:select * from J where not exists (select * from S where not exists (select * from SPJ where sno = S.sno and jno = J.jno))

21.把全部红色零件的颜色改成蓝色。

命令:update P set color = '蓝' where color = '红'

22.由S5供给J4的零件P6改为由S3供应。

命令:update SPJ set sno = 'S3' where sno = 'S5' and pno = 'P6' and jno = 'J4'

23.从供应商关系中删除S2的记录,并从供应情况关系中删除相应的记录;?

命令:delete from SPJ where sno = 'S2'

delete from S where sno = 'S2'

24.请将(S2,J6,P4,200)插入供应情况关系。

命令:insert into SPJ(sno,jno,pno,qty) values('S2','J6','P4',200)

25.请为三建工程项目建立一个供应情况的视图,包括SNO,PNO,QTY。针对该视图完成下列查询:

  1. 找出三建工程项目使用的各种零件代码及其数量;
  2. 找出供应商S1的供应情况。

建立视图:

create view IS_Sanjian(sno,pno,qty)

as

select sno,pno,qty

from SPJ

where  jno in

(select jno

from J

where jname = '三建'

)

with check option

命令:(1):select pno as '零件号码',SUM(qty) as '数量' from IS_Sanjian group by pno

(2):select * from IS_Sanjian where sno = 'S1'

SQL server 上机实验 多表联查相关推荐

  1. 【SQL Server 上机实验题 】

    SQL Server 上机实验题 上机实验题1 创建数据库 创建四张表 插入四张表的数据 上机实验题2 上机实验题4 上机实验题5 上机实验题7 上机实验题1 创建数据库 create databas ...

  2. sql多表联查(内连接、外连接)、实验八表联查

    一.T-sql多表查询 1.内连接(inner join) 1.1概念 只返回两个数据集合之间匹配关系的行 1.2命令 select A.name姓名A,A.school学校A,B.name姓名B,B ...

  3. 如何使用SQL Server数据库实验助手(DEA)工具

    介绍 (Introduction) This is my second article about Database Experimentation Assistant (DEA). Please r ...

  4. 用友U8的SQL SERVER 数据库结构说明表

    用友U8的SQL SERVER 数据库结构说明表       在帐套中的两个表,一个表是RPT_GRPDEF,存放帐套中重要的表名及相关说明:另一个是RPT_ITMDEF,存放的是主要表中的相关字段说 ...

  5. 查看SQL Server被锁的表以及如何解锁

    锁定数据库的一个表的区别 SELECT * FROM table WITH (HOLDLOCK) 其他事务可以读取表,但不能更新删除 SELECT * FROM table WITH (TABLOCK ...

  6. SQL Server 2014 内存优化表(1)实现内存优化表

    内存优化表(Memory-Optimized Tables)是SQL Server 2014的新特性,目前仅适用于评估版(Evaluation Edition).开发版(Developer Editi ...

  7. SQL SERVER 2008不能修改表的解决方法(增加字段、修改字段名)(未解决)

    SQL SERVER 2008不能修改表的解决方法(增加字段.修改字段名)(未解决) 参考文章: (1)SQL SERVER 2008不能修改表的解决方法(增加字段.修改字段名)(未解决) (2)ht ...

  8. Sql server中如何将表A和表B的数据合并(乘积方式)

    sql server中如何将表A 和表B的数据合并成乘积方式,也就是说表A有2条数据,表B有3条数据,乘积后有6条数据, 这就要用到sql中的笛卡尔积方式了 1.什么是笛卡尔积 笛卡尔积在SQL中的实 ...

  9. 清空SQL Server数据库中所有表数据的方法(转)

    清空SQL Server数据库中所有表数据的方法 其实删除数据库中数据的方法并不复杂,为什么我还要多此一举呢,一是我这里介绍的是删除数据库的所有数据,因为数据之间可能形成相互约束关系,删除操作可能陷入 ...

最新文章

  1. python3 的 round 函数的 练习
  2. R语言伪相关性分析(Spurious Correlation)、相关关系不是因果关系:以缅因州离婚率数据集为例
  3. 深度洞见|一文详解 2022 数字营销的变与不变
  4. 国际会议“First Helicon Plasma Physics and Applications Worckshop”口头报告PPT
  5. 寫一個函數計算當參數為 n(n很大) 時的值 1-2+3-4+5-6+7……+n
  6. 指定一个actor对pawn不可见
  7. 移动互联网APP测试流程及测试点(转载)(一)
  8. Intel 64/x86_64/IA-32/x86处理器 - SIMD指令集 - SSE扩展(12) - 预取指令与SFENCE指令
  9. sql server死锁_了解SQL Server死锁图的图形表示
  10. docker存储驱动模式之direct-lvm配置
  11. 终于找全了!质量总监、质量经理、计划主管..质量部27个岗位职责
  12. linux下隐藏文件 和 显示隐藏文件命令
  13. 关于浏览器被劫持主页的处理方法(完结版)
  14. [ 大道至简系列 ] 三分钟理解-1NF,2NF,3NF
  15. Fiddler+雷电模拟器APP抓包
  16. 微机原理课程设计-模拟十字路口交通信号灯
  17. wxpython中表格顶角怎么设置_当wxGrid中的某个单元格以编程方式更改时,突出显示该行中的一行(使用wxPython)...
  18. 31 个Python代码实现的常用功能(附案例源码)
  19. 仕德伟 php 漏洞,网络爱心接力救助绝症女孩——仕德伟网络市场总监朱恋尽“微博”之力...
  20. IDEA无法自动导包问题

热门文章

  1. PAT (Basic Level) Practice_1039 到底买不买
  2. pytorch训练时显存溢出
  3. Mtlab中的小括号()、中括号[]、大括号{}的使用及区别
  4. CS224n 词的向量表示word2vec 之skipgram (Negative sampling )
  5. PP指数:推送、筛选、搜索,全方位洞悉用户观看习惯
  6. java调用微信加密_java微信消息加解密
  7. 天正出现未知命令 CAD输入命令时提示未知命令
  8. mac下解压缩rar文件工具-rarosx(免费)
  9. 基础 | batchnorm原理及代码详解
  10. RESIDE:Benchmarking Single Image Dehazing and Beyond