文章目录
- 1.实验目的
- 2.实验内容
- 3.实验环境
- 4.实验步骤及结果
1.实验目的
通过本次实验使学生掌握SQL数据更新操作。
2.实验内容
- 数据插入
- 数据修改
- 数据删除
3.实验环境
- Windows
- SQL Server
4.实验步骤及结果
- 创建一个数据库,文件名为“教学”
- 打开“教学”数据库
Student 表
S# | Sname | Age | Ssex |
---|---|---|---|
S1 | WANG | 15 | 男 |
S2 | LI | 17 | 女 |
S3 | LU | 19 | 女 |
S4 | ZHAO | 13 | 女 |
S5 | YANG | 20 | 男 |
Course表
C# | Cname | T# |
---|---|---|
C1 | Maths | T1 |
C2 | DB | T2 |
C3 | English | T3 |
C4 | Computer | T2 |
SC表
S# | C# | Score |
---|---|---|
S1 | C1 | 50 |
S2 | C1 | 70 |
S3 | C1 | 80 |
S4 | C1 | 75 |
S5 | C1 | 87 |
S1 | C2 | |
S2 | C2 | |
S4 | C2 | 85 |
S1 | C3 | |
S5 | C4 | 60 |
S4 | C4 | 45 |
Title表
T# | Tname | Title |
---|---|---|
T1 | 吴恩达 | 教师 |
T2 | 刘晓庆 | 教授 |
T3 | 张龙 | 副教授 |
注明:表格和下面的代码可能不会一一对应,可能需要增加,删除,更改表中的数据
- 输入如下数据:
1、往关系course中插入一个课程元组
Insert
into course(C#,cname,T#)
values ('C5','COMPUTER','t3')
go
2) 在sc中删除尚无成绩的选课元组
Delete
from sc
where score is null
go
3) 把选修刘晓庆老师课程的女同学的选课元组全部删去
Delete
from SC
where c#
in (select c#
from course,title
where course.t#=title.t# and tname='刘晓庆')and
s# in(select s#
from student
where sex='女')
go
4) 统计每个学生的平均成绩并存到平均成绩表中。
Create Table avgescore
(s# char(9) primary key,
chengji smallint)
go
创建表和插入数据不能同时进行
Insert into avgescore
select student.s#,avg(score) 成绩
from student,sc
where student.s#=sc.s#
group by student.s#;
go
insert如果是查询不需要加 values
5) 把maths不及格的成绩全改为 60分
Update sc
set score=60
where score<60 and c# in (
select c#
from course where Cname='maths')
go
6) 把低于所有课程总平均成绩的女同学成绩提5%
Update SC
set score=score*1.05
where s# in(
Select s#
from Student
where sex='女')
and score<(
Select AVG(score)
From SC);
go
7) 删除所有女同学的选课信息。
Delete
from sc
where s# in
(select s#
from Student
where sex='女'and c# is not null)
go
8) 一次往Title表里加入3名老师信息
Insert
into title(t#,tname,title)
values('t5','王天一','教授'),
('t6','张萌','助教'),
('t7','林莉','助手')
go
9)将教师号为T3的教师姓名改为刘凯,职称改为实验员
Update title
Set tname = '刘凯', title = '实验员'
Where t# = 't3'
go