以SQL Server软件为例;
0.查询student表里的属性
select *
from student;
1.将一个新学生记录
(学号:95020;姓名:陈冬;性别:男;所在系:IS;年龄:18岁)插入到Student表中。)
insert
into student(sno,sname,ssex,sdept,sage)
values ('95020','陈冬','男','IS',18);
2.将19级所有学生的年龄增加1岁。
update student
set sage = sage+1
where sno like '2019%';
3.将非软件系全体学生的成绩置零。
备注:先查询非软件系全体学生的成绩,更新完再查询一遍
update sc
set score=0
where sno in (select sno from student where sclass not like '软件工程');
4. 删除2号课程的所有选课记录。
备注:先查询一次,删除后再查询一次 ()删除2号课程的所有选课记录。
select *
from sc
where cno='c2';
delete
from sc
where cno='c2';
5.删除181软件所有学生的选课记录。
备注:先查询一次,删除后再查询一次
select *
from sc
where sno in
( select sno
from student
where sclass = '软件工程181';
);
delete
from sc
where sno in(select sno from student where sclass = '软件工程181');
6.建立本班级学生的视图。
备注:查询本班学生信息,创建视图,然后做视图查询视图创建
create view Sclass_student AS
select sno, sname,ssex,sage,sdept,sclass,sbirth,stime
from student
where sclass = '软件工程212';
select *
from Sclass_student;
7.将学生的学号及他的平均成绩定义一个视图。
备注:创建视图,然后做视图查询
create view S_A(sno,score)
as
select sno,avg(score)
from sc
group by sno;
select *
from S_A;
8.在上题的视图中查询平均成绩在90分以上的学生的学号和平均成绩。
select sno,avg(score)
from S_A
group by sno
having avg(score)>90;
9.在软件系学生的视图中找出年龄小于20岁的学生。
备注:创建软件系学生的视图,然后在视图中查询年龄小于20岁的学生
create view ha
as
select *
from student
where sdept like '软件%';
select *
from ha
where sage<20;
10.将软件系20级学生视图在IS_STUDENT中学号为20202218(可以自己给定一个学号)的学生姓名改为刘辰。
create view IS_STUDENT
as
select *
from student
where sclass like '软件%20%';
update IS_STUDENT
set sname = '刘辰'
where sno = '20202218';
11.删除上题创建的视图
delete IS_STUDENT;