数据库系统概论基础SQL语句整理(题目+代码+运行截图)

建表:

create table Student(
    sno char(20) primary key,
	sname char(20),
	ssex char(5),
	sage int,
	sdept char(10),
	S_entrance char(20)
	)
create table Course(
    cno char(10)primary key,
	cname char(20),
	cpno char(10),
	ccredit char(10))
create table sc(
    sno char(20) foreign key references student(sno),
	cno char(10) foreign key references course(cno),
	grade int )

(一)数据操作

  1. 插入数据

 1)在 Student 表中插入下列数据:

   200215121,李勇,男,20,CS

   200215122,刘晨,女,19,CS

   200215123,王敏。女,18,MA

   200215125,张立,男,19,IS     结果如下图

insert into student values('200215121','李勇','男',20,'cs',null)
insert into student values('200215122','刘勇','女',19,'cs',null)
insert into student values('200215123','王敏','女',18,'ma',null)
insert into student values('200215125','张立','男',19,'is',null)

2)在 Course 表中插入以下数据:

1,数据库,5,4        

2,数学,null,2

6,数据处理,null,2

4,操作系统,6,3

7,PASCAL 语言,6,4

5,数据结构,7,4

3,信息系统,1,4

insert into course values('1','数据库','5','4')
insert into course values('2','数学',null,'2')
insert into course values('6','数据处理',null,'2')
insert into course values('4','操作系统','6','3')
insert into course values('7','PASCAL语言','6','4')
insert into course values('5','数据结构','7','4')
insert into course values('3','信息系统','1','4')

3) 在 SC 表中插入以下数据:

200215121,1,92            

200215121,2,85

200215121,3,88

200215122,2,90

200215122,3,80

insert into sc values('200215121','1',92)
insert into sc values('200215121','2',85)
insert into sc values('200215121','3',88)
insert into sc values('200215122','2',90)
insert into sc values('200215122','3',80)

4)将一个新学生元祖(学号:200215128;姓名:陈冬;性别:男;所在系:IS;年龄:18 岁)插入到 Student 表中。

insert into student values('200215128','陈冬','男',18,'is',null)

5)将学生张成民的信息插入到 Student 表中

insert into student values('200215126','张成民','男',18,'cs',null)

6)插入一条选课记录:(‘200215128’,‘1’)

insert into sc values('200215128','1',null)

7)对每一个系,求学生的平均年龄,并把结果存入数据库

create table Dept_age(
sdept char(10),
Avg_age int)

insert into Dept_age(sdept,Avg_age)
select sdept,avg(sage)
from student
group by sdept

2.修改数据 
1)将学生 200215121 的年龄改为 22 岁

update student 
set sage = '22'
where sno = '200215121'

2)将所有学生的年龄增加一岁

update student 
set sage = sage+1

3)将计算机科学系全体学生的成绩置零

update sc
set grade = '0'
where sno in (
    select sno 
    from student 
    where sdept = 'cs')


3.删除数据 
1)删除学号为 200215128 的学生记录 

delete 
from student 
where sno = '200215128'

2)删除所有学生的选课记录

delete 
from sc

二.查询操作 
1.单表查询 
1)查询全体学生的学号与姓名    

select sno,sname from student


2) 查询全体学生的姓名、学号、所在系

select sno,sname,sdept from student


3)查询全体学生的详细记录 

select * from student


4) 查询年龄不在 20-23 岁(包括 20 岁和 23 岁)之间的学生的姓名、系别和年龄


select sname , sdept , sage
from student
where sage not between 20 and 23


5)查询计算机科学系(CS)、数学系(MA)、和信息系(IS)学生的姓名和性别

select sname,ssex
from student
where sdept in ('cs','ma','is')


6)查询学号为 200215121 的学生的详细情况 

select * from student 
where sno = '200215121'


7)某些学生选修课程后没有参加考试,所以有选课记录,但没有考试成绩。查询缺少成绩的学生的学号和相应的课程号

select sno,cno
from sc where grade is null


8)查询所有有成绩的学生学号和课程号

select sno,cno
from sc
where grade is not null


9)查询选修了 3 号课程的学生的学号及其成绩,查询结果按分数的降序排列

select sno,grade
from sc
where cno = '3'
order by grade desc


10)计算 1 号课程的学生平均成绩 

select avg(grade)
from sc
where cno = '1'

2.连接查询 
1)查询每个学生及其选修课程的情况

select student.*,sc.*
from student ,sc
where student.sno = sc.sno 


2)对上个题用自然连接完成

select student.sno,sname,ssex,sdept,S_entrance,sc.sno,sc.cno,grade
from student ,sc
where student.sno = sc.sno 


3)查询每一门课的间接先修课(即先修课的先修课)

