7、查询计算机科学系的学生或年龄不大于20岁的学生信息。 8、将学号为“S03”的学生年龄改为22岁。 9、将所有学生的年龄增加1岁。 10、将数学系所有学生的成绩置零。 11、删除学号为“S04”的学生选修的课号为“C02”的记录。 12、删除所有学生的选课记录。 13、删除数学系所有学生的选课记录。 实验心得 教师评语 优 良 中 及格 不及格 批改日期
实验五 索引与视图
实验名称 使用设备 索引与视图(2课时) 实验日期 硬件:电脑一台 实验地点 软件:Windows、SQL server 2000 1、掌握sql建立索引的二种方法,即在基本表中建立和用命令方式建立;掌实验目的 握删除索引的方法。 2、掌握sql视图建立、修改和删除。 1、为student数据库中的Students,Courses,Reports三个表建立索引。其中Students表按Sno(学号)升序建立唯一索引,Courses表按Cno(课程号)升序建立唯一索引,Reports表按Sno(学号)升序和Cno(课程号)号降序建立唯一索引。 Use student create unique index Stu_Sno on students(Sno) 实 create unique index Cou_Cno on Courses(Cno) create unique index Rep_SCno on Reports column Sno,Cno desc 验 2、在基本表Students的Sname(姓名)和Sno(学号)列上建立一个聚簇索引,而且Students 中的物理记录将按照Sname值和Sno值的升序存放。 内 create clustered index Stud_SS on students(Sname,Sno) 容 3、删除基本表Reports上的Rep_SCno索引。 Drop index Reports. Rep_SCno 4、建立数学系学生的视图,并要求进行修改和插入操作时仍需保证该视图只有数学系的学生,视图的属性名为Sno,Sname,Sage,Sdept。
Create view C_Student As Select Sno,Sname,Sage,Sdept from students where Sdep=’数学’ With check option 5、建立学生的学号(Sno)、姓名(Sname)、选修课程名(Cname)及成绩(Grade)的视图。本视图由三个基本表的连接操作导出。 Create view Student_CR As SelectS.Sno,S.Sname, C.Cname,R.Grade from students as S, Courses C, Reports R where S.Sno=R.Sno and C.Cno=R.Cno 6、定义一个反映学生出生年份的视图。 Create view stu_y As Select Sno,出生年份=cast((year(getdata())-Sage) as varchar(4)) + ‘年’ from students 7、删除视图Student_CR。 Drop view Student_CR 8、在数学系的学生视图C_Student中找出年龄(Sage)小于20岁的学生姓名(Sname)和年龄(Sage)。 Select Sname,Sno from C_Student where Sage=20 9、在Student_CR视图中查询成绩在85分以上的学生学号(Sno)、姓名(Sname)和课程名称(Cname)。 Select Sno,Sname,Cname from Student_CR where Grade=85 10、将数学系学生视图C_Student中学号为S05的学生姓名改为“黄海”。 Updata C_Student Set Sname=’黄海’ Where Sno=’S05’ 11、向数学系学生视图C_Student中插入一个新的学生记录,其中学号为“S09”,姓名为“王海”,年龄为20岁。 Insert into C_Student(Sno,Sname,Sage,Sdept) Values(‘S09’,’王海’,20,’数学’) 12、删除数学系学生视图C_Student中学号为“S09”的记录。
Delete from C_Student Where Sno=’S09’ 基本掌握了用SQL语言建立视图和索引 实验心得 教师评语 优 良 中 及格 不及格 批改日期 实验六 存储过程和触发器、安全管理
实验名称 使用设备 存储过程和触发器、安全管理(2课时) 实验日期 硬件:电脑一台 实验地点 软件:Windows、SQL server 2000 1、 主要使用SQL语言对存储过程和触发器进行创建、显示与删除。 2、 会使用企业管理器对存储过程和触发器进行创建、显示与删除。 实验目的 3、主要使用SQL语言对SQL server中数据库进行安全管理。 1、根据Sno创建一个存储过程Sno_prog,用于显示学生学号和姓名。并执行此过程。 实 Use student If exists(select name from sysobjects where name=’Sno_prog’ and type=’p’) Drop procedure Sno_prog go Create procedure Sno_prog As 验 Select Sno,Sname From students Execute Sno_prog 2、创建一个存储过程add_prog,用于添加学生表Students的学生记录。 Use student If exists(select name from sysobjects where name=' add_prog' and type='p') 内 Drop procedure add_prog go Create procedure add_prog As Insert students ( Sno,Sname,Ssex,Sage,Sdept) values ('','','','','') 容 3、创建一个存储过程dele_prog,删除Courses中指定Cno的记录。
Use student If exists(select name from sysobjects where name=’ dele_prog’ and type=’p’) Drop procedure dele_prog go Create procedure dele_prog As Delete from Courses(Cno) 4、显示存储过程add_prog。 Execute sp_help add_prog 5、删除存储过程Sno_prog。 Drop procedure Sno_prog 6、在表Reports上创建一个触发器Sno_update,当Stuents表更改Sno时同步更改Reports中的Sno。 Use student If exists(select name from sysobjects where name=’ Sno_update’ and type=’tr’) Drop trigger Sno_update Go Create trigger Sno_update on Reports after updata As Declare @x1 varchar(8), @x2 varchar(8) Select @x1=Sno from delete Select @x2=Sno from inserted Updata students set Sno=@x2 where Sno=@x1 Updata Reports set Sno=@x2 where Sno=@x1 7、在表Reports上创建一个触发器Cno_update,当Courses表更改Cno时同步更改Reports中的Cno。 Use student If exists(select name from sysobjects where name=’ Cno_update’ and type=’tr’) Drop trigger Cno_update go Create trigger Cno_update on Reports after updata As Declare @xm1 varchar(8), @xm2 varchar(8) Select @xm1=Cno from delete Select @xm2=Cno from inserted Update Courses set Cno=@xm2 where Cno=@xm1 Update Reports set Cno=@xm2 where Cno=@xm1 8、在Reports表上创建一个触发器“成绩插入”、“成绩删除”、“成绩更新”触发器,当用户插入、删除、更新记录时触发。