[关闭]
@rg070836rg 2016-01-20T00:16:06.000000Z 字数 12010 阅读 1785

数据库实验集合

datastructure

0 建表代码

  1. create database Students
  2. on
  3. primary(name=Students,
  4. filename='D:\test\test.mdf',
  5. size=4mb,
  6. maxsize=10mb,
  7. filegrowth=2mb
  8. )
  9. log on
  10. (name=Studentslog,
  11. filename='D:\test\testlog.ldf',
  12. size=1mb,
  13. maxsize=5mb,
  14. filegrowth=1mb
  15. )
  16. use Students;
  17. create table Student
  18. (
  19. sno char(8) not null primary key,
  20. Sname char(15) null,
  21. Ssex char(4) null,
  22. Sage int null,
  23. Sdept char(10) null
  24. )
  25. create table Course
  26. (
  27. Cno char(6) not null,
  28. Cname char(10),
  29. Cpno char(6),
  30. Ccredit int null,
  31. primary key(cno),
  32. )
  33. go
  34. create table SC
  35. (
  36. Sno char(8) not null foreign key references Student(sno),
  37. Cno char(6) not null foreign key references course(cno),
  38. primary key(Sno,Cno),
  39. Grade int null
  40. )
  41. insert into Student values('19130201','丁鹏','男',20,'Software')
  42. insert into Student values('19130202','王韵婷','女',20,'Software')
  43. insert into Student values('19130203','尹嘉琪','男',18,'Software')
  44. insert into Student values('19130204','卢冬冬','男',20,'Software')
  45. insert into Student values('19130205','史逸凡','男',19,'Software')
  46. insert into Course values('1','数据库',5,4)
  47. insert into Course values('2','数学',null,2)
  48. insert into Course values('3','信息系统',1,4)
  49. insert into Course values('4','操作系统',6,3)
  50. insert into Course values('5','数据结构',7,5)
  51. insert into Course values('6','数据处理',null,2)
  52. insert into Course values('7','PASCAL语言',6,4)
  53. insert into SC values('19130201',1,99)
  54. insert into SC values('19130202',5,95)
  55. insert into SC values('19130203',3,100)
  56. insert into SC values('19130205',1,93)
  57. insert into SC values('19130204',5,92)
  58. alter table course add foreign key(cpno) references course(cno)
  59. select * from Course;
  60. select * from SC;
  61. select * from Student;

数据库10.8实验


1 给表student列Sno增加检查长度为8位的约束并测试。

  1. alter table Student add constraint ck_tudent_ssex check (len(Sno)=8)

2 给表student列ssex的输入限定为男、女两个值并测试。

  1. use Students;
  2. create table Student
  3. (
  4. Sno char(8) not null primary key,
  5. Sname char(8) null,
  6. Ssex char(4) null check(ssex='男' or ssex='女'),
  7. Sage int null,
  8. Sdept char(10) null
  9. )

3 给表sc列grade的输入限定为0到100并测试。

  1. use Students;
  2. create table SC
  3. (
  4. Sno char(8) not null,
  5. Cno int null,
  6. Grade int null check(Grade >=0 and Grade<=100),
  7. //或采用constraint ck_sc_grade check(Grade >=0 and Grade<=100)
  8. )

4 给表sc的列增加外键约束并测试。

  1. alter table SC add constraint pk_sc_fk foreign key(Sno) references Student(Sno);

5 给表student增加列idcard表示身份证号并限定输入长度为18位,且最后一位奇数表示男,偶数表示女,这个值必须与ssex一致,并请测试。

  1. alter table Student add idcard char(18) null;
  2. alter table Student add constraint pk_id_ck check(len(idcard)=18 and((CAST(right(idcard,1) AS INT )%2=1 and Ssex ='男') or (CAST(right(idcard,1) AS INT )%2=0 and Ssex ='女')));

数据库10.15实验

