东北大学数据库实验报告.docx

上传人:b****6 文档编号:3308253 上传时间:2022-11-21 格式:DOCX 页数:46 大小:3.56MB
下载 相关 举报
东北大学数据库实验报告.docx_第1页
第1页 / 共46页
东北大学数据库实验报告.docx_第2页
第2页 / 共46页
东北大学数据库实验报告.docx_第3页
第3页 / 共46页
东北大学数据库实验报告.docx_第4页
第4页 / 共46页
东北大学数据库实验报告.docx_第5页
第5页 / 共46页
点击查看更多>>
下载资源
资源描述

东北大学数据库实验报告.docx

《东北大学数据库实验报告.docx》由会员分享,可在线阅读,更多相关《东北大学数据库实验报告.docx(46页珍藏版)》请在冰豆网上搜索。

东北大学数据库实验报告.docx

东北大学数据库实验报告

实验

(一):

熟练掌握SQL语言

实验目的:

熟悉上机环境,创建数据库,在数据库上建立关系模式,插入数据,进行相应的查询操作。

实验内容:

具体包括如下三部分。

一、熟悉上机环境。

客户/服务器结构,数据库服务器在一台NT服务器上,同学们通过客户机(操作系统为Windows2000)上安装的SQLServer客户端程序,使用SQLServer数据库服务器。

具体包括:

1.了解SQLServer环境。

鼠标点击开始,进入“MicrosoftSQLServer企业管理器”,点击SQLServer组下的数据库服务器(服务器名称为NEUC-201S(WindowsNT)),可以看到服务器上的圆形标志变为绿色,说明客户端程序已与服务器连接成功。

点击服务器(NEUC-201S(WindowsNT))下的数据库,可以看到服务器上已建立的数据库,你可访问你有权访问的数据库,并进行相应的操作功能。

因为,数据库服务器上建有许多数据库,每个数据库都有一些合法的用户。

2.鼠标点击开始,进入“MicrosoftSQLServer查询分析器”,输入用户名和口令,进入SQL查询分析器。

如:

你登录的客户机为27号,那么请以用户名user27,口令为user27登录,登录后缺省连到数据库user27上,user27/user27是数据库user27的创建者,因此用户user27/user27具有在数据库user27上创建表等一切权力。

3.在SQL查询分析器环境下,你就可进行SQL命令语句操作了。

二、在数据库useri上创建学生选课有关关系模式,并插入相应的数据,实现有关查询。

1.描述学生、课程情况的E-R图如下,请将其转化为用关系数据模型描述的关系模式

 

2.在数据库中定义这些关系模式,并向基本表中插入如下数据:

CREATEtableStudents

(Snochar(10)primarykey,

Snamechar(20),

Sageint,

sdeptchar(20))

createtableCourses

(Cnochar(4),

Cnamechar(40),

Ccreditint,

Ctimeint,

Cpnochar(4),

primarykey(cno),

Foreignkey(Cpno)referencesCourses(Cno))

createtableSC

(Snochar(10),

Cnochar(4),

Gradeint,

primarykey(Sno,Cno),

foreignkey(Sno)referencesStudents(Sno),

foreignkey(Cno)referencesCourses(Cno))

insertintoStudentsvalues('20133901','陈一',21,'计算机')

insertintoStudentsvalues('20133902','陈二',22,'电子')

insertintoStudentsvalues('20133903','陈三',23,'通信')

insertintoStudentsvalues('20133904','陈四',20,'自动化')

insertintoStudentsvalues('20133905','陈五',21,'软件')

insertintoStudentsvalues('20133906','陈六',22,'数学')

insertintoStudentsvalues('20133907','陈七',23,'物联网')

insertintoStudentsvalues('20133908','陈八',20,'生物')

insertintoStudentsvalues('20133909','陈九',21,'计算机')

insertintoStudentsvalues('20133910','陈十',22,'机械')

insertintoCoursesvalues('0001','C语言',4,60,null)

insertintoCoursesvalues('0002','C++语言',4,60,null)

insertintoCoursesvalues('0003','JAVA',4,60,null)

insertintoCoursesvalues('0004','数据结构',4,60,null)

insertintoCoursesvalues('0005','高数',4,60,null)

insertintoCoursesvalues('0006','离散数学',4,60,null)

insertintoCoursesvalues('0007','计算原理',4,60,'0005')

insertintoCoursesvalues('0008','算法设计',4,60,null)

insertintoCoursesvalues('0009','文本',4,60,null)

insertintoCoursesvalues('0010','信息安全',4,60,'0009')

insertintoSCvalues('20133901','0001',2)

insertintoSCvalues('20133902','0001',3)

insertintoSCvalues('20133903','0001',4)

insertintoSCvalues('20133904','0003',3)

insertintoSCvalues('20133905','0001',2)

insertintoSCvalues('20133906','0001',1)

insertintoSCvalues('20133907','0001',2)

insertintoSCvalues('20133908','0001',3)

insertintoSCvalues('20133909','0001',4)

insertintoSCvalues('20133910','0002',3)

3.插入相应的数据,试着插入重复的元组,结果如何?

插入相同数据

