南昌航空大学实验报告2016 年月日课程名称:数据库原理及应用实验名称:SQL-更新操作学号:姓名:同组人:指导教师评定:签名:实验目的:利用INSERT、UPDA TE和DELETE命令(或语句)实现对表(或试图)数据的添加、修改与删除等更新操作,这里主要介绍对表的操作。
实验内如与要求:建表和数据库的代码:Create database jxglUSE jxglGOCreate Table Student(Sno CHAR(5) not null primary key(Sno),Sname varchar(20),Sage smallint check(Sage>=15 AND Sage<=45),Ssex char(2) default'男'check(Ssex='男' OR Ssex='女' ),Sdept char(2));Create Table Course(Cno char(2)NOT NULL primary key(Cno),Cname VARCHAR(20),Cpno char(2),Ccredit SMALLINT);Create Table SC(Sno char(5) NOT NULL CONSTRAINT S_F FOREIGN KEY REFERENCES Student(Sno), Cno CHAR(2) NOT NULL,Grade smallint check ((Grade IS NULL)OR(Grade between 0 and 100)),Primary key(Sno,Cno),foreign key(Cno) references Course(Cno));insert into Student values('98001','钱横',18,'男','CS');insert into Student values('98002','王林',19,'女','CS');insert into Student values('98003','李民',20,'男','IS');insert into Student values('98004','赵三',16,'女','MA');insert into Course values('1','数据库系统','5',4);insert into Course values('2','数学分析',null,2);insert into Course values('3','信息系统导论','1',3);insert into Course values('4','操作系统_原理','6',3);insert into Course values('5','数据结构','7',4);insert into Course values('6','数据处理基础',null,4);insert into Course values('7','C语言','6',3);insert into SC values('98001','1',87);insert into SC values('98001','2',67);insert into SC values('98001','3',90);insert into SC values('98002','2',95);insert into SC values('98002','3',88);图:Student表:Course表:SC表:请实践以下命令式更新操作1、在学生表Student和学生选课表SC中分别添加表5-1和表5-2中的记录表5-1:表5-2:代码:InsertInto Student Values('99010','赵青江','18','男','CS'); InsertInto Student Values('99011','张丽萍','19','女','CH'); InsertInto Student Values('99012','陈景欢','20','男','IS'); InsertInto Student Values('99013','陈婷婷','16','女','PH'); InsertInto Student Values('99014','李军','16','女','EH'); InsertInto SC Values('99010','1','87');InsertInto SC Values('99010','2',null);InsertInto SC Values('99010','3','80');InsertInto SC Values('99010','4','87');InsertInto SC Values('99010','6','85');InsertInto SC Values('99011','1','52');InsertInto SC Values('99011','2','47');InsertInto SC Values('99011','3','53');InsertInto SC Values('99011','5','45');InsertInto SC Values('99012','1','84');InsertInto SC Values('99012','3',null);InsertInto SC Values('99012','4','67');InsertInto SC Values('99012','5','81');插入后:Student表:插入后:SC表:2、备份Student表到TS中并清空TS表。
代码:未清空前TS表:select*into TSfrom Student;清空后TS表:3、给IS系的学生开设7号课程,建立所相应的选课记录,成绩暂定为60分。
代码:表:insertinto SCselect Sno,Cno,60from Student,Coursewhere Sdept='IS'and Cno='7';4、把年龄小于等于16岁的女生计录保存到表TS中。
代码:图:insertinto TSselect*from Studentwhere Sage<=16 and Ssex='女';5、在表Student中检索每门课均不及格的学生学号、年龄、性别及所在系等信息。
代码:图:select sno,sname,sage,sdeptfrom studentwhere sno in(select Snofrom SCwhere Grade<60groupby Snohaving count(sno)=count(cno));6、将学号“99011”的学生姓名改为刘华,年龄增加一岁。
代码:修改前:update Studentset Sname='李华',Sage=Sage+1where Sno='99011'; 修改后:7、把选修了“数据库系统”课程而成绩不及格的学生成绩全改为空值。
代码:图:update SCset Grade=NULLwhere Sno in(select Snofrom SCwhere Grade<60 and Cno=(select Cnofrom Coursewhere Cname='数据库系统'))8、将Student的前四位学生的年龄均增加1岁。
代码:更改前:更改后:update Studentset Sage=Sage+1where Sno in(selecttop 4 Snofrom student)9、学生王林在3课程中作弊,该课程成绩改为空值。
代码:更改前:更改后:update SCset Grade=NULLwhere Sno=(select Snofrom Studentwhere Sname='王林')and Cno= 3;10、把成绩低于总平均成绩的女同学的成绩提高5%。
代码:更改前:更改后:update SCset Grade=Grade+Grade*0.05where Sno in(select Snofrom SCwhere Grade<(select Avg(Grade)from SC)and Sno in(select Snofrom Studentwhere Ssex='女'))11、在基本表中SC中修改课程号为“2”号课程的成绩,若成绩小于等于80分时降低2%若成绩大于80分时降低1%(用两个UPDATE语句实现)。