熟练掌握建立数据库和表,向数据库输入数据、修改数据、删除数据的操作。
建立数据库并设计各表,输入多条实际数据,并实现数据的增、删、改操作。
创建用于学生管理数据库,数据库名为StudentCourse,包含学生的基本信息,课程信息和选课信息。数据库StudentCourse包含下列3个表:
(l) student:学生基本信息。
(2) course:课程信息表。
(3) sc:学生选课表。
各表的结构分别如表1、表2和表3所示。
表1 学生信息表:student
列名 |
数据类型 |
长度 |
是否允许为空值 |
sno |
字符型 |
10 |
否 |
sname |
字符型 |
10 |
否 |
ssex |
字符型 |
2 |
否 |
sage |
整数型 |
是 |
|
sdept |
字符型 |
4 |
否 |
表2 课程信息表:course
列名 |
数据类型 |
长度 |
是否允许为空值 |
cno |
字符型 |
3 |
否 |
cname |
字符型 |
30 |
否 |
credit |
整数型 |
是 |
|
pcno |
字符型 |
3 |
是 |
表3 学生选课表:sc
列名 |
数据类型 |
长度 |
是否允许为空值 |
sno |
字符型 |
10 |
否 |
cno |
字符型 |
3 |
否 |
grade |
整数型 |
是 |
在SQLSERVER中用企业管理器-新建数据库;
在SQL SERVER中用数据库—StudentCourse—表—右键—新建表;
在SQL SERVER中查询分析器的编辑窗口中用SQL语句建立。
use XSGL
Create table student(sno CHAR(5),sname CHAR(10),ssex CHAR(2),sage int,sdept CHAR(4))
Create table course(cno CHAR(2),cname CHAR(30),credit INT,pcno CHAR(2) NULL)
Create table sc(sno CHAR(5),cno CHAR(2),grade INT ULL)
1. 用可视化方法:
I: 在SQL SERVER中用数据库—StudentCourse—表-表名—右键-打开表-编辑前200行;
输入下列数据:
sno |
sname |
ssex |
sage |
sdept |
95001 |
李勇 |
男 |
20 |
CS |
95002 |
刘晨 |
女 |
19 |
IS |
95003 |
王敏 |
女 |
18 |
MA |
95004 |
张立 |
男 |
19 |
IS |
95005 |
刘云 |
女 |
18 |
CS |
cno |
cname |
credit |
pcno |
1 |
数据库 |
4 |
5 |
2 |
数学 |
6 |
|
3 |
信息系统 |
3 |
1 |
4 |
操作系统 |
4 |
6 |
5 |
数据结构 |
4 |
7 |
6 |
数据处理 |
3 |
|
7 |
PASCAL语言 |
4 |
6 |
sno |
cno |
grade |
95001 |
1 |
92 |
95001 |
2 |
85 |
95001 |
3 |
88 |
95002 |
2 |
90 |
95002 |
3 |
80 |
95003 |
2 |
85 |
95004 |
1 |
58 |
95004 |
2 |
85 |
2. 在SQL SERVER查询分析器的编辑窗口中使用SQL语句插入数据。
Insert into student(sno,sname, ssex,sage,sdept) values('95001', '李勇', '男', 20, 'CS')
1. 用可视化方法:
在SQL SERVER中用企业管理器—数据库—StudentCourse—表-表名—右键-编辑前200行;
在表格中将相应的数据修改即可。
2.SQL语句命令方法:
将所有学生的年龄增加一岁: update student set sage=sage+1
将4号课程的学分改为4 update course set credit=4 where cno=4
设置7号课程没有先行课 update course set pcno=null where cno=7
将95001号学生的1号课程的成绩增加3分
update sc set grade=grade+3 where sno=’95001’ and cno=’1’
1. 用可视化方法:
在SQL SERVER中用数据库—StudentCourse—表-表名—右键-编辑前200行;
单击左边的行标记, 选定某一行, 或单击后拖动选择相邻的多行, 再右击鼠标选择弹出式菜单中的删除。
2.SQL语句命令方法:
删除学号为95005的学生的记录:delete from student where sno=’95005
删除所有的课程记录: delete from course
删除成绩为不及格(少于60分)的学生的选课记录:
delete from sc where grade<60
实验三:数据库的SQL基本操作
(1) 查询全体学生的学号和姓名
select sno,sname
from student
(2) 查询选修了课程名为’数据库原理’ 的学生的学号和姓名
select student.sno,sname
from student,sc,course
where
sc.cno=course.cno
and student.sno=sc.sno
and course.cname='数据结构'
(3) 查询全体学生的姓名, 出生年份,和所在系, 并用小写字母表示所有系名,并给各列指定列名。
select sno,2017-sage BIRTHDAY,Lower(sdept) sdept
from student
(4) 查询有多少名学生的数据库课程成绩不及格
select (sum)sno
from student,sc,course
where grade<=60 and student.sno=sc.sno
and course.cno=sc.cno and cname=’数据结构’
(5) 查找所有姓’李’的学生的姓名, 学号和性别
select sname,ssex,sno
from student
where sname LIKE ‘李%’
(6) 求没有选修数学课程的学生学号
select sno
from SC
WHERE cno is Null
(7) 查询选修了课程的学生的学号
select Distinct sno
from SC
WHERE cno is not null
(8) 计算1号课程的学生的平均成绩, 最高分和最低分
select AVG(grade),max(grade),min(grade)
from SC
WHERE sno=95001
(9) 查询数学系和信息系的学生的信息;
select*
from student
where
sdept='MA'Or sdept='CS'
(10) 将年龄为19岁的学生的成绩置零
UPDATE sc
set grade=0
where sno in
(select sno
from student
where sage=19)
(11) 查询所有选修了1号课程的学生姓名
select sname
from student,sc
where student.sno=sc.sno and cno=1
(12) 对每一个性别,求学生的平均年龄,并把结果存入数据库
先创建表,再插入数据
create table dept_age
(sdept CHAR(15),
avg_age smallint);
insert
into dept_age(sdept,avg_age)
select sdept,AVG(sage)
from student
group by sdept
(13) 查询每个学生已获得的学分
select sno,sum(cridit)
from sc,course
where grade>=60 and sc.cno=course.cno
group by sno
(14) 将所有女生的记录定义为一个视图
create view gril
as
select*
from student
where ssex='女'
(15) 查询没有选修了1号课程的学生姓名
select sname
from student
where not exists
(select*
from sc
where sno=student.sno and cno=1)
(16) 将所有选修了数据库课程的学生的成绩加5分
update sc
set grade=grade+5
where cno in
(select cno
from course
where cname='数据库')
(17) 查询各系的男女生学生总数, 并按系别,升序排列, 女生排在前
select sdept,ssex,count(*)as 人数
from student
group by sdept,ssex order by sdept,ssex desc
(18) 查询’信息系’(IS)学生”数据结构”课程的平均成绩
select avg(grade)
from student,course,sc
where student.sno=sc.sno
and course.cno=sc.cno
and sdept='IS'and cname='数据结构'
(19) 创建一个反映学生出生年份的视图
create view birthday(sno,sname,sbirth)
as
select sno,sname,2014-sage
from student;
(20) 查询与’王田’在同一个系学习的学生的信息
select*
from student
where sdept in
(select sdept
from student
where sname='王田')
(21) 查询年龄在20岁以下的学生的姓名及其年龄
select sname,sage
from student
where sage<20
(22) 查询当前至少选修数据库和信息系统其中一门课的学生的学号
select sno
from course,sc
where cname='数据库'
and course.cno=sc.cno
union
select sno
from course,sc
where cname='信息系统'
and course.cno=sc.cno
(23) 查询每个学生的学号, 姓名, 选修的课程名和成绩:
select student.sno,sname,cname,grade
from student,sc,course
where student.sno=sc.sno and sc.cno=course.cno
(24) 查找名字中包括“俊”的学生的姓名, 学号,选课课程和成绩
select*
from student
where sname LIKE '%俊%'
(25) 查询学分大于8的学生,输出学生的学号和学分
select sno,sum(cridit)
from sc,course
where sc.cno=course.cno
group by sno
having sum(cridit)>8
(26) 查询IS,CS,MA系的所有学生的姓名和性别
select sno,sname
from student
where sdept='IS'or sdept='CS'OR sdept='MA'
(27) 查询至少选修了2门课程的学生的平均成绩
select sno
from sc
group by sno
having count(*)>2
(28) 查询每个学生所选课程的平均成绩, 最高分, 最低分,和选课门数
select sno,avg(grade),max(grade),min(grade),count(*)
from sc
group by sno
(29) 删除年龄大于21岁所有学生的选课记录
delete
from student
where sage>21
(30) 查询没有先行课的课程的课程号cno和课程名cname
select cno,cname
from course
where pcno is NULL
(31) 创建信息系学生信息的视图
create view IS_student
as
select sno,sname,ssex,sage,sdept
from student
where sdept='IS'
(32) 在信息系的学生视图中查询年龄小于20岁的学生
select*
from IS_student
where sage<20
(33) 删除马朝阳同学的所有选课记录
delete
from student
where sname='马朝阳'
(34) 查询选修了3号课程的学生的学号和成绩, 并按分数降序排列
select student.sno,grade
from student,sc
where student.sno=sc.sno and
cno='3'
order by grade desc;
(35) 查询数据库课程成绩不及格的学生,输入其学号,姓名和成绩
select student.sno,sname,grade
from student,sc,course
where student.sno=sc.sno and
grade<60and
course.cno=sc.cno and cname='数据库'
(36) 查询全体学生的情况,查询结果按所在系号升序排列, 同一系中的学生按年龄降序排列
select*
from student
order by sdept ASC,sage DESC
(37) 查询每个学生及其选修课程的情况
select student.sno,cname,grade
from student,sc,course
where student.sno=sc.sno
and course.cno=sc.cno
order by sno
(38) 查询每一门课程的间接先行课
select cno,pcno
from course
(39) 查询选修1号课程且成绩在85分以上的所有学生的学号、姓名
select sc.sno,sname
from sc,student
where grade>85 and
sc.sno=student.sno
and cno='1'
(40) 查询全体学生的所有信息
select student.sno,sname,ssex,sdept,cno,grade
from student left outer join sc on(student.sno=sc.sno)
(41) 查询选修了课程’1’和课程’2’的学生的学号
select sc.sno
from sc,student
where student.sno=sc.sno
and cno='1'or cno='2'
group by sc.sno
(42) 创建信息系选修了1号课程的学生的视图
Create view is_s1(sno,sname,grade)
As
Select student.sno,sname,grade
From student,sc
Where sdept=’Is’
Student.sno=sc.sno
And sc.cno=’1’
(43) 建立信息系选修了1号课程且成绩在90分以上的学生的视图
Create view Is_s2
As
Select sno,sname,grade
From is_s1
Where grade>=90
(44) 查询修课总学分在10学分以下的学生姓名
Select sname
From student,sc,course
Where sc.cno=course.cno and student.sno=sc.sno
Group by sname
Having sum(credit<10)
(45) 查询比’刘晨’年龄小的所有学生的信息
Select*
From student
Where sage<
(select sage from student where sname=’刘晨’)
(46) 查询所有选修了2号课程的学生的姓名
Select sname from student where exsits
(select*
From sc
Where sno=student.sno and cno=’1’
(47) 查询其他系中比信息系(IS)某一学生年龄小的学生姓名和年龄
Select sname sage
From student
Where sage
From student
Where sdept=’IS’)
AND sdept<>’IS’
(48) 查询学生2号课程的成绩,并按照成绩由高到低输出
Select sno,grade
From sc
Where sno=’95002’
Order by grade desc
(49) 查询考试成绩有不及格的学生的学号
Select distinct sno
From sc;
Where grade<60
(50) 查询其他系中比信息系(IS)学生年龄都小的学生姓名和年龄
Select sname sage
From student
Where sage
From student
Where sdept=’IS’)
AND sdept<>’IS’
(51) 将所有学生的学号和他的平均成绩定义为一个视图
Creat view s_G(sno,gavg)
As
Select sno,avg(grade)
From sc
Group by sno;
(52) 在视图S_G中查询平均成绩在90分以上的学生的学号和平均成绩:
Select*
From s_g
Where Gavg>=90
(53) 查询与计算机系(CS)系所有学生的年龄均不同的学生学号, 姓名和年龄
Select sname sage
From student
Where sage<>all(select sage
From student
Where sdept=’CS’)
AND sdept<>’CS’
(54) 查询信息系选修了1号课程的学生
Select*
From sc,student
Where cno=’1’ and sdept=’IS’
And sc.sno=student.sno
(55) 查询与其他所有学生年龄均不同的学生学号, 姓名和年龄
Select sno,sname sage
From student A
Where not exists
(select * from student B where A.sage=B.sage and A.sno<>B.sno)
(56) 查询选修了全部课程的学生姓名
Select sname from student where not exists
(select *from course where not exist
(select * from sc where sno=student.sno=student.sno and cno=course.cno))
(57) 求没有人选修的课程号cno和cname
Select cno,cname
From course c
Where not exists
(select * from sc
Where sc.cno=c.cno)
(58) 查询满足条件的(sno,cno)对, 其中该学号的学生没有选修该课程号cno的课程
Select sno,cno
From student,course
Where not exists
(select * from sc
Where cno=course.cno and sno=student.sno)
(59) 查询每个学生的课程成绩最高的成绩信息(sno,cno,grade)
Select *
From grade A
Where grade =(select max(grade)
From sc
Where cno=A.cno)
(60) 查询学生总人数
Select count(*)
As 学生总数
From student
(61) 查询年龄在20-30岁直接的学生的姓名, 姓名, 所在系
Select sname,sage,sdept
From student
Where sage<=30 and sage>=20
(62) 查询所有课程的总学分数和平均学分数,以及最高学分和最低学分
Select sum(credit) as总credit,avg(credit) as 课程平均分,max(credit) as 最高学分,min(credit) as 最低学分
From sc
Where cno cno=‘1’
(64) 求成绩低于该门课程平均成绩的学生的成绩信息(sno,cno,grade)
Select sno,cno,grade
From grade A
Where grade=(select avg(grade)
From sc
Where cno=A.cno)
(65) 查询各系的学生的人数并按人数从多到少排序
Select sdept,count(*)as 人数
From student
Group by sdept
order by 人数 desc
(66) 创建年龄大于23岁的学生的视图
Create view s_age
As
Select *
From student
Where sage>23
(67) 查询选修了课程的学生总数
SELECT COUNT(*)
FROM S
WHERE EXISTS( SELECT * FROM SC WHERE SC.SNO = S.SNO )
(68) 查询选修了3门课程以上的学生的学号和姓名
Select sno,sname
From student
Where sno in(select sno from sc group by(sno) having count(*)>3)
(69) 查询平均分超过80分的学生的学号和平均分
Select sno,avg(grade) as 平均成绩
From sc
group by sno
having avg(*)>=80
比较: 求各学生的60分以上课程的平均分
Select sno,avg(grade) as 平均成绩
From sc
Where >=60
Group by sno
(70)查询”信息系”(IS)中选修了2门课程以上的学生的学号
Select sno
From sc
Where sno in(select sno from student where sdept =’IS’)
Group by sno
Having count(*)>=2
(71) 查询选修了1号课程或2号课程的学生的学号
Select sno from sc where cno=’1’
Uoion
Select sno from sc where cno=’2’
(72) 查询平均成绩少于70分的学生的学号
Select sno,avg(grade)
From sc
Group by sno
Having avg(*)<70
(73) 将信息系学生视图IS_Student中学号为”95002”的学生姓名改为”刘辰”
Update is_student
Set sname=’刘晨’
Where sno=‘95002’
比较: update IS_Student set sname=’刘辰’ where sno=’95003’ 此语句不能实现数据的更新.
(74) 向信息系学生视图IS_Student中插入一个新的学生记录, 学号为95029,姓名为”刘一梦”, 年龄为20岁
Insert
Into is_student
Values(‘95029’,‘刘一梦’,‘20’)
(75) 删除信息系学生视图IS_Student中学号为95004的学生的记录
Delete
From Is_student
Where sno=’95004’