insertintoStudentsvalues('20133901','陈一',21,'计算机')

insertintoStudentsvalues('20133901','陈一',21,'计算机')

insertintoStudentsvalues('20133901','陈一',21,'计算机')

insertintoStudentsvalues('20133901','陈一',21,'计算机')

insertintoStudentsvalues('20133901','陈一',21,'计算机')

insertintoStudentsvalues('20133901','陈一',21,'计算机')

不能插入重复数据

4.在已建立的关系模式之上(已插入一些数据)建立主键约束,参照约束和用户定义的约束(要求学生年龄不小于14岁,不大于35岁),如果约束不能建立,请分析原因,修改后建立上述约束。

altertableStudentsaddcheck(Sage>=14andSage<=35)

altertableStudentsaddprimarykey(Sno)不能建立

5.如果约束建立后,请试着插入重复元组,结果如何?

试着插入重复元组,无法插入:

实验

(二):

熟练掌握SQL语言

insertintoStudentsvalues('20133901','赵一',21,'计算机')

insertintoStudentsvalues('95133902','赵二',18,'计算机')

insertintoStudentsvalues('20133903','赵三',23,'通信')

insertintoStudentsvalues('20133904','赵四',24,'数学')

insertintoStudentsvalues('95133905','赵五',25,'软件')

insertintoStudentsvalues('20133906','赵六',26,'数学')

insertintoStudentsvalues('20133907','赵七',27,'物联网')

insertintoStudentsvalues('95133908','赵八',28,'数学')

insertintoStudentsvalues('20133909','老九',19,'计算机')

insertintoStudentsvalues('95133910','老十',17,'计算机')

insertintoStudentsvalues('95133941','老一',17,'物联网')

insertintoStudentsvalues('95133943','老二',15,'物联网')

insertintoCoursesvalues('0000','C1',4,60,null)

insertintoCoursesvalues('0001','C语言',4,60,null)

insertintoCoursesvalues('0002','C++',4,60,null)

insertintoCoursesvalues('0003','JAVA',4,60,null)

insertintoCoursesvalues('0004','数据结构',4,60,null)

insertintoCoursesvalues('0005','高数',4,60,null)

insertintoCoursesvalues('0006','离散',4,60,null)

insertintoCoursesvalues('0007','数据库',4,60,'0005')

insertintoCoursesvalues('0008','算法设计',4,60,null)

insertintoCoursesvalues('0009','文本智能处理',4,60,null)

insertintoCoursesvalues('0010','信息安全',4,60,'0009')

insertintoCoursesvalues('0011','C2',4,60,'null')

insertintoSCvalues('95133910','0000',70)

insertintoSCvalues('95133910','0001',1)

insertintoSCvalues('20133941','0001',2)

insertintoSCvalues('95133902','0001',3)

insertintoSCvalues('20133903','0001',4)

insertintoSCvalues('20133904','0003',3)

insertintoSCvalues('95133905','0001',2)

insertintoSCvalues('20133906','0001',1)

insertintoSCvalues('20133907','0001',2)

insertintoSCvalues('95133908','0001',82)

insertintoSCvalues('20133909','0000',90)

insertintoSCvalues('20133909','0002',3)

insertintoSCvalues('95133901','0011',3)

insertintoSCvalues('95133901','0002',3)

insertintoSCvalues('95133903','0000',3)

insertintoSCvalues('95133903','0001',3)

insertintoSCvalues('95133903','0002',3)

insertintoSCvalues('95133903','0003',3)

insertintoSCvalues('95133903','0004',3)

insertintoSCvalues('95133903','0005',3)

insertintoSCvalues('95133843','0006',3)

insertintoSCvalues('95133903','0007',3)

insertintoSCvalues('95133903','0008',3)

insertintoSCvalues('95133903','0009',3)

insertintoSCvalues('95133903','0010',3)

insertintoSCvalues('95133903','0011',3)

用SQL完成下列查询

1.求选修了课程的学生的学号,要求:

(1)不使用distinct语句,消除重复元组,写出其SQL语句

SelectSnofromSC

(2)使用distinct语句,消除重复元组,写出其SQL语句

selectdistinctSnofromSC

(3)如果该查询写成:

selectStudents.SnofromStudents,SCwhereStudents.Sno=SC.Sno

请问该查询结果和上面哪个结果是相同的?

和1相同

2.求开设的课程号、课程名、学时和学分,要求对查询结果的列重新命名。

selectCnoMyCno,CnameMyCname,CtimeMyCtime,CCreditMyCredictfromCourses

3.求计算机系和数学系的学生学号、姓名和年龄。

selectSno,Sname,SagefromStudentswhereSdept='计算机'orSdept='数学'

4.求不是数学系、计算机系的学生的情况,要求select后写*代替列出所有的列名。

select*fromStudentswhereStudents.Sdept!

='数学'andStudents.Sdept!

='计算机'

5.求全体学生的信息,要求按年龄升序排列。

select*fromStudentsorderbySageasc

6.求计算机系年龄在18~20岁之间的学生姓名和年龄

selectSname,SagefromStudentswhereSage>=18andSage<=20andSdept='计算机'

