交互式SQL(4)—数据更新及完整性检查
三、实验内容及步骤(在实验报告中记录每个查询的SQL语句)
① 向表Students 中插入(0601001,赵林, 男,1985-09-08,计算机)的记录。
② 向SC 表中添加一个学生的选课记录,学号为0601001,所选的课程号为C2。 SC表中有Sno、Cno、Grade 这3 个列。这里只知道学号和课程号,不知道成绩值。
③ 向表Students 中插入(0601002,张修雨,default)记录,该记录的数据中default 表示默认值‘男’,其他数据为空值。
④ 用CREATE 语句创建表StudentBAK1,包含(与Students 的Sno、Sname、Sdept 相同)3 个字段, 然后用INSERT SELECT 语句实现向StudentBAK1 添加Students 表中的计算机系学生的学号、姓名、所在系的信息。
⑤ 用 Create Table … AS … 语句实现把Students 表中1986 年后(包含1986 年)出生的学生的学号、姓名存储到一个新表StudentBAK2。
⑥ 将Students表中姓名为【赵林】的同学所在系改为【机电】,爱好改为【足球】。
⑦ 将选修了课程名为【数据库原理】并且有成绩的学生成绩加5 分。
⑧ 将Test 数据库的StudentBAK1 表中所有姓赵的同学删除。
⑨ 删除机电系课程成绩不及格或者没有登记成绩的学生选课记录。
⑩ 将Test 数据库的StudentBAK2 表中的所有行删除。
#① 向表Students 中插入(0601001,赵林, 男,1985-09-08,计算机)的记录。
insert into student VALUES('0601001','赵林','男','1985-09-08','计算机',null);
#② 向SC 表中添加一个学生的选课记录,学号为0601001,所选的课程号为C2。 SC表中有Sno、Cno、Grade 这3 个列。这里只知道学号和课程号,不知道成绩值。
insert into sc(sno,cno) values('0601001','C2');
#③ 向表Students 中插入(0601002,张修雨,default)记录,该记录的数据中default 表示默认值‘男’,其他数据为空值。
insert into student VALUES('0601002','张修雨',default,null,null,null);
#④ 用CREATE 语句创建表StudentBAK1,包含(与Students 的Sno、Sname、Sdept 相同)3个字段, 然后用INSERT SELECT 语句实现向StudentBAK1 添加Students 表中的计算机系学生的学号、姓名、所在系的信息。
create table studentBAK1
( Sno char(7) PRIMARY key,
Sname varchar(20),
Sdep varchar(10)
);
INSERT into studentBAK1
select sno,sname,sdep
from student WHERE sdep='计算机';
#⑤ 用 Create Table … AS … 语句实现把Students 表中1986 年后(包含1986 年)出生的学生的学号、姓名存储到一个新表StudentBAK2。
Create Table StudentBAK2
AS select SNO,SNAME
from student
where year(sbirthday)>=1986;
#⑥ 将Students表中姓名为【赵林】的同学所在系改为【机电】,爱好改为【足球】。
update student
set sdep='机电',memo='足球'
where sname='赵林';
#⑦ 将选修了课程名为【数据库原理】并且有成绩的学生成绩加5 分。
update sc
set grade =grade+5
where GRade is not null and cno='c2';
#⑧ 将Test 数据库的StudentBAK1 表中所有姓赵的同学删除。
delete from studentBAK1
where studentbak1.Sname like '赵%';
#⑨ 删除机电系课程成绩不及格或者没有登记成绩的学生选课记录。
select *
from sc,student
where sc.sno=student.sno and sdep='机电';
delete sc
from sc,student
where sc.sno=student.sno and sdep='机电' and
(grade<60 or grade is null);
#⑩ 将Test 数据库的StudentBAK2 表中的所有行删除。
drop table StudentBAK2 ;