(学号+姓名)数据库实验报告二
浙江万里学院实验报告
成绩: 课程名称:数据库管理技术
教师: 实验名称:试验二 SQL程序
设计
领导形象设计圆作业设计ao工艺污水处理厂设计附属工程施工组织设计清扫机器人结构设计
专业班级:电商114班姓名:张俊婷学号:2011014790实验日期:12.3 一、实验目的:
1、掌握SQL的数据更新、查询语句 2、学习T-SQL程序设计 二、实验内容:
1、T-SQL语句
1)表的定义
在P113表的基础上,在数据库中定义一个零件颜色统计(PC)表,其属性有零件颜色(COLOR)、零件种类数量(PQTY)
Create table PC
(color char(2) primary key,
Pqty int not null)
2)数据插入
在供应者(S)表中加入一行
记录
混凝土 养护记录下载土方回填监理旁站记录免费下载集备记录下载集备记录下载集备记录下载
,其中SNO为S6,SNAME为N6,CITY为宁波。 Insert into S(Sno,Sname,city) values(‘S6',‘N6',‘city')
在零件表中统计每种颜色零件的数量,将它们插入到到零件颜色统计(PC)表中 Insert into PC(Pqty)
Select count(Pqty)
From PC
Group by color
专业班级: 姓 名: 学号: 实验日期:
3)数据查询
A)单表查询
查询所在城市为上海的工程的所有信息
Select *
From J
Where city=‘上海'
查询所需零件总数大于1000的工程代号及其零件总数,请按工程代号降序排列,工程代
号相同的请按零件总数升序排列
Select Jno,Sum(qty)
From SPJ
Where Sum(qty)>‘1000'
Order by Jno desc,sum(qty) asc
B)多表查询
查询由供应者S1提供零件的工程名称(请用联接查询,带in的嵌套查询,和带存在量
词exists的嵌套查询三种方法完成)
Select Jname Select Jname
From J From J
Where Jno in Where exists
(Select Jno (Select *
From SPJ From SPJ
Where Sno=‘S1’) Where Sno=‘S1’and SPJ.Jno=J.Jno)
Select Jname
From J,SPJ
Where Sno ='S1'and SPJ.Jno=J.Jno
2
专业班级: 姓 名: 学号: 实验日期: 查询上海供应者不提供任何零件的工程的代号
Select Jno Select Jno
From J From J
Where Pno in Where exists
(Select Pno (Select *
From SPJ From SPJ
Where Sno Where SPJ.Jno=J.Jno and exists
(select Sno (Select *
From S From S
where city='上海')) where city='上海'and SPJ.Sno=S.Sno)) Select Jno
From J,SPJ
Where J.Jno=SPJ.Jno
查询与零件名PN1相同颜色的零件代号
Select Pno Select Pno Select Y.Pno From P From P From P X,P Y
Where color in Where exists Where X.Pname='PN1'and X.color=Y.color
(select color (select *
From P From P
Where Pname='PN1') Where Pname='PN1'and color=color) 4)数据更新
将所有使用北京供应商供应的零件的数据加10%
Update SPJ set qty=qty*1.1 where Sname in
(select Sname from S where Sname='北京')
5)数据删除
在零件(P)表中删除所有红色零件
Delete from P where color in (select color from SPJ where color='红色') 在供应(SPJ)表中删除所有为南京供应商提供的供应记录。 Delete from SPJ where Sno in (select Sno from S where city='南京')
3