select a.cno,b.cpno
from course a,course b
where a.cpno = b.cno


4)查询每个学生及其选修课程的情况,用外连接来完成

select student.sno,sname,ssex,sdept,cno,grade
from student left outer join sc on (student.sno = sc.sno)


5)查询选修 2 号课程且成绩在 90 分以上的所有学生

select student.sno,sname
from student ,sc
where cno = '2' and grade>90 and sc.sno = student.sno


6)查询每个学生的学号、姓名、选修的课程名及成绩  

select student.sno,sname,cno,grade
from student,sc
where student.sno = sc.sno

3.嵌套查询 
1)查询与“刘晨”在同一个系学习的学生

select sno,sname,sdept
from student
where sdept in  (select sdept from student where sname = '刘晨')


2)查询选修了课程名为“信息系统”的学生学号和姓名

select sno,sname
from student 
where sno in (select sno from sc,course where sc.cno = course.cno and cname = '信息系统')


3)找出每个学生超过他选修课程平均成绩的课程号

select cno
from sc a
where grade >= (select avg(grade) from sc b where a.sno = b.sno)


4)查询其他系中比计算机科学系某一学生年龄小的学生姓名和年龄

select sname, sage
from student 
where sage < any (select sage from student where sdept = 'cs')
and sdept != 'cs'


5)查询其他系中比计算机科学系所有学生年龄小的学生姓名和年龄

select sname, sage
from student 
where sage < all (select sage from student where sdept = 'cs')
and sdept != 'cs'


6)查询选修了 1 号课程的学生姓名

select sname
from student,sc
where student.sno = sc.sno and cno = '1'


7)查询没有选修 1 号课程的学生姓名

select sname
from student,sc
where student.sno = sc.sno and cno != '1'


8)查询选修了全部课程的学生姓名

select sname
from student 
where not exists(
    select *
	from course
	where not exists(
	    select *
		from sc
		where sc.sno = student .sno and sc.cno = course.cno))


9)查询至少选修了学生 200215121 选修的全部课程的学生号码 

select distinct sno
from sc a
where not exists(
    select * 
	from sc b
	where b.sno = '200215121' and not exists(
	    select * 
		from sc c
		where c.sno = a.sno and c.cno = b.cno))

 4.集合查询 
1)查询计算机科学系的学生及年龄不大于 19 岁的学生

select * from student where sdept = 'cs'
union
select * from student where sage <= 19


2)查询选修了课程 1 或课程 2 的学生

select sno from sc where cno = '1'
union 
select sno from sc where cno = '2'


3)查询计算机科学系的学生与年龄不大于 19 岁的学生的交集

select * from student where sdept = 'cs'
intersect
select * from student where sage <=19


4) 查询既选修了课程 1 又选修了课程 2 的学生。就是查询选修课程 1的学生集合与选修课程 2 的学生集合的交集

select * from sc where cno = '1'
intersect
select * from sc where cno = '2'


5)查询计算机科学系的学生与年龄不大于 19 岁的学生的差集

select *
from student
where sdept = 'cs'
except
select * from student 
where sage <= 19

 三、视图操作 
1.建立视图 
1)建立信息系选修了 1 号课程的学生的视图

create view is_si(sno,sname,grade)
as
select student.sno,sname,grade
from student,sc
where sdept = 'is'
and student.sno = sc.sno
and sc.cno = '1'


2)定义一个反应学生出生年份的视图

create view bt_s(sno,sname,sbirth)
as
select sno,sname,2014-sage
from student


3)将学生的学号及他的平均成绩定义为一个视图

create view s_g(sno,gavg)
as
select sno,avg(grade)
from sc
group by sno

2.查询视图: 
1)在信息系学生的视图中找出年龄小于 20 岁的学生

select sno,sage
from is_student
where sage < 20


2)查询选修了 1 号课程的信息系学生

select is_student.sno,sname
from is_student,sc
where is_student.sno = sc.sno and sc.cno = '1'


3)在 S_G 视图中查询平均成绩在 90 分以上的学生学号和平均成绩

select *
from s_g
where gavg >= 90


3.更新视图: 
1)将信息系学生视图 IS_Student 中学号为 200215122 的学生姓名改为“刘辰”

update is_student
set sname = '刘辰'
where sno = '200215122


2)向信息系学生视图 IS_Student 中插入一个新的学生记录,其中学号为 200215129,姓名为赵新,年龄为 20 岁 

insert 
into is_student
values ('200212129','赵新','20')


3)删除信息系学生视图 IS_Student 中学号为 200215129 的记录

delete
from is_student
where sno = '200215129'

猜你喜欢

转载自blog.csdn.net/red_red_red/article/details/89680508
今日推荐