数据更新数据库实验3.docx
《数据更新数据库实验3.docx》由会员分享,可在线阅读,更多相关《数据更新数据库实验3.docx(18页珍藏版)》请在冰点文库上搜索。
数据更新数据库实验3
数据库基础与实践实验报告
实验三数据更新
班级:
惠普测试142班
学号:
**********
姓名:
***
日期:
2016.11.9
1实验目的:
1)掌握SQL进行数据添加的方法;
2)掌握SQL进行数据修改的方法;
3)掌握SQL进行数据删除的方法。
2实验平台:
操作系统:
Windowsxp。
实验环境:
SQLServer2000以上版本。
3实验内容与步骤
利用实验一创建的sch_id数据库完成下列数据更新,并对语句的功能进行测试。
1.向数据库的每张用户表(除SC表)中至少添加3条元组,其中S表中插入2位计算机专业的同学,一位非计算机专业的同学。
代码:
insertintoDvalues('D4','动漫')
insertintoDvalues('D5','体育')
insertintoDvalues('D6','金融')
insertintoTvalues('T7','张三','男','30','教授','2000','2000','D4')
insertintoTvalues('T8','李四','男','35','讲师','1200','1500','D5')
insertintoTvalues('T9','王五','女','40','副教授','1400','1500','D6')
insertintoSvalues('S9','李华','男','20','D1')
insertintoSvalues('S10','张明','男','21','D1')
insertintoSvalues('S11','张丽','女','23','D5')
insertintoSvalues('S12','王华','女','18','D6')
insertintoSvalues('S13','李欣美','女','18','D4')
insertintoCvalues('C6','语文','36')
insertintoCvalues('C7','线代','40')
insertintoCvalues('C8','数据库','36')
insertintoCvalues('C9','马克思','36')
insertintoTCvalues('T7','C6')
insertintoTCvalues('T8','C7')
insertintoTCvalues('T8','C8')
insertintoTCvalues('T9','C9')
运行结果截图:
2.向SC表中插入选课记录,为计算机专业的同学选上全部课程,成绩取值为空值。
代码:
deletefromSCwhereexists(select1fromS,DwhereSC.sno=S.snoandS.dno=D.dnoandD.dn='计算机')
insertintoSC(sno,cno)selectsno,cnofromS,C,DwhereS.dno=D.dnoandD.dn='计算机'
运行结果截图:
SC表数据更新后的查询结果截图:
select*fromSC
3.将课时大于等于80学时的课程全部改为72学时。
代码:
:
selectcno课程编号,cn课程名,ct课时fromCwherect>=80
updateCsetct=72wherect>=80
selectcno课程编号,cn课程名,ct课时fromCwherect=72
测试记录:
C表数据更新前的查询结果截图:
更新语句运行结果截图:
C表数据更新后的查询结果截图:
4.删除成绩为空值的选课记录。
代码:
:
selectsno学号,cno课程编号,score分数fromSCwherescoreISNULL
deletefromSCwherescoreISNULL
selectsno学号,cno课程编号,score分数fromSC
测试记录:
SC表数据更新前的查询结果截图:
运行结果截图:
SC表数据更新后的查询结果截图:
5.删除姓名为刘伟的老师的授课记录。
代码:
:
selecttn教师姓名,cn所授课程fromTC,T,CwhereTC.tno=T.tnoandTC.cno=C.cnoandT.tn='刘伟'
deletefromTCwheretno=(selecttnofromTwheretn='刘伟')
selecttn教师姓名,cn所授课程fromTC,T,CwhereTC.tno=T.tnoandTC.cno=C.cnoandT.tn='刘伟'
测试记录:
T表数据更新前的查询结果截图:
运行结果截图:
T表数据更新后的查询结果截图:
6.调整岗位津贴,教授岗贴增长10%,副教授岗贴增长20%,讲师岗贴增长30%。
代码:
selecttn姓名,prof职称,sal工资fromTwhereprof='教授'
unionall
selecttn姓名,prof职称,sal工资fromTwhereprof='副教授'
unionall
selecttn姓名,prof职称,sal工资fromTwhereprof='讲师'
--updateTsetsal=sal*1.1whereprof='教授'
--updateTsetsal=sal*1.2whereprof='副教授'
--updateTsetsal=sal*1.3whereprof='讲师'
--一条update语句实现
updateTsetsal=sal*caseprof
when'教授'then1.1
when'副教授'then1.2
when'讲师'then1.3
end
whereprofin('教授','副教授','讲师')
测试记录:
T表数据更新前的查询结果截图:
运行结果截图:
T表数据更新后的查询结果截图:
7.将基本工资低于平均基本工资的教师的工资增长10%。
代码:
:
selecttn姓名,prof职称,sal工资fromTwheresal<(selectavg(sal)fromT)
updateTsetsal=sal*1.1wheresal<(selectavg(sal)fromT)
selecttn姓名,prof职称,sal工资fromT
测试记录:
T表数据更新前的查询结果截图:
运行结果截图:
T表数据更新后的查询结果截图:
8.将基本工资低于同职称教师基本工资的教师工资增长10%。
代码:
:
selecttn姓名,prof职称,sal工资fromTorderbyprof,saldesc
selecttn姓名,prof职称,sal工资fromTt1whereexists
(select1fromTwheret1.salorderbyprof,saldesc
updateTsetsal=sal*1.1fromTt1whereexists(select1fromTwheret1.salselecttn姓名,prof职称,sal工资fromTorderbyprof,saldesc
测试记录:
T表数据更新前的查询结果截图:
查询基本工资低于同职称教师基本工资的教师:
运行结果截图:
T表数据更新后的查询结果截图:
4讨论
1)构造一个新的数据更新需求,并用带exists的子查询完成该数据更新。
--将工资和奖金都低于2500元的教师的奖金提高10%
updateTsetcomm=comm*1.1fromTt1wherenotexists(select1wheret1.sal>=2500orm>=2500)
2)如果要修改S表中一位已经选过课的学生的学号(例如:
学号从1010101010改为1111111111),请给出具体的数据更新步骤。
--向S和SC表插入新纪录
insertintoSselect'1111111111',sn,sex,age,dnofromSwheresno='1010101010'
insertintoSCselect'1111111111',cno,scorefromSCwheresno='1010101010'
--从S和SC表删除旧记录:
deletefromSCwheresno='1010101010'
deletefromSwheresno='1010101010'