数据库实验第三次

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

南昌航空大学实验报告
2016 年月日
课程名称:数据库原理及应用实验名称: SQL-更新操作
学号:姓名:同组人:
指导教师评定:签名:
实验目的:
利用INSERT、UPDATE和DELETE命令(或语句)实现对表(或试图)数据的添加、修改与删除等更新操作,这里主要介绍对表的操作。

实验内如与要求:
建表和数据库的代码:
Create database jxgl
USE jxgl
GO
Create 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:
代码:
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');
插入后:Student表:
插入后:SC表:
2、备份Student表到TS中并清空TS表。

代码:未清空前TS表:
select*
into TS
from Student;
清空后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中检索每门课均不及格的学生学号、年龄、性别及所在系等信息。

代码:图:
select sno,sname,sage,sdept
from student
where sno in
(
select Sno
from SC
where Grade<60
group by Sno
having count(sno)=count(cno));
6、将学号“99011”的学生姓名改为刘华,年龄增加一岁。

代码:修改前:
update Student
set Sname='李华',Sage=Sage+1
where Sno='99011'; 修改后:
7、把选修了“数据库系统”课程而成绩不及格的学生成绩全改为空值。

代码:图:update SC
set Grade=NULL
where Sno in
(
select Sno
from SC
where Grade<60 and Cno=
(
select Cno
from Course
where Cname='数据库系统'
))
8、将Student的前四位学生的年龄均增加1岁。

代码:更改前:更改后:update Student
set Sage=Sage+1
where Sno in
(
select top 4 Sno
from student)
9、学生王林在3课程中作弊,该课程成绩改为空值。

代码:更改前:更改后:update SC
set Grade=NULL
where Sno=
from Student
where Sname='王林')
and Cno= 3;
10、把成绩低于总平均成绩的女同学的成绩提高5%。

代码:更改前:更改后:
update SC
set Grade=Grade+Grade*
where Sno in
(select Sno
from SC
where Grade<(
select Avg(Grade)
from SC)and Sno in
(
select Sno
from Student
where Ssex='女'
))
11、在基本表中SC中修改课程号为“2”号课程的成绩,若成绩小于等于80分时降低2%若成绩大于80
分时降低1%(用两个UPDATE语句实现)。

代码:更改前:更改后:update SC
set Grade=Grade-Grade*
where Grade<80 and Cno='2'
set Grade=Grade-Grade*
where Grade>80 and Cno='2'
12、利用“select into…”命令来备份Student、SC、Course三表,备份表名自定。

代码:更改前:更改后:
select*into TStudent from Student
select*into TSC from SC
select*into TCourse from Course;
13、在基本表SC中删除尚无成绩的选课元组。

代码:更改前:(select Sno,Grade from SC where Grade is NULL) delete
from SC
where Grade is NULL;
14、把‘钱横‘同学的选课情况全部删除、
代码:更改前:更改后:
delete
from SC
where Sno=
(
select Sno
from Student
where Sname='钱横')
15、能删除学号为“98003”的学记录吗如果一定要删除该记录,该如何操作给出操作命令。

不能,图:
先删除SC表中有关98003学生的信息:delete from SC where Sno='98003'
再删除Student表中的记录:图:
代码:delete from Student where Sno='98003'
16、删除姓‘张‘的学生的记录。

代码:更改前:(查询姓张同学信息)更改后:
select*
from Student
where Sname like'张%'
17、清空Student与Course两表。

代码:更改后两表:
具体操作:
先删除sc表消除外键约束
再删除sc表
将TSC表中的数据插入到sc表中
delete SC
delete Student
delete Course
drop table SC
select
into SC
from TSC;
14、如何又从备份表中恢复所有的三张表。

代码:
drop table Student
drop table Course
select*
into Student
from TStudent
select*
into Course
from TCourse
更改后SC表:更改后Student表:。

相关文档
最新文档