数据库实验报告(共17页).docx
《数据库实验报告(共17页).docx》由会员分享,可在线阅读,更多相关《数据库实验报告(共17页).docx(17页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、精选优质文档-倾情为你奉上数据库实验报告班级:计科161姓名:蒋东旗指导老师:杜献峰实验一 基本表的定义、删除与修改定义基本表1建立学生表Stu,每个属性名的意义为Sno-学号、Sname-姓名、Ssex-性别、Sage-年龄、Sdept-所在系。这里要求Sno和Sname不能为空值,且取值唯一。Sno为主码。create table stu( sno char(20) primary key, sname char(20) unique, ssex char(2), sage smallint, sdept char(20);2 建立课程表Cose,其属性名意义分别为Cno-课程号, Cna
2、me-课程名, 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。其中的属性名意义分别为Sno-学号,Cno-课程号和Grade-考试成绩。Sno和Cno为主码,Sno和Cno分别为外码。create table stusc( sno char(9), cno char(4), grad
3、e smallint, primary key(cno,sno), foreign key(sno)references stu(sno), foreign key(cno)references cose(cno);修改基本表(Alter)1 向基本表Stu中增加“入学时间”属性列,其属性名为RegisterDate,数据类型为DATE型。Alter table Turing.stuAdd (RegisterDate date);2 删除Student表的属性列RegisterDate。 Alter table stuDrop column RegisterDate cascade const
4、raints;说明:为了保证后面例子能够顺利运行,请大家一定将属性列RegisterDate从Stu表中删除。3 将Sage(年龄)的数据类型改为SMALLINT型。 Alter table stuModify sage smallint;4 将Stu表的属性列RegisterDate名修改为RegDate,其它不变。 Alter table turing.stu Rename column RegisterDate to RegDate;5 增加Sname(姓名)必须取唯一值的约束。alter table turing.stuadd constraint cons_sname unique(
5、sname);6 删除Sname(姓名)必须取唯一值的约束。 Alter table turing.stu Drop constraint cons_sname;7 表中添加PRIMARY KEY 约束 Alter table turing.stusc Add constraint PK_SC PRIMARY KEY (sno,cno); 8 StuSC表中添加FORENGN KEY 约束 alter table turing. stusc add constraint fk_sc foreign key (sno) references turing.stu(sno) foreign key
6、 (cno) references turing.cose(cno);9定义SC表中grade默认值为0; 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 student.sno
7、=sc.sno;28 查询每个学生的学号(Sno)、姓名(Sname)、选修的课程名(Cname)及成绩(Grade)。select student.sno,sname,cname,gradefrom student,sc,coursewhere student.sno=sc.sno and o=o;自身连接29 查询每一门课的间接先修课(即先修课的先修课)。select A.cname,B.cnamefrom course A,course Bwhere A.cpno=B.cno;外连接30把例3.37中的等值连接改为左连接。C 嵌套查询带谓词IN的嵌套查询31 查询选修了编号为“C02”的
8、课程的学生姓名(Sname)和所在系(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 stud
9、entwhere sno in(select sno from scwhere cno in(select cnofrom coursewhere cname=数据结构);带谓词ANY或ALL的嵌套查询34 查询非自动化系的不超过自动化系所有学生的年龄的学生姓名(Sname)和年龄(Sage)。select sname,sagefrom studentwhere sage=all(select sagefrom studentwhere sdept=自动化)and sdept!=自动化;带谓词EXISTS的嵌套查询35 查询所有选修了编号为“C01”课程的学生姓名(Sname)和所在系(Sde
10、pt)。select sname,sdeptfrom studentwhere exists(select snofrom scwhere student.sno=sc.sno and cno=c01);36 查询选修了所有课程的学生姓名(Sname)和所在系。select sname,sdeptfrom studentwhere exists(select *from scwhere student.sno=sc.sno);D 集合查询37 查询计算机科学系的学生或年龄不大于20岁的学生信息。select *from studentwhere sdept=计科系unionselect *fr
11、om studentwhere sage=20;38 查询数学系的学生且年龄不大于20岁的学生的交集,这实际上就是查询数学系中年龄不大于20岁的学生。select *from studentwhere sdept=数学unionselect *from studentwhere sage=20;39 查询数学系的学生与年龄不大于20岁的学生的差集。select *from studentwhere sdept=数学minusselect *from studentwhere sage(select avg(grade)from scwhere sno=0);3、查询每一位同学的平均成绩及选修课
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数据库 实验 报告 17
限制150内