2022年数据库实验报告.pdf

上传人:H****o 文档编号:14222319 上传时间:2022-05-03 格式:PDF 页数:25 大小:436.35KB
返回 下载 相关 举报
2022年数据库实验报告.pdf_第1页
第1页 / 共25页
2022年数据库实验报告.pdf_第2页
第2页 / 共25页
点击查看更多>>
资源描述

《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

14、g(grade),count(cno) from SC group by Sno; 精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 13 页,共 25 页 - - - - - - - - - - 实验三 索引及数据 DML 操作实验准备为了不破坏自己演示数据库中的数据,这里先创建自己的3个数据表 stu 表、 cose 表和 stuCose 表,并分别导入实验用数据,脚本如下:Create Table stu as select * from student; Create Table cose as s

15、elect * from course;Create Table StuCose as select * from SC;建立索引1在学生选课数据库中的Stu,Cose,StuCose 三个表建立唯一索引。其中Stu 表按Sname升序建唯一索引,Cose表按 Cname升序建唯一索引,StuCose 表按 Sno(学号 )升序和Cno(课程号 ) 号降序建唯一索引。create unique index stusno on stu(sno);create unique index cosecno on cose(cname);create unique index stucosesc on

16、stucose(sno asc,cno,desc);删除索引4删除基本表Stu 上的索引。drop index stucno;5删除基本表StuCose 上的索引。drop index stucosesc;精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 14 页,共 25 页 - - - - - - - - - - 插入数据6向 StuCose 表中插入一条新记录,学号为“000” ,选的课程号为“C01123” ,成绩为89。插入数据能否成功为什么成功。insert into stucose(sno,c

17、no,grade)values(000,“C01123,89);7向 Stu 表中插入一条新记录, “000, 刘德华 ,男, 22,计科系”。insert into stu(sno,sname,ssex,sage,sdept)values(000,刘德华 男,22,计科系 );8 创建数据库表History_Student(sno,sname,sdept),其模式及属性与Stu 完全一样。要求将关系Stu 中的所有元组插入到关系History_Student中去。create table history_student(sno,sname,sdept)as select sno,sname,

18、sdept from stu;更新数据9将学号为“ 2124”的学生年龄改为22 岁,即要修改满足条件的一个元组的属性值。update studentset age=22where sno=2124;10 将系别是 “计科系”学生都修改为“计算机科学与技术系”,并将这些学生的年龄都增加 1 岁。update studentset sdept=计算机科学与技术系where sdept=计科系 ;11 将数理学院所有学生的数据结构成绩都加5 分。精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 15 页,共 2

19、5 页 - - - - - - - - - - update scset grade=grade+5where sno in(select snofrom studentwhere sdept=数理学院 );12* 将学生李大鹏同学的数据结构成绩修改为89 分。update scset grade=89where sno in(select snofrom studentwhere sname=李大鹏 )andcno in(select cnofrom coursewhere cname= 数据结构 )13* 将数据结构最低分同学的成绩修改为60 分。update scset grade=60

20、where sno in(select snofrom scwhere gradeconnect u1/u16、在用户U1下进行查询操作,验证这时能否使用U2所具有的权限。7、激活角色。SQLset role role_name;实验五存储过程建立与调用存储过程实验1、利用存储过程,向student表添加一条学生信息。Create or replace procedure insertdata (sno %type,sname %type,ssex %type,sage %type,sdept %type )IsBeginInsert into studentValues(sno,sname,

21、ssex,sage,sdept);End insertdata;Exec insertdata(0121,lsj,男,18,english);2、利用存储过程,以姓名作为输入参数查询该生的平均成绩。Create or replace procedure getaverage(Name in %type)Is精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 21 页,共 25 页 - - - - - - - - - - Average number(3,1);BeginSelect avg(grade)Int

22、o averageFrom student,scWhere = and sname=name;End getaverage;3、利用存储过程, 以课程名作为输入参数,将计科系的该课程成绩低于60 分的都提高到 60 分。Create or replace procedure updata(name in %type)IsBeginUpdate scSet grade=60Where sno in(select sno from student where sdept=sc) and grade60 and cno in(select cno from course where cname=na

23、me);End updata;函数实验4、 创建函数,向student表中添加一条学生信息,如果添加成功则返回true, 否则返回 false 。Create or replace function t1 (sno %type,sname %type,ssex %type精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 22 页,共 25 页 - - - - - - - - - - ,sage %type,sdept %type )return numberIsBeginInsert into studen

24、tValues(sno,sname,ssex,sage,sdept);if sql%rowcount0 then return 1;elsereturn 0;end if;End t1;5、创建函数1。以学号作为输入参数,查询该生的姓名( 作为输出参数 ) 和平均成绩, 该生的平均成绩作为函数的输出。( 注:为了下面对该函数的调用,将姓名定义为OUT类型的参数,平均成绩作为返回值) 。Create or replace function tt1(sno in %type) return numberIsAverage number(3,1);sname %type;BeginSelect av

25、g(grade)Into averageFrom scWhere sno=;select sname精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 23 页,共 25 页 - - - - - - - - - - into snamefrom studentwhere sno=sno;return average;End tt1;6、创建函数。要求创建一个函数,输入一个学号通过该函数计算这个学生所选课程的平均成绩。Create or replace function t3(Name in %type) re

26、turn numberIsAverage number(3,1);BeginSelect avg(grade)Into averageFrom student,scWhere = and sname=name;return average;End t3;7、创建函数。 以课程名作为输入参数,将计科系的该课程成绩低于60 分的都提高到60分,更新的人数作为作为函数的输出。CREATE OR REPLACE FUNCTION (name IN RETURN NUMBERIS cnt NUMBER;BEGIN UPdate SET grade=60精品资料 - - - 欢迎下载 - - - - -

27、- - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 24 页,共 25 页 - - - - - - - - - - WHERE grade60 AND sno IN(select sno from where sdept=计科系 ) AND cno=(select cno from where cname=name); IF SQL%ROWCOUNT0 THEN cnt:=SQL%ROWCOUNT; RETURN cnt; ELSE RETURN 0; END IF;END Update_SC;精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 25 页,共 25 页 - - - - - - - - - -

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 高考资料

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知得利文库网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号-8 |  经营许可证:黑B2-20190332号 |   黑公网安备:91230400333293403D

© 2020-2023 www.deliwenku.com 得利文库. All Rights Reserved 黑龙江转换宝科技有限公司 

黑龙江省互联网违法和不良信息举报
举报电话:0468-3380021 邮箱:hgswwxb@163.com