版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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、查询每一位同学的平均成绩及选修课
12、程的门数。select sno,avg(grade),count(cno)from SCgroup by Sno;实验三 索引及数据DML操作实验准备为了不破坏自己演示数据库中的数据,这里先创建自己的3个数据表stu表、cose表和stuCose表,并分别导入实验用数据,脚本如下:Create Table stu as select * from student; Create Table cose as select * from course;Create Table StuCose as select * from SC;建立索引1 在学生选课数据库中的Stu,Cose,StuCose三
13、个表建立唯一索引。其中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 stucose(sno asc,cno,desc);删除索引4 删除基本表Stu上的索引。drop index stucno;5 删除基本表StuCose上的索引。drop index stucoses
14、c;插入数据6 向StuCose表中插入一条新记录,学号为“”,选的课程号为“C01123”,成绩为89。插入数据能否成功?为什么?成功。insert into stucose(sno,cno,grade)values(,“C01123,89);7 向Stu表中插入一条新记录,“,刘德华,男,22,计科系”。insert into stu(sno,sname,ssex,sage,sdept)values(,刘德华男,22,计科系);8 创建数据库表History_Student(sno,sname,sdept),其模式及属性与Stu完全一样。要求将关系Stu中的所有元组插入到关系History
15、_Student中去。create table history_student(sno,sname,sdept)as select sno,sname,sdept from stu;更新数据9 将学号为“4”的学生年龄改为22岁,即要修改满足条件的一个元组的属性值。update studentset age=22where sno=4;10 将系别是“计科系”学生都修改为“计算机科学与技术系”,并将这些学生的年龄都增加1岁。update studentset sdept=计算机科学与技术系where sdept=计科系;11将数理学院所有学生的数据结构成绩都加5分。update scset g
16、rade=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=60where sno in(select snofrom scwhere gradecon
17、nect u1/u16、在用户U1下进行查询操作,验证这时能否使用U2所具有的权限。7、激活角色。SQLset role role_name;实验五 存储过程建立与调用存储过程实验1、利用存储过程,向student表添加一条学生信息。Create or replace procedure insertdata (sno student.sno%type,sname student.sname%type,ssex student.ssex%type,sage student.sage%type,sdept student.sdept%type )IsBeginInsert into studen
18、tValues(sno,sname,ssex,sage,sdept);End insertdata;Exec insertdata(1,lsj,男,18,english);2、利用存储过程,以姓名作为输入参数查询该生的平均成绩。Create or replace procedure getaverage(Name in student.sname%type)IsAverage number(3,1);BeginSelect avg(grade)Into averageFrom student,scWhere student.sno=sc.sno and sname=name;End getav
19、erage;3、利用存储过程,以课程名作为输入参数,将计科系的该课程成绩低于60分的都提高到60分。Create or replace procedure updata(name in ame%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=name);End updata;函数实验4、 创建函数,向student表中添加一条学生信息,如果添加成功则返回tr
20、ue,否则返回false。Create or replace function t1 (sno student.sno%type,sname student.sname%type,ssex student.ssex%type,sage student.sage%type,sdept student.sdept%type )return numberIsBeginInsert into studentValues(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 student.sno%type) return numberIsAverage number(3,1);sname student.sname%type;BeginSelect avg(grade)Into averageFrom scWhere sno=sc.sno;select snameinto snamefrom studentwhe
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- KIN1148-Standard-生命科学试剂-MCE
- T-CNEA 249.1-2024 核工业用锆及锆合金化学分析方法 第1部分:碳量的测定 高频燃烧红外吸收法
- 2026年坚持自己测试题及答案
- 2026年工商管理测试题及答案
- 2026年工程面试测试题及答案
- 2026年centos8测试题及答案
- 2026年心动网络测试题及答案
- 2026年如何弄心理测试题及答案
- 2026年临床血液检验测试题及答案
- 2026年米与厘米测试题及答案
- 石油钻井工程技术规范
- 2026年高考语文备考之60篇背诵古诗文默写高频考查名句汇编
- 四川兆迪水泥窑协同处置一般固废项目环境影响报告表
- 2025~2026学年北京市西城区人教版六年级下学期小升初毕业考试数学试题【含解析】
- 全科医学科慢性病管理指导
- 中粮集团秋招面试题及答案
- 【普通高中数学课程标准】日常修订版-(2017年版2025年修订)
- 土木工程施工课后习题答案
- ISO9001-2026质量管理体系中英文版标准条款全文
- 《土木工程智能施工》课件 第3 章 土方工程-土方开挖与填筑
- 2025向量化与文档解析技术加速大模型RAG应用
评论
0/150
提交评论