东北大学秦皇岛分校数据库实验报告.docx

上传人:b****3 文档编号:27511482 上传时间:2023-07-02 格式:DOCX 页数:31 大小:3.02MB
下载 相关 举报
东北大学秦皇岛分校数据库实验报告.docx_第1页
第1页 / 共31页
东北大学秦皇岛分校数据库实验报告.docx_第2页
第2页 / 共31页
东北大学秦皇岛分校数据库实验报告.docx_第3页
第3页 / 共31页
东北大学秦皇岛分校数据库实验报告.docx_第4页
第4页 / 共31页
东北大学秦皇岛分校数据库实验报告.docx_第5页
第5页 / 共31页
点击查看更多>>
下载资源
资源描述

东北大学秦皇岛分校数据库实验报告.docx

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

东北大学秦皇岛分校数据库实验报告.docx

东北大学秦皇岛分校数据库实验报告

数据库原理

实验报告

 

学号:

姓名:

提交日期:

成绩:

 

东北大学秦皇岛分校

 

【实验内容】

实验一:

1、分别使用上述方法启动sqlserver的服务。

2、在企业管理器中创建一个数据库,要求如下:

(1)数据库名称Test1。

(2)主要数据文件:

逻辑文件名为Test1_data1,物理文件名为Test1_data1.mdf,初始容量

(3)为

1MB,最大容量为10MB,增幅为1MB。

(4)次要数据文件:

逻辑文件名为Test1_data2,物理文件名为Test1_data2.ndf,初始容量

(5)为

1MB,最大容量为10MB,增幅为1MB。

(6)事务日志文件:

逻辑文件名为Test1_log1,物理文件名为Test1_log1.ldf,初始容量为

1MB,最大容量为5MB,增幅为512KB。

3、在查询分析器中创建一个数据库,要求如下:

(1)数据库名称Test2。

(2)主要数据文件:

逻辑文件名为Test2_data1,物理文件名为Test2_data1.mdf,初始容量为

1MB,最大容量为10MB,增幅为1MB。

(3)次要数据文件:

逻辑文件名为Test2_data2,物理文件名为Test2_data2.ndf,初始容量为

1MB,最大容量为10MB,增幅为1MB。

(4)事务日志文件:

逻辑文件名为Test2_log1,物理文件名为Test2_log1.ldf,初始容量为

1MB,最大容量为5MB,增幅为1MB。

代码:

createdatabasetest

onprimary(name=test_data,

filename='d:

\sqlex\test_data.mdf',

size=1,

maxsize=10,

filegrowth=1),

(name=test_data1,

filename='d:

\sqlex\test_data1.ndf',

size=1,

maxsize=10,

filegrowth=1)

logon(name=test_log,

filename='d:

\sqlex\test_log.ldf',

size=1,

maxsize=5,

filegrowth=1)

4、在查询分析器中按照下列要求修改第3题中创建的数据库test2

(1)主要数据文件的容量为2MB,最大容量为20MB,增幅为2MB。

(2)次要数据文件的容量为2MB,最大容量为20MB,增幅为2MB。

(3)事务日志文件的容量为1MB,最大容量为10MB,增幅为2MB。

阿lter database test2  

modify file (name=Test2_data1,  size=2, 

maxsize=20, filegrowth=2)  

alter database test2  

modify file (name=Test2_data2,  size=2, 

maxsize=20, filegrowth=2)  

alter database test2  

modify file (name=Test2_log1,  size=2, 

maxsize=10, filegrowth=2) 

5、数据库更名:

把test1数据库更名为new_test1

Sp

6、在企业管理器中删除new_test1数据库,在查询分析器中

实验二:

1、创建数据库studentInfo,包含如下表,创建这些表并按要求定义约束:

表2.1student(学生表)结构

字段名

说明

数据类型

约束说明

Student_id

学号

字符串,长度为10

主键

Student_name

姓名

字符串,长度为10

非空

sex

性别

字符串,长度为1

非空值,取‘F’或‘M’

age

年龄

整数

允许空值

department

所在系名

字符串,长度为15

默认值为‘电子信息系’

表2.2course(课程表)结构

字段名

说明

数据类型

约束说明

Course_id

课程号

字符串,长度为6

主键

Course_name

课程名

字符串,长度为20

非空值

PreCouId

先修课程号

字符串,长度为6

允许空值

Credits

学分

十进制数,精度3,小数位1

非空值

表2.3score(选课表)结构

字段名

说明

数据类型

约束说明

Student_id

学号

字符串,长度为10

外键,参照student的主键

Course_id

课程号

字符串,长度为6

外键,参照course的主键

Grade

成绩

十进制数,精度3,小数位1

允许空值

