2022年数据库实验报告.pdf
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_05.gif)
《2022年数据库实验报告.pdf》由会员分享,可在线阅读,更多相关《2022年数据库实验报告.pdf(25页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。
1、数据库实验报告班级:计科161姓名:蒋东旗指导老师:杜献峰精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 1 页,共 25 页 - - - - - - - - - - 实验一 基本表的定义、删除与修改定义基本表1 建立学生表Stu,每个属性名的意义为Sno-学号、 Sname-姓名、 Ssex- 性别、 Sage-年龄、 Sdept- 所在系。这里要求Sno和 Sname不能为空值,且取值唯一。Sno 为主码。create table stu( sno char(20) primary key, snam
2、e char(20) unique, ssex char(2), sage smallint, sdept char(20);2 建立课程表Cose,其属性名意义分别为Cno-课程号 , Cname-课程名 , Cpno-先修课程号 , Credit-学分。 Cno为主码。create table cose( cno char(4) primary key, cname char(40) not null, cpno char(4), ccredit smallint, foreign key(cpno)references cose(cno);3 建立成绩表StuSC。其中的属性名意义分别为
3、Sno-学号, Cno-课程号和Grade- 考试成绩。 Sno和 Cno为主码, Sno和 Cno分别为外码。create table stusc精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 2 页,共 25 页 - - - - - - - - - - ( sno char(9), cno char(4), grade smallint, primary key(cno,sno), foreign key(sno)references stu(sno), foreign key(cno)referen
4、ces cose(cno);修改基本表 (Alter)1向基本表Stu 中增加“入学时间”属性列,其属性名为RegisterDate,数据类型为DATE型。Alter table Add (RegisterDate date);2 删除 Student表的属性列RegisterDate。 Alter table stuDrop column RegisterDate cascade constraints;说明: 为了保证后面例子能够顺利运行,请大家一定将属性列RegisterDate从 Stu 表中删除。3 将 Sage(年龄 ) 的数据类型改为SMALLINT型。 Alter table
5、stuModify sage smallint;4 将 Stu 表的属性列RegisterDate名修改为RegDate,其它不变。 Alter table Rename column RegisterDate to RegDate;5 增加 Sname(姓名 ) 必须取唯一值的约束。alter table 精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 3 页,共 25 页 - - - - - - - - - - add constraint cons_sname unique(sname);6 删除
6、Sname(姓名 ) 必须取唯一值的约束。 Alter table Drop constraint cons_sname;7 表中添加PRIMARY KEY 约束 Alter table Add constraint PK_SC PRIMARY KEY (sno,cno);8 StuSC表中添加FORENGN KEY 约束alter table turing. stusc add constraint fk_sc foreign key (sno) references (sno) foreign key (cno) references (cno);9 定义 SC表中 grade 默认值为0
7、;alter table turing. Stuscmodify (grade default 0) 10 定义 SC表中 grade 最小值为0,最大值为100;alter table turing. stuscadd constraint chk_grade1check (0=grade) and (grade 3);B 链接查询不同表之间的连接查询27查询每个学生及其选修课程的情况。select student.*,sc.*from student,scwhere =;28 查询每个学生的学号(Sno) 、姓名 (Sname)、选修的课程名(Cname)及成绩 (Grade) 。sele
8、ct ,sname,cname,gradefrom student,sc,coursewhere = and =;自身连接29 查 询每一门课的间接先修课( 即先修课的先修课) 。select ,from course A,course B精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 9 页,共 25 页 - - - - - - - - - - where =;外连接30 把例中的等值连接改为左连接。C 嵌套查询带谓词 IN 的嵌套查询31 查询选修了编号为“C02 ”的课程的学生姓名(Sname)和所
9、在系 (Sdept) 。select sno,snamefrom studentwhere sno in(select snofrom scwhere cno=c02);32 查询与“李伟”在同一个系学习的学生学号(Sno) 、姓名 (Sname)和系名 (Sdept) 。select sno,sname,sdeptfrom studentwhere sdept in(select sdeptfrom studentwhere sname= 李伟 );33查询选修了课程名为“数据结构”的学生学号(Sno) 和姓名 (Sname)。select sno,snamefrom studentwher
10、e sno in(select sno 精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 10 页,共 25 页 - - - - - - - - - - from scwhere cno in(select cnofrom coursewhere cname= 数据结构 );带谓词 ANY或 ALL的嵌套查询34查询非自动化系的不超过自动化系所有学生的年龄的学生姓名(Sname)和年龄(Sage) 。select sname,sagefrom studentwhere sage=all(select sag
11、efrom studentwhere sdept=自动化 )and sdept!=自动化 ;带谓词 EXISTS的嵌套查询35查询所有选修了编号为“C01”课程的学生姓名(Sname)和所在系 (Sdept) 。select sname,sdeptfrom studentwhere exists(select snofrom scwhere = and cno=c01);36 查询选修了所有课程的学生姓名(Sname)和所在系。select sname,sdeptfrom studentwhere exists(select *精品资料 - - - 欢迎下载 - - - - - - - - -
12、 - - 欢迎下载 名师归纳 - - - - - - - - - -第 11 页,共 25 页 - - - - - - - - - - from scwhere =;D 集合查询37 查询计算机科学系的学生或年龄不大于20 岁的学生信息。select *from studentwhere sdept=计科系 unionselect *from studentwhere sage=20;38 查询数学系的学生且年龄不大于20 岁的学生的交集,这实际上就是查询数学系中年龄不大于20 岁的学生。select *from studentwhere sdept=数学 unionselect *from
13、studentwhere sage=20;39查询数学系的学生与年龄不大于20 岁的学生的差集。select *from studentwhere sdept=数学 minus精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 12 页,共 25 页 - - - - - - - - - - select *from studentwhere sage (select avg(grade) from sc where sno=2140); 3、查询每一位同学的平均成绩及选修课程的门数。select sno,av
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 2022 数据库 实验 报告
![提示](https://www.deliwenku.com/images/bang_tan.gif)
限制150内