索引和数据完整性实验报告.doc
文本预览下载声明
SQL Server2005实验指导 指导教师:龙文佳
PAGE 1
实验7 索引和数据完整性
目的与要求
掌握索引的使用方法
掌握数据完整性的实现方法
实验内容
建立索引
数据完整性
实验步骤
建立索引
= 1 \* GB3 ①对yggl数据库的employees表中的departmentid列建立索引。
Use yggl
If exists(select name from sysindexes where name=’depart_ind’
Drop index employees.depart_ind)
Go
Create index depart_ind on employees(departmentid)
= 2 \* GB3 ②对pxscj数据库的kcb的课程号列建立索引。(唯一聚集索引)
Use pxscj
If exists(select name from sysindexes where name=’kc_id_ind’)
Drop index kc_id_ind
Go
Create unique clustered index kc_in_ind on kcb(课程号)
数据完整性
= 1 \* GB3 ①建立一个规则对象,输入4个数字,每一位的范围分别是[0-3][0-9][0-6][0-9],然后把它绑定到book表的book_id字段上,再解除规则,最后删除规则。
Create table book
(
Book_id char(6) not null primary key,
Name varchar(20) not null,
Hire_date datetime not null,
Cost int check(cost=0 and cost=500) null
)
Go
Create default today as getdate()
Go
Exec sp_binddefault ‘today’,’book.[hire_date]’
Go
= 2 \* GB3 ②创建一个表employees5,只含employeeid,name,sex和education列。将name设为主键,作为列name的约束。对employeeid列进行unique约束,并作为表的约束。
create table employees5
(employeeid char(6) not null,
name char(10) not null primary key,
sex tinyint,
education char(4),
constraint uk_id unique(employeeid)
)
= 3 \* GB3 ③删除上例中创建的unique约束。
alter table employees5
drop constraint uk_id
= 4 \* GB3 ④创建新表student,只考虑“号码”和“性别”两列,性别只能包含男或女。
create table student
(号码char(6) not null,
性别char(2) not null check(性别in(男,女))
)
= 5 \* GB3 ⑤创建新表salary2,结构与salary相同,但salary2表不允许outcome列大于income列。
create table salary2
(
employeeid char(6) not null,
income float not null,
outcome float not null,
check(income=outcome)
)
= 6 \* GB3 ⑥对yggl数据库中的employees表进行修改,为其增加“departmentid”字段的check约束。
alter table employees
add constraint depart check(departmentid=1 and departmentid=5 )
= 7 \* GB3 ⑦创建一个规则对象,用以限制输入到该规则所绑定的列中的值只能是该规则中列出的值。
create rule list_rule
as @list in(财务部,研发部,人力资源部,销售部)
go
exec sp_bindrule list_rule,departments.departmentname
go
= 8 \* GB3 ⑧创建一个表salary3,要求所有salary3表上employeeid列的值都要出现在salary表中,利用参照完整性约束实现,要求
显示全部