联合主键:

(Student_id,Course_id)

以下为各个表的数据;

Students表数据

Student_id

Student_name

sex

age

department

20010101

Jone

M

19

Computer

20010102

Sue

F

20

Computer

20010103

Smith

M

19

Math

20030101

Allen

M

18

Automation

20030102

deepa

F

21

Art

Course表数据

Course_id

Course_name

PreCouId

Credits

C1

English

4

C2

Math

C5

2

C3

database

C2

2

Score表数据

Student_id

Course_id

Grade

20010101

C1

90

20010103

C1

88

20010102

C2

94

20010102

C2

62

代码:

createtablestudent1(

student_idchar(10)notnull,

Student_namechar(8)notnull,

Sexchar

(1)notnull,

Agetinyintnull,

Departmentchar(20)default'computer',

constraintSPKprimarykey(student_id),

constraintCKcheck(Sexin('f','m'))

createtablecourse(

course_idchar(6),

course_namechar(20)notnull,

preCNochar(6),

creditsint,

constraintCPKprimarykey(course_id)

createtablescore(

Student_idchar(10)notnullreferencesStudents(SNo),

Course_idchar(6)notnull,

Gradeint,

constraintEPKprimarykey(Student_id,Course_id),

constraintELINKforeignkey(Course_id)referencescourse(course_id)

insertintostudentvalues('20010101','Jone','m',19,'computer')

insertintostudentvalues('20010102','Sue','f',20,'computer')

insertintostudentvalues('20010103','Smith','f',19,'math')

insertintostudentvalues('20030101','Allen','m',18,'automation')

insertintostudentvalues('20030102','Deepa','f',21,'art')

insertintocoursevalues('C1','English',null,4)

insertintocoursevalues('C2','Math','C5',2)

insertintocoursevalues('C3','database','C2',2)

insertintoscorevalues('20010101','C1',90)

insertintoscorevalues('20010103','C1',88)

insertintoscorevalues('20010102','C2',94)

insertintoscorevalues('20010102','C3',62)

2、增加、修改、删除字段,要求:

(1)为表student增加一个memo(备注)字段,类型为varchar(200)。

(2)将memo字段的数据类型更改为varchar(300)。

(3)删除memo字段

altertablestudentaddmemovarchar(200)nullsp_helpstudent

altertablestudentaltercolumnmemovarchar(300)

altertablestudentdropcolumnmemo

3、向表中插入数据验证约束

use StudentInfo go  alter table score add constraint chkGrade check 

(grade >0 and grade<100)  

alter table student add constraint chkSex check(sex in ('m','f')) 

4、分别使用企业管理器和查询分析器删除表

use studentInfo go  

drop table score

实验三:

1、向students表添加一个学生记录,学号为20010112,性别为男,姓名为stefen,

2、年龄25岁,所在系为艺术系art。

insertintostudentvalues('20010112','Stefen','f',25,'art')

3、向score表添加一个选课记录,学生学号为20010112,所选课程号为C2。

insertintoscorevalues('20010112','C2',null)

4、建立临时表tempstudent,结构与students结构相同,其记录均从student表获取

Select student_id,student_name,sex,age,department into tempstudent 

from students where student_id between 20090112 and 20090118

5、将所有学生的成绩加5分

updatescoresetgrade=grade+5

6、将姓名为sue的学生所在系改为电子信息系

updatestudentsetdepartment='电子信息系'wherestudent_name='sue'

7、将选课为database的学生成绩加10分

updatescoresetgrade=grade+10wherecourse_id=(selectcourse_idfromcourse

wherecourse_name='database')

8、删除所有成绩为空的选修记录

deletescorewheregradeisnull

9、删除学生姓名为deepa的学生记录

deletestudentwherestudent_name='deepa'

10、删除计算机系选修成绩不及格的学生的选修记录。

 

deletescorewherestudent_id=(selectstudent_idfromstudentwheredepartment='

computer')andgrade<60

实验四:

在已经建立好的studentInfo数据库中使用已存在的3个数据表student、course、score基础

上完成下列查询实验:

一.简单查询实验

1、查询全体学生的学号、姓名、所在系,并为结果集的各列设置中文名称。

select学号=student_id,系=department,student_nameas姓名fromstudentinfo

2、查询全体学生的选课情况,并为所有成绩加5分。

select*fromscore表

updatescore表setgrade=grade+5

3、显示所有选课学生的学号,去掉重复行。

select distinct Student_id from score 

4、查询选课成绩大于80分的学生。

select distinct Student_id from score 

5、查询年龄在20到30之间的学生学号,姓名,所在系

selectstudent_id,student_name,department,agefromstudentinfowhereagebetween

20and30

6、查询数学系、电子信息系、艺术系的学生学号,姓名。

selectstudent_id,student_name,departmentfromstudentinfowheredepartmentin

('计算机工程系','math','art')

7、查询姓名第二个字符为u并且只有3个字符的学生学号,姓名

selectstudent_id,student_namefromstudentinfowherestudent_namelike('_u_')

二个字符为u并且只有3个字符的学生学号,姓名

8、查询所有以S开头的学生。

selectstudent_id,student_namefromstudentinfowherestudent_namelike('s%')

9、查询姓名不以S、D、或J开头的学生

10、查询没有考试成绩的学生和相应课程号(成绩值为空)

selectstudent_id,student_namefromstudentinfowherestudent_namelike

('[^sdj]%')

11、求年龄大于19岁的学生的总人数

selectstudent_id,course_id,gradefromscore表wheregradeisnotNULL

12、分别求选修了c语言课程的学生平均成绩、最高分、最低分学生。

selectcount(*)as'大于19岁人数'fromstudentinfowhereage>19

13、求学号为4090105的学生总成绩

select sum (grade)'总成绩' from score 

where student_id='4090105' 

14、求每个选课学生的学号,姓名,总成绩

select student.student_id,student_name,score.grade from student,score 

where student.student_id=score.student_id

15、查询所有姓李且全名为3个汉字的学生姓名,学号,性别

select student_id,student_name,sex from student 

where student_name='李__' 

16、求课程号及相应课程的所有的选课人数

select course_id,count(*)'Sum' from score,student 

where student.student_id=score.student_id group by course_id

17、查询选修了3门以上课程的学生姓名学号

一、多表连接查询

1、查询美国学生基本信息及选课情况

2、查询每个学生学号姓名及选修的课程名、成绩

3、求电子信息系选修课程超过2门课的学生学号姓名、平均成绩并按平均成绩降序排列

4、查询与sue在同一个系学习的所有学生的学号姓名

5、查询所有学生的选课情况,要求包括所有选修了课程的学生和没有选课的学生,显示他们

的姓名学号课程号和成绩(如果有)

1.select student_id,student_name from student where EXISTS ( select * 

from scorewhere score.student_id=student.student_id  group by 

student_id having count(*)>=3

2.select * from student,course

3.select student.student_id,student.student_name,course_id,grade from student

score where student.student_id=score.student_id 

4.select student.student_id,student.student_name from student 

where department=(select department from student 

where student_name='sue')

5.select student.student_id,student.student_name,score.course_id,score.grade 

from student,scorewhere student.student_id=score.student_id

实验五:

实验内容:

1、分别使用企业管理器和查询分析器为northwind数据库中products表建立一个聚集索引,

索引字段为产品类型和产品编号。

UsenorthwindCreateclusteredIndexIX_pdonproducts(ProductID,CategoryID)

2、查询分析器中使用表categories和products创建视图对象view_cate_prod,查询每种类

型的产品总库存(库存为products表中unitinstock列)。

createviewdbo.view_cate_prod(产品类型编号,类型名称,产品总库存)as

selectcategories.CategoryID,categories.Categoryname,sum(products.UnitsInStock)

fromproductsinnerjoincategories

onproducts.CategoryID=categories.CategoryID

groupbycategories.CategoryID,categories.Categoryname

3.在已经建立的studentInfo数据库的3个表基础上,完成下列操作:

(1)建立数学系的学生视图;

(2)建立计算机系选修了课程名为database的学生的视图,视图名为compStudentview,

该视图的列名为学号、姓名、成绩

(3)创建一个名为studentSumview的视图,包含所有学生学号和总成绩

(4)建立一个计算机系学生选修了课程名为database并且成绩大于80分的学生视图,

视图名为CompsutdentView1,视图的列为学号姓名成绩。

(5)使用sql语句删除compsutdentview1视图。

1.create view mathsthdentview  as select * from student where 

student.department ='math' 

2.create view compstudentview as select student.student_id '学号',

student_name '姓名',grade '成绩' from student,score 

where student.department ='computer' and student.student_id=score.student_id 

and score.course_id='C3' tudent_id and score.course_id='C3'

3.create view studentSumview  as select student.student_id,sum(grade)'sum' 

from student,score 

where student.student_id=score.student_id group by student.student_id 

4.create view Compstudentview1 as select  student.student_id,student_name,

grade from student,score where student.department='computer'and 

score.course_id='C3' and grade>80 and student.student_id=score.student_id  

student,score where 

student.department='computer'and score.course_id='C3' and 

grade>80 and student.student_id=score.student_id

5.drop view Compstudentview1 

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

当前位置:首页 > PPT模板 > 艺术创意

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

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