1.查询姓名为“丁鹏”的学号、性别、年龄、院系;

  1. select sno,Ssex,Sage,Sdept from Student where Sname='丁鹏';

2.查询选修课程名为“数据库”课程的同学的学号、姓名、院系;

  1. select sno,Sname,Sdept from Student where sno in
  2. (select Sno from SC where Cno in
  3. (select Cno from Course where Cname = '数据库'));

3.查询没被任何同学选的课程号及课程名;

  1. select Cno,Cname from Course where Cno not in
  2. (select distinct Cno from SC);

4查询与“丁鹏”在同一院系的同学的学号、姓名、性别、院系;

  1. select sno,Sname,Ssex,Sdept from Student where Sdept in(
  2. select Sdept from Student where Sname='丁鹏');

数据库10.22实验

  1. 查询所有被选的课程号及课程名,并将查询结果的列名用中文表示
  1. select Cno as '课程编号',Cname as '课程名称' from Course where Cno in
  2. (select distinct Cno from SC);
  1. 查询年龄在19到21岁之间的同学的学号、姓名、性别,并按学号降序显示
  1. select sno,sname,ssex from Student where Sage>=19 and Sage<=21 order by sno desc
  1. 查询既不是英语系、数学系,也不是计算机科学系的学生的学号、姓名和性别
  1. select sno,sname,ssex from Student where Sdept!='English' and
  2. Sdept!='Math' and Sdept!='Computer'
  1. 查询所有姓王的学生的学号、姓名和性别,并按学号升序显示
  1. select sno,sname,ssex from Student where sname like '王%' order by sno
  1. 查询所有不姓刘的学生姓名
  1. select sname from Student where sname not like '刘%'
  1. 查询名字中第2个字为"敏"字的学生的学号和姓名
  1. select sno,sname from Student where sname like '_敏%'
  1. 查所有有成绩的学生学号和课程号
  1. select sno,cno from SC where Grade is not null

数据库10.29实验

  1. 创建登录名log1,口令为123456,缺省数据库为 student
  1. exec sp_addlogin 'log1','123456','Students'
  1. 创建用户wangyong,登录名为log1
  1. exec sp_grantdbaccess 'log1','wangyong'
  1. 创建角色student_role
  1. EXEC sp_addrole 'student_role'
  1. 为角色studen_role增加成员wangyong
  1. EXEC sp_addrolemember 'student_role', 'wangyong'
  1. 授予角色studen_role在表student上的查询权限、在sc表上的插入权限
  1. GRANT select on Student TO student_role
  2. GRANT insert on SC TO student_role
  1. 撤销角色studen_role在表student上的查询权限
  1. REVOKE SELECT ON Student FROM student_role
  1. 删除角色、用户、登录名
  1. exec sp_droprolemember 'student_role','wangyong'
  2. exec sp_droprole 'student_role'
  3. exec sp_droplogin 'log1'
  1. 创建视图v_1查询计算机系的所有学生学号、姓名、性别、年龄
  1. create view CS_Student
  2. AS
  3. select sno as '学号',Sname as '姓名',Ssex as '性别',Sage as '年龄'
  4. FROM Student
  5. WHERE Sdept='Software'

数据库11.05实验

  1. 查询选修了课程的学生人数
  1. select count(distinct Sno) from SC
  1. 计算1号课程的学生平均成绩
  1. select AVG(Grade) as 平均成绩 from SC where Cno='1'
  1. 查询选修1号课程的学生最高分数、最低分数
  1. select MAX(Grade)as 最高成绩,MIN(Grade)as 最低成绩 from SC where Cno='1'
  1. 列出每个课程号及相应的选课人数
  1. select Cno ,COUNT(Sno) as 选课人数 from SC group by Cno
  1. 查询选修了3门以上课程的学生学号
  1. select Sno from SC group by Sno having COUNT(*)>3
  1. 列出每门课程的最高分数、最低分数
  1. select cno,MAX(Grade)as 最高成绩,MIN(Grade)as 最低成绩 from SC group by Cno

