数据库实验四-SQL语言-更新操作

一、实验目的
利用insert、 update和delete命令(或语句)实现对表(或视图)数据的添加、修改与删除等更新操作。
二、实验内容
①在学生表Student和学生选课表SC中分别添加表5-1和表5-2中的记录。
②备份Student表到TS中,并清空TS表。
③给IS系的学生开设7号课程,建立所有相应的选课记录,成绩暂定为60分。
④把年龄小于等于16岁的女生记录保存到表TS中。
⑤在表Student中检索每门课均不及格的学生学号、姓名、年龄、性别及所在系等信息,并把检索到的信息存人TS表中。
⑥将学号为“98011”的学生姓名改为刘华,年龄增加1岁。
⑦把选修了“数据库系统”课程而成绩不及格的学生的成绩全改为空值(NULL)。
⑧将Student的前4位学生的年龄均增加1岁
⑨学生王林在3号课程考试中作弊,该课成绩改为空值(NULL)。
⑩把成绩低于总平均成绩的女同学的成绩提高5%。
⑪在基本表SC中修改课程号为“2”号课程的成绩,若成绩小于等于80分时降低2%,若成绩大于80分时降低1%(用两个UPDATE语句实现)。
⑫利用“select into..."命令来备份Student、 SC、Course三表,备份表名自定。
⑬在基本表SC中删除尚无成绩的选课元组。
⑭把“钱横”同学的选课情况全部删去。
⑮能删除学号为“98005”的学生记录吗?如果一定要删除该记录,该如何操作?给出操作命令。
⑯删除姓“张”的学生的记录。
⑰清空Student与Course两表。
⑱如何又从备份表中恢复所有的三表?

三、实验结果

--1在学生表Student和学生选课表SC中分别添加表5-1和表5-2中的记录
insert into STUDENT values('99010','赵青江',18,'男','CS');
insert into STUDENT values('99011','张丽萍',19,'女','CH');
insert into STUDENT values('99012','陈景欢',20,'男','IS');
insert into STUDENT values('99013','陈婷婷',16,'女','PH');
insert into STUDENT values('99014','李军',16,'女','EH');

insert into sc values('99010','1',87);
insert into sc values('99010','2',null);
insert into sc values('99010','3',80);
insert into sc values('99010','4',87);
insert into sc values('99010','6',85);
insert into sc values('99011','1',52);
insert into sc values('99011','2',47);
insert into sc values('99011','3',53);
insert into sc values('99011','5',45);
insert into sc values('99012','1',84);
insert into sc values('99012','3',null);
insert into sc values('99012','4',67);
insert into sc values('99012','5',81);

--2备份Student表到TS中,并清空TS表。
select *
into ts
from student;

delete from ts;

--3给IS系的学生开设7号课程,建立所有相应的选课记录,成绩暂定为60分。
insert into sc
select sno,cno,60
from student,course
where sdept='IS'and cno='7';

--4把年龄小于等于16岁的女生记录保存到表TS中。
insert into ts
select *
from student
where sage<=16 and ssex='女';

--5在表Student中检索每门课均不及格的学生学号、姓名、年龄、性别及所在系等信息,并把检索到的信息存人TS表中。
insert into ts
select distinct x.sno,sname,sage,ssex,sdept
from student x ,sc
where x.sno=sc.sno
and x.sno in (
select sno
from sc
where grade<60);

--6将学号为“98011”的学生姓名改为刘华,年龄增加1岁。
update student
set sname='李华',sage=sage+1       
where sno='99011';

--7把选修了“数据库系统”课程而成绩不及格的学生的成绩全改为空值(NULL)。
update sc
set grade=null
where sno in
(
select sno
from course,sc
where course.cno=sc.cno and grade<60 and course.cno=1
);

--8将Student的前4位学生的年龄均增加1岁
update student
set sage=sage+1
where sno in
( select top 4 sno
from student
);

--9学生王林在3号课程考试中作弊,该课成绩改为空值(NULL)。
update sc
set grade=null
from student join sc
on STUDENT.sno=sc.sno
where sname='王林' and cno='3';

--10把成绩低于总平均成绩的女同学的成绩提高5%。
update sc
set grade=grade+grade*0.5
where sno in
(
select sc.sno
from sc join student
on sc.cno=student.sno
where ssex='女' and
grade<(
select avg(grade)
from sc)
);
select * from sc;

--11在基本表SC中修改课程号为“2”号课程的成绩,若成绩小于等于80分时降低2%,若成绩大于80分时降低1%(用两个UPDATE语句实现)。
update sc
set grade=grade-grade0.2
where cno='2' and grade<=80;
update sc
set grade=grade-grade
0.1
where cno='2' and grade>80;

--12利用“select into..."命令来备份Student、 SC、Course三表,备份表名自定。
select *into bfst from student;
select * into bfsc from sc;
select * into bfc from course;

--13在基本表SC中删除尚无成绩的选课元组。
(select sno,grade
from sc
where grade is NULL)
delete from sc

--14把“钱横”同学的选课情况全部删去。
delete from sc
where sno in(
select sno
from student
where sname='钱横');

--15能删除学号为“98005”的学生记录吗?如果一定要删除该记录,该如何操作?给出操作命令。
delete from sc where sno='98003' ;
delete from student where sno='98003';

--16删除姓“张”的学生的记录。
delete from sc
where sno in(
select sno
from student
where sname like '张%');

--17清空Student与Course两表。
delete from student;
delete from course;

select* from student;
select * from course;

--18如何又从备份表中恢复所有的三表?
drop table student;
drop table course;
 select* into student from bfst;
 select* into course from bfc; 
select* from student;
select * from course;

四、实验小结
在本次实验中,我们成功地对数据库进行了更新操作。我们首先了解了数据库更新的基本原理,包括插入、更新和删除数据。然后,我们使用SQL语句实现了这些操作。在实验过程中,我们遇到了一些挑战,如SQL语法错误和数据完整性问题。通过分析错误信息,我们及时修正了问题,并成功地完成了实验任务。这次实验丰富了我们的数据库操作经验,提高了我们使用SQL语句的能力。

posted @ 2024-06-18 14:59  冰露奇缘  阅读(113)  评论(0)    收藏  举报