7.求姓名是以老开头的计算机系学生。

select*fromStudentswhereSnamelike'老%'andSdept='计算机'

8.求选修了C1课程的学生的学号、姓名、成绩。

selectStudents.Sno,Students.Sname,SC.GradefromStudents,SC,CourseswhereSC.Sno=Students.SnoandSC.Cno=Courses.CnoandCourses.Cname='C1'

9.求学号为95开头的学生的学号和所选修的课程的学分。

selectStudents.Sno,Courses.Cname,Courses.CCreditfromStudents,SC,CourseswhereStudents.Snolike'95%'andStudents.Sno=SC.SnoandSC.Cno=Courses.Cno

10.求选修C1课程且成绩为80分以上的学生的学号、姓名和成绩。

selectStudents.Sno,Students.Sname,SC.GradefromStudents,SC,CourseswhereStudents.Sno=SC.SnoandSC.Cno=Courses.CnoandCourses.Cname='C1'andSC.Grade>80

11.求计算机系每个学生的姓名,选修的课程名和成绩。

selectStudents.Sname,Courses.Cname,SC.GradefromStudents,SC,CourseswhereStudents.Sdept='计算机'andStudents.Sno=SC.SnoandSC.Cno=Courses.Cno

12.求每个学生的学号及平均成绩。

selectSno,AVG(Grade)AvgGradefromSCgroupbySno

13.求男学生每一年龄组中超过1人的年龄组及人数。

selectSage,Count(Sno)countSnofromStudentsgroupbySagehavingCount(Sno)>1

14.求每一门课程的间接先行课号。

selectC1.Cno,C2.CpnofromCoursesC1,CoursesC2whereC1.Cpno=C2.Cno

15.求选修了全部课程的学生的学号。

selectSnofromStudentswherenotexists(select*fromCourseswherenotexists(select*fromSCwhereSno=Students.SnoandCno=Courses.Cno));

 

实验(三):

数据库的安全性和完整性

实验目的:

了解和使用安全子系统;进行完整性定义和检查。

实验内容:

具体包括如下三部分。

一熟练掌握SQL(续)

1.修改表Students,在其中加入一个字段性别sexchar

(1)

altertableStudentsaddsexchar

(1);

2.创建索引:

在Students表的Sno上创建唯一聚簇索引,在SC表的(Sno,Cno)上创建唯一索引.查询Students表的内容,记录的顺序有变化吗?

查询SC表的内容,记录的顺序有变化吗?

CreateuniqueclusteredindexStudent_snoonStudents(Sno);

CreateuniqueindexSno_cnoonSC(Sno,Cno);

Students表的内容,记录的顺序有变化

SC表的内容,记录的顺序没有变化

3.创建视图

▪创建计算机系学生视图CS_Students,其中包括学号、年龄和性别。

带withcheckopition子句

不带withcheckopition子句

时各插入一计算机系学生记录,通过视图查询插入的记录,结果如何?

通过表Students查询插入的记录,查看所在系字段上的值?

createviewCS_students1asselectSno,Sage,sexfromStudentswhereSdept='CS'withcheckoption

INSERTINTOCS_students1

VALUES('20133333',19,'m');

createviewCS_students2asselectSno,Sage,sexfromStudentswhereSdept='CS'

INSERTINTOCS_students2

VALUES('20138888',18,'m');

▪创建一视图,表示学生的平均成绩,其中包括Sno,avgGrade。

通过该视图插入一学生姓名和平均成绩,结果如何?

通过该视图查找平均成绩在90分以上的学生的学号和平均成绩,结果又如何?

createviewStuavggrade(sno,avggrade)

asselectSno,avg(Grade)fromSCgroupbySC.sno

InsertintoStuavggrade

values('20133333',88)

selectsno,avggrade

fromStuavggrade

whereavggrade>=90

二了解和使用安全子系统

1、将sqlserver的一合法帐户(user60)加入到你的数据库当中来,并给其授权,

SP_grantdbaccess'user63';

使之:

具有创建表的权限

grantCreatetabletouser63;

具有对Students表查询的权限

grantselectonStudentstouser63;

具有对Studetns表的年龄字段修改的权限

grantupdate(Sage)onStudentstouser63;

2、用user60/user60进入系统,完成权限验证:

在Students表上,实现select*fromStudents

select*fromdb63.user63.STUDENT

在course表上,实现select*fromcourse

select*fromdb63.user63.Course

updateStudetnssetSage=Sage+1

updatedb63.user63.STUDENTsetSage=Sage+1

select*fromdb63.user63.STUDENT

updateStudentssetSdept=’CS’

updatedb63.user63.STUDENTsetSdept='CS'

select*fromdb63.user63.STUDENT

3、你是数据库的dbo,你来查看dept表,结果如何?

使用setuser命令使自己成为user60后,查看dept表,结果如何?

三完整性定义和检查

1、创建表Studetns,要求学生的年龄在16和25岁之间,并且所在系的缺省取值为‘CS’

CREATETABLEStudents

(SnoCHAR(9)notnull,

SnameCHAR(20),

Sdeptchar(20)default'CS',

Sageintcheck(Sag

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 小学教育 > 语文

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1