实验四 数据更新操作
一. 实验目的
1. 熟悉使用UPDATE/INSERT/DELETE语句进行表操作; 2. 能将这些更新操作应用于实际操作中去; 二. 实验要求
1. 完成下面的实验内容,并提交实验报告; 2. 在实验报告中附上相应的代码; 三. 实验内容
(1)向表(Class)中插入数据
ClsNO CS01 CS02 MT04 PH08 GL01 ClsName 计算机一班 计算机二班 数学四班 物理八班 地理一班 Director 张宁 王宁 陈晨 葛格 张四 Specialty 计算机应用 计算机应用 数学 物理 应用地理
插入数据之后使用命令:Select * from Class; 检查插入数据的正确性
(2) 向表(Student)中插入数据 Sno 20090101 20090102 20090306 19940107 Sname 王军 李杰 王彤 吴杪 Ssex ClsNO 男 男 女 女 CS01 CS01 MT04 PH08 Saddr 下关40# 江边路96# 中央路94# 莲化小区74# Sage 20 22 19 18 Height 1.76 1.72 1.65 1.60
插入数据之后使用命令:Select * from Student; 检查插入数据的正确性
(3)向表(Course )中插入数据 Cno 0001 0003 0007
Cname 高等数学 计算机基础 物理 Cpno Null 0001 0001 Credit 6 3 4 插入数据之后使用命令:Select * from Course; 检查插入数据的正确性
(4)向表(SC )中插入数据
SNO 20090101 20090101 20090102 20090102 20090306 20090306 19940107 20090306
(5) 对于student表,将所有班级号为‘CS01’的,并且年龄小于20岁的学生的班级
号改为‘CS02’。 (6) 对于student表,删掉所有年龄不小于20岁,并且专业号为‘CS02’的学生的记录。
对于student表,插入一条新记录,它的具体信息为,学号:20071101、姓名:张三、性别:男、年龄:19、班级编号:‘CS01’。 (7) 对于student表,将年龄最小的学生的家庭地址去掉。 (8) 对于student表,将平均年龄最小的一个班级编号改为‘GL01’
四、实验步骤:
1.向class表插入数据如下:
insert
into class(clsno,clsname,director,specialty) values('cs01','计算机一班','张宁','计算机应用'); insert
into class(clsno,clsname,director,specialty) values('cs02','计算机二班','王宁','计算机应用'); insert
into class(clsno,clsname,director,specialty) values('mt04','数学四班','陈晨','数学'); insert
into class(clsno,clsname,director,specialty) values('ph08','物理八班','葛格','物理'); insert
into class(clsno,clsname,director,specialty) values('gl01','地理一班','张四','应用地理');
CNO 0001 0007 0001 0003 0001 0003 0007 0007 Grade 90 86 87 76 87 93 85 90
2.向Student表插入数据如下:
insert
into student(sno,sname,scsex,clsno,saddr,sage,height)
values('20090101','王军','男','cs01','下关40#','20','1.76'); insert
into student(sno,sname,scsex,clsno,saddr,sage,height)
values('20090102','李杰','男','cs01','江边路96#','22','1.72'); insert
into student(sno,sname,scsex,clsno,saddr,sage,height)
values('20090306','王彤','女','mt04','中央路94#','19','1.65'); insert
into student(sno,sname,scsex,clsno,saddr,sage,height)
values('19940107','吴杪','女','ph08','莲化小区74#','18','1.60');
3. 向course表插入数据如下:
insert
into course(cno,cname,cpno,ccredit) values('0001','高等数学','null','6'); insert
into course(cno,cname,cpno,ccredit) values('0003','计算机基础','0001','3'); insert
into course(cno,cname,cpno,ccredit) values('0007','物理','0001','4');
4.向表SC 中插入数据
insert
into sc(sno,cno,grade)
values('20090101','0001','90'); insert
into sc(sno,cno,grade)
values('20090101','0007','86'); insert
into sc(sno,cno,grade)
values('20090102','0001','87'); insert
into sc(sno,cno,grade)
values('20090102','0003','76'); insert
into sc(sno,cno,grade)
values('20090306','0001','87'); insert
into sc(sno,cno,grade)
values('20090306','0003','93'); insert
into sc(sno,cno,grade)
values('19940107','0007','85'); insert
into sc(sno,cno,grade)
values('20090306','0007','90');
5.对于student表,将所有班级号为‘CS01’的,并且年龄不大于20岁的学生的班级号改为‘CS02’
update student set clsno = 'cs02'
where clsno = 'cs01' and sage<20;
6. 对于student表,删掉所有年龄不小于20岁,并且专业号为‘CS02’的学生的记录。
如果建表的时候没写级联删除,就在删除前先添加级联删除语句
delete from student
where clsno = 'cs02' and sage >= 20;
对于student表,插入一条新记录,它的具体信息为,学号:20071101、姓名:张三、性别:
男、年龄:19、班级编号:‘CS01’。
insert
into student(sno,sname,scsex,clsno,saddr,sage,height) values('20071101','张三','男','cs01',null,'19',null);
7.对于student表,将年龄最小的学生的家庭地址去掉。
update student set saddr = 'null' where sage in
(select min(sage) from student)
8.对于student表,将平均年龄最小的一个班级编号改为‘GL01’
update student set clsno = 'gl01' where sage <=
(select min(avg(sage)) from student
group by student.clsno);
9、 对于student表,将学号为19940107的学生的姓名该成“吴用”;
update student set sname = '吴用' where sno = '19940107';
10、对于sc表,将学号为20090101且的成绩为86的学生的成绩改为96
update sc set grade = '96'