数据库11.19实验


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

  1. select a.cno as '课程号' , a.cname as '课程名', b.cpno as '间接先修课'
  2. from course a,course b
  3. where b.cno = a.cpno
  1. 查询每个学生的学号、姓名、选修的课程名及成绩
  1. select a.sno as '学号',a.sname as '姓名',cno as '选修科目',grade as '成绩'
  2. from Student a,sc b
  3. where a.sno=b.sno
  1. 查询与“刘晨”在同一个系学习的学生。
  1. select a.*
  2. from Student a,Student b
  3. where b.sname='丁鹏'
  4. and a.sdept=b.sdept
  1. 查询选修了课程名为“信息系统”的学生学号和姓名
  1. select a.sno,a.sname
  2. from Student a,course b,sc c
  3. where b.cname='数据库'
  4. and c.cno=b.cno
  5. and a.sno=c.sno
  1. 找出每个学生超过他选修课程平均成绩的课程号与课程名。
  1. select b.sno,a.cno ,a.cname from
  2. course a,
  3. sc b,
  4. (select sno,AVG(grade) as 'avge' from sc group by sno)c
  5. where a.cno=b.cno
  6. and b.sno=c.sno
  7. and b.grade >= c.avge

数据库11.26实验

  1. 列出每个系的男生人数、女生人数
  1. select Sdept,ssex,count(ssex)as '人数' from Student group by Sdept,ssex
  1. 查询选修了课程名为“信息系统”的学生学号和姓名
  1. select b.cname,a.sname,a.sno
  2. from Student a,course b,sc c
  3. where a.sno=c.sno
  4. and b.cno=c.cno
  5. and b.cname='数据库'
  1. 删除“王兰”所选的全部课程;
  1. delete from sc
  2. where sno in
  3. (select sno from Student where sname='丁鹏')
  1. 将“计算机”系的学生成绩全部清零;
  1. update sc
  2. set grade = null
  3. where sno in
  4. (select sno from student where sdept ='software')
  1. 查询没有选修1号课程的学生姓名。
  1. select sname from Student
  2. where sno not in
  3. (select sno from sc where cno =1)
  1. 查询选修了课程1或者选修了课程2的学生姓名。
  1. select sname from Student
  2. where sno in
  3. (select sno from sc where cno ='1')
  4. union
  5. select sname from Student
  6. where sno in
  7. (select sno from sc where cno ='2')
  1. 查询既选修了课程1又选修了课程2的学生姓名
  1. select sname from Student
  2. where sno in
  3. (select sno from sc where cno ='1')
  4. intersect
  5. select sname from Student
  6. where sno in
  7. (select sno from sc where cno ='2')
  1. 查询选修了课程1但没有又选修了课程2的学生姓名
  1. select sname from Student
  2. where sno in
  3. (select sno from sc where cno ='1')
  4. except
  5. select sname from Student
  6. where sno in
  7. (select sno from sc where cno ='2')

数据库12.03实验

  1. 创建一个视图显示学号、姓名、数据结构和数据库原理课程的成绩
  1. create view Is_S1(sno,sname,grade1,grade2)
  2. as
  3. select a.sno,a.sname,b.Grade,c.Grade
  4. from Student a
  5. left outer join
  6. (select sno,grade from sc,course where sc.cno=cource.cno and course.cname='数据结构') b on b.sno=a.sno
  7. left outer join
  8. (select sno,grade from sc,course where sc.cno=cource.cno and course.cname='数据库') c on c.sno=a.sno
  1. 查询选修了全部课程的同学的学号、姓名
  1. select a.sname,a.sno
  2. from Student a
  3. where not exists
  4. (select *
  5. from Course
  6. where not exists
  7. (select *
  8. from SC
  9. where Sno= a.Sno
  10. and Cno= Course.Cno
  11. )
  12. )
  1. 查询平均成绩在60分以上的同学的学号、姓名
  1. select a.sno, b.sname,a.平均成绩
  2. from Student b,
  3. (
  4. select sno,AVG(grade) as '平均成绩'
  5. from sc
  6. group by sno
  7. having AVG(grade)>60
  8. ) a
  9. where a.sno = b.sno
  1. 查询平均成绩在全系平均成绩之上的同学的学号、姓名
  1. go
  2. create view S_G(sno,sname,Gavg)
  3. as
  4. select student.sno,sname,AVG(grade)
  5. from sc,student
  6. where student.sno=sc.sno
  7. group by student.sno,student.sname
  8. go
  9. select *
  10. from S_G
  11. where Gavg>(select AVG(grade) from sc)

