当前位置:文档之家› sql数据库实例(数据库入门)word版本

sql数据库实例(数据库入门)word版本

s q l数据库实例(数据库入门)数据库设计及应用实验一、实验内容创建数据库:包括Student, Course, Enroll, Statistics表,表的结构如下:Student (sno, sname, age, sex)Course (cno, cname, credit)Enroll (sno, cno, grade)Statistics (sno, cNumber, creditSum)说明:cNumber是sno学生选修课程的数目;creditSum是sno学生选修课程的总学分。

1、设计并在MS SQL Server 2000中创建以上表结构,并设置完整性约束。

2、查询所有选修课程的学生的基本信息、课程信息及相应的考试成绩。

3、查询所有学生的信息,若已选课就还要给出选修课程的信息及考试成绩。

4、查询所有课程的信息,若课程有学生选修就还要给出选修课程的学生的信息及考试成绩。

5、查询选修名为“数据库”的课程的考试成绩最高的学生的信息。

6、对Student的age创建规则,满足18≤age≤25,并给出验证实例及验证结果。

7、创建触发器:当学生选修一门新的课程后,Statistics表的cNumber自动加1,且creditNumber自动增加新选课程的学分。

8、创建视图:找出所有已修学分超过6、所修课程平均分不低于60的学生的基本信息、以及所修课程的平均分。

按照平均分排序,若平均分相同按照学号排序。

9、对于如下一组数据操作:(1)select * from course;(2)select * from course where cname=’数据库’(3)select * from course where credit=3(4)select * from course where credit>2 and credit<5(5)update course set credit=3 where credit=2对Course表的credit属性创建索引,并给出上述查询在创建了该索引后的执行计划;去掉Course表的credit属性上创建的索引,并给出上述查询的执行计划。

对上述有无索引个查询执行的情况进行对比分析,总结出什么时候索引有效?10、将表Student、Course、Enroll作内连接的结果发布为HTML网页格式。

二、实验步骤及分析过程1、设计并在MS SQL Server 2000中创建以上表结构,并设置完整性约束。

(1)创建数据库SC。

SQL语句为:create database SC 创建后的数据库如图1。

图1 创建数据库的结(2)创建表创建学生表:create table Student(sno varchar(10) not null primary key,sname varchar(50) not null,age int,sex varchar(2) not null)创建课程表:create table Course(cno varchar(10) not null primary key,cname varchar(50) not null,credit int not null)创建注册表:create table Enroll(sno varchar(10) not nullreferences Student(sno),cno varchar(10) not nullreferences Course(cno),grade int,primary key (sno,cno))创建选课情况表:create table Statisticss(sno varchar(10) not null primary keyreferences Student(sno),cNumber int not null,creditSum int not null,)创建后的表如图2。

图2 创建表的结果(3)插入数据Student表:insert into Student values ('2008001','李贵斌',22,'男') insert into Student values ('2008002','冉从宝',21,'男') insert into Student values ('2008003','杨文学',20,'男') insert into Student values ('2008004','杨璐',22,'女') insert into Student values ('2008005','李小萌',20,'女')Course表:insert into Course values ('001','数据库',4)insert into Course values ('002','java',3)insert into Course values ('003','操作系统',5)insert into Course values ('004','软件工程',4)insert into Course values ('005','计算机英语',2)Enroll表:insert into Enroll values ('2008001','001',89) insert into Enroll values ('2008002','003',98) insert into Enroll values ('2008003','002',85) insert into Enroll values ('2008001','004',88) insert into Enroll values ('2008004','001',89) insert into Enroll values ('2008002','001',90) insert into Enroll values ('2008003','003',78) insert into Enroll values ('2008002','004',79)(4)查看表的内容select * from Student 结果如图3。

图3表Student中的select * from Course 结果如图4。

图4 表Course中的select * from Enroll 结果如图5。

2、查询所有选修课程学生的基本信息、课程信息及相应的考试成绩。

SQL 语句为:select a.sno,a.sname,a.age,a.sex,o,ame,b.credit,c.gradefrom Student a,Course b,Enroll cwhere a.sno=c.sno and o=o执行结果如图6所示。

执行结果分析:只显示了选了课程的学生信息。

3、查询所有学生的信息,若已选课就还要给出选修课程的信息及考试成绩。

SQL 语句为:select a.sno,a.sname,a.sex,a.age,o,ame,d.credit,d.gradefrom Student a left outer join(select o,ame,b.credit,c.grade,c.snofrom Course b,Enroll c where o=o)don a.sno=d.sno执行结果如图7所示。

图5 表Enroll 中的图6 选修课程的学生、课程及成绩执行结果分析:不但列出选了课程的学生而且还列出来没选课程的学生信息。

4、查询所有课程的信息,若课程有学生选修就还要给出选修课程的学生的信息及考试成绩。

SQL 语句为:select o,ame,a.credit,d.sno,d.sname,d.sex,d.age,d.gradefrom Course a left outer join(select b.sno,b.sname,b.age,b.sex,c.grade,ofrom Student b,Enroll c where b.sno=c.sno)don o=o执行结果如图8所示。

执行结果分析:不但列出被选课程的信息和学生选课情况而且还列出来没被选课程的信息。

5、查询选修名为“数据库”的课程的考试成绩最高的学生的信息。

(1)先查看选了数据库课程的学生信息。

图7 所有学生选课情况及成绩信息图8 所有课程、选课的学生及成绩SQL语句为:select a.sno,a.sname,a.sex,a.age,ame,c.gradefrom Student a,Course b,Enroll cwhere a.sno=c.sno and o=o and ame='数据库' 执行结果如图9所示。

执行结果分析:有三位学生选修了数据库课程。

(2)查询选修“数据库”课程的考试成绩最高的学生的信息。

SQL语句为:select a.sno,a.sname,a.sex,a.age,ame,d.gradefrom Student a ,(select o,ame,c.grade,c.sno from Course b,Enroll cwhere o=o and ame='数据库' ) dwhere a.sno=d.sno and d.grade=(select max(grade)from (select o,ame,c.gradefrom Course b,Enroll c where o=o)dwhere ame='数据库')执行结果如图10所示。

执行结果分析:与图9结合比较,图10了列出了选修“数据库”课程成绩最高学生信息。

图9 选修“数据库”课程的学生信息图10 选修“数据库”课程成绩最高学6、对Student的age创建规则,满足18≤age≤25,并给出验证实例及验证结果。

(1)创建规则并且绑定它。

相关主题