数据库12.10实验

  1. 列出每门课程成绩都在90分以上的学生学号、姓名
  1. //拿出每条记录,若存在一门课小于90分,则不要
  2. select sno,sname
  3. from Student a
  4. where not exists
  5. (
  6. select grade from sc
  7. where a.sno=sc.sno
  8. and sc.grade<=90
  9. )
  1. 查询没有选修数据结构课程的学生学号与姓名
  1. //拿出每一条记录,对比是否选择了数据结构
  2. select a.sno,a.sname
  3. from Student a
  4. where not exists
  5. (
  6. select a.* from course b,sc c
  7. where a.sno=c.sno
  8. and b.cname='数据结构'
  9. and c.cno=b.cno
  10. )
  1. 查询既没有选修数据结构又没有选修数据库课程的学生学号与姓名
  1. //利用交集,结合上题
  2. select a.sno,a.sname
  3. from Student a
  4. where not exists
  5. (
  6. select a.* from course b,sc c
  7. where a.sno=c.sno
  8. and b.cname='数据结构'
  9. and c.cno=b.cno
  10. )
  11. intersect
  12. select a.sno,a.sname
  13. from Student a
  14. where not exists
  15. (
  16. select a.* from course b,sc c
  17. where a.sno=c.sno
  18. and b.cname='数据库'
  19. and c.cno=b.cno
  20. )
  1. 将所有选择“数据结构”课程的“计算机系”的学生成绩置为0分
  1. //结合第二题
  2. update sc
  3. set grade = 98
  4. where sno not in
  5. (
  6. select a.sno from student a ,course b,sc c
  7. where a.sno=c.sno
  8. and b.cname='数据结构'
  9. and c.cno=b.cno
  10. and a.sdept='software'
  11. )

数据库12.17实验

0 上课示例

  1. create procedure sp_1(@sdept char(4))
  2. as
  3. begin
  4. select a.sno,a.sname,b.grade
  5. from student a,sc b
  6. where sdept = @sdept
  7. and a.sno=b.sno
  8. end
  9. //调用
  10. execute sp_1 'software'
  1. create proc sp_2(@cno1 char(4),@avge int output)
  2. as
  3. begin
  4. select @avge = avg(grade)
  5. from sc
  6. where cno=@cno1
  7. end
  1. declare @avge1 int
  2. declare @cno2 char(4)
  3. set @cno2='001'
  4. select @cno2='001'
  5. exec sp_2 @cno2,@avge1 output
  6. select @avge1
  1. if @x1>1 and @x1<10
  2. begin
  3. end
  4. else
  5. begin
  6. end

(1)编写一个存储过程,可以查询指定系的学生的选课信息,列出学号、姓名、所在系、课程名和成绩等内容。调用该存储过程,测试执行结果。

  1. create procedure sp_2(@sdept char(20))
  2. as
  3. begin
  4. select a.sno,a.sname,a.sdept,b.cname,c.grade
  5. from Student a,course b,sc c
  6. where a.sno=c.sno
  7. and b.cno=c.cno
  8. and a.sdept=@sdept
  9. end
  10. execute sp_2 'software'

(2)编写一个存储过程,返回指定课程的平均分。调用该存储过程,测试执行结果。

  1. create proc sp_3(@cno1 char(4),@avge int output)
  2. as
  3. begin
  4. select @avge = avg(grade)
  5. from sc
  6. where cno=@cno1
  7. end
  8. declare @avge1 int
  9. declare @cno2 char(4)
  10. set @cno2='3'
  11. exec sp_3 @cno2,@avge1 output
  12. select @avge1

(3)编写一个存储过程可以查询指定系指定成绩等级的学生的选课信息,列出学号、姓名、所在系、课程名和成绩等内容。调用该存储过程,测试执行结果。(成绩等级为优、良、中、及格、不及格,其中成绩在90分到100分之间为‘优’,在80分到89分之间为‘良’,在70分到79分之间为‘中’,在60分到69分之间为‘及格’,在0分到59分之间为‘不及格’。)

  1. create procedure sp_6(@sdept varchar(20),@GradeLevel varchar(6))
  2. as
  3. Declare @SQLText varchar(200),@GradeStr varchar(30)
  4. Set @SQLText=
  5. 'Select S.sno, S.sname, S.Sdept, C.Cname, SC.grade
  6. From Student S
  7. Left Join SC
  8. on S.sno=SC.sno
  9. Left Join Course C
  10. on SC.Cno=C.cno'
  11. Set @GradeStr= Case
  12. When @GradeLevel='优' then 'between 90 And 100'
  13. When @GradeLevel='良' then 'between 80 And 89'
  14. When @GradeLevel='中' then 'between 70 And 79'
  15. When @GradeLevel='及格' then 'between 60 And 69'
  16. When @GradeLevel='不及格' then 'between 0 And 59'
  17. When @GradeLevel IS NULL then 'IS NULL'
  18. Else 'LevelError'
  19. end
  20. IF @GradeStr='LevelError'
  21. print '错误:输入的成绩等级不符合要求!'
  22. Else
  23. Execute(@SQLText+' where Sdept='''+@sdept +''' And Grade '+@GradeStr)
  24. Execute sp_6 'software','优'

数据库12.24实验

  1. 实验十五:T-SQL(13)
  2. 一个简化的图书馆信息管理系统,系统需求如下:
  3. 1.图书馆有若干管理员librarian,各自有员工号empid、姓名name、身份证号idno等属性。
  4. 2.图书馆有若干种图书booktype,每种图书有ISBN、名称title、出版社publisher、作者writers、价格price等属性,每种图书有唯一的ISBN号,同种图书可购入多本。
  5. 3.每一本图书book有唯一标记bookid和种类booktype
  6. 4.读者reader在办理借书证后方可借阅,一个读者有唯一的借书证号cardno,还有姓名name、身份证号idno、住址address、注销标记logoff等。每个读者最多可借20本书,读者在注销前,须归还所有已借图书或报失。
  7. 5.需处理以下基本业务:
  8. ①借书:在某时刻某读者通过某管理员借阅某一本书。
  9. ②还书:在某时刻通过某管理员归还某一本书,读者可以在借阅历史表中查阅自己以前所借的书。
  10. ③报失:在某时刻某读者向某管理员报失某一本书;报失之后该书不能再借;每一次还书和报失记录都须对应某一次借书记录,且可由不同管理员处理。
  11. E/R图建立该系统的概念模型如下:

187670B02AA84575AC18BD628EFDF806.png-23.7kB

  1. 请按要求完成如下工作:
  2. 1. 参考以上E-R图,设计关系模式,并确定各关系模式的属性应满足的数据完整性约束,然后定义表的参照完整性约束
  3. 2. 根据借还书流程设计相应的触发器.

0建库

  1. create database LIS
  2. on
  3. primary(name=LIS,
  4. filename='D:\test\test1.mdf',
  5. size=4mb,
  6. maxsize=10mb,
  7. filegrowth=2mb
  8. )
  9. log on
  10. (name=LISlog,
  11. filename='D:\test\testlog1.ldf',
  12. size=1mb,
  13. maxsize=5mb,
  14. filegrowth=1mb
  15. )
  16. use LIS;

1 librarian表

  1. --1.图书馆有若干管理员librarian,各自有员工号empid、姓名name、身份证号idno等属性。
  2. create table librarian
  3. (
  4. Lempid char(12) not null primary key,
  5. Lname char(15) null,
  6. Lidno char(18) null
  7. )

2 booktype表

  1. --2.图书馆有若干种图书booktype,每种图书有ISBN、名称title、出版社publisher
  2. --作者writers、价格price等属性,每种图书有唯一的ISBN号,同种图书可购入多本。
  3. create table booktype
  4. (
  5. ISBN char(20) not null primary key,
  6. title char(15),
  7. publisher char(15),
  8. writers char(15),
  9. price int
  10. )

3 book表

  1. --3.每一本图书book有唯一标记bookid和种类booktype
  2. create table book
  3. (
  4. bookid char(8) not null primary key,
  5. ISBN char(20) foreign key references booktype(ISBN),
  6. )

4 reader表

  1. --4.读者reader在办理借书证后方可借阅,一个读者有唯一的借书证号cardno
  2. --还有姓名name、身份证号idno、住址address、注销标记logoff等。
  3. --每个读者最多可借20本书,读者在注销前,须归还所有已借图书或报失。
  4. create table reader
  5. (
  6. cardno char(20) not null primary key,
  7. name char(15) null,
  8. idno char(18) null,
  9. address char(18) null,
  10. logoff char(1) not null,
  11. )

5 Record表

  1. --5.需处理以下基本业务:
  2. --①借书:在某时刻某读者通过某管理员借阅某一本书。
  3. --②还书:在某时刻通过某管理员归还某一本书,读者可以在借阅历史表中查阅自己以前所借的书。
  4. --③报失:在某时刻某读者向某管理员报失某一本书;报失之后该书不能再借;
  5. --每一次还书和报失记录都须对应某一次借书记录,且可由不同管理员处理。
  6. --用E/R图建立该系统的概念模型如下:
  7. create table Record
  8. (
  9. recid char(12) not null primary key,
  10. brwLempid char(12) foreign key references librarian(Lempid),
  11. cardno char(20) foreign key references reader(cardno),
  12. bookid char(8)foreign key references book(bookid),
  13. borrowdate DATETIME,
  14. status char(8) null,
  15. enddate DATETIME,
  16. endLempid char(12) foreign key references librarian(Lempid),
  17. )

6 根据借还书流程设计相应的触发器

  1. CREATE trigger borrow_record on book_record after insert
  2. as begin
  3. declare @bookid1 char(10)
  4. declare @empid1 int
  5. declare @borrow_time char(10)
  6. select @bookid1 = bookid ,@empid1 = empid ,@borrow_time = borrow_time
  7. from inserted
  8. insert into borrow_record values(@bookid1,@empid1,@borrow_time)
  9. end
  10. CREATE trigger return_record on book_record after insert
  11. as begin
  12. declare @bookid1 char(10)
  13. declare @empid1 int
  14. declare @return_time1 char(10)
  15. declare @return_flag1 char(4)
  16. select @bookid1 = bookid ,@empid1 = empid ,@return_time1 = return_time,@return_flag1 = return_flag
  17. from inserted
  18. insert into return_record values(@bookid1,@empid1,@return_time1,@return_flag1)
  19. end
  20. CREATE trigger lost_record on book_record after insert
  21. as begin
  22. declare @bookid1 char(10)
  23. declare @empid1 int
  24. declare @lost_time1 char(10)
  25. declare @lost_flag1 char(4)
  26. select @bookid1 = bookid ,@empid1 = empid ,@lost_time1 = lost_time,@lost_flag1 = lost_flag
  27. from inserted
  28. insert into lost_record values(@bookid1,@empid1,@lost_time1,@lost_flag1)
  29. end
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注