文档库 最新最全的文档下载
当前位置:文档库 › SQL 2008课后习题答案 实验6

SQL 2008课后习题答案 实验6

1.建立索引

<1>对YGGL数据库的Employees表中的DepartmentID列建立索引

use YGGL
go
create index depart_ind
on Employees(DepartmentID)
go

<2>在Employees表的Name列和Address列上建立复合索引

create index Ad_ind
on Employees(Name,Address)

<3>对Departments表上的DepartmentName列建立唯一非聚集索引

create unique index Dep_ind
on Departments(DepartmentName)


2.重建索引

<1>重建表Employees中的所有索引

alter index all
on Employees rebuild

<2>重建表Employees中EmployeeID列0上的索引

create index Employ_ind
on employees(EmployeeID)

alter index Employ_ind
on employees rebuild

3.删除索引

<1>使用drop index删除表Employees上的索引depart_id

drop index depart_ind on Employees

<2>使用drop index一次删除Employees表上的多个索引

drop index Employees.Employ_ind,Departments.Dep_ind,Employees.Ad_ind


4.数据完整性

<1>创建一个表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)
)

<2>删除上题中的创建unique约束

alter table Employees5
drop constraint UK_id


<3>使用T-SQL命令创建一个新表,使用一个复合列作为主键,作为表的约束,并为其命名

create table Employees7
(
EmployeeID char(6) not null,
Name char(10) not null,
Education char(4) not null,
Birthday date not null,
Sex bit not null default 1,
WorkYear tinyint null,
Address varchar(40) null,
PhoneNumber char(12) null,
DepartmentID char(3) not null,
primary key (EmployeeID,DepartmentID),
constraint ED_UK unique(EmployeeID,DepartmentID)
)
go


<4>使用alter table语句为表Employees5添加一个新列Address,并为该列定义unique约束

alter table Employees5
add Address varchar(40)
constraint AD_UK unique (Address)
go

<5>创建新表student,只考虑“号码”和“性别”两列,性别只能包含男或女

create table student
(号码 char(6) not null,
性别 char(2) not null
check(性别 in('男','女'))
)

<6>创建新表Salary2,结构与Salary相同,但Salary2表不允许OutCome列大于InCome列

create table Salary2
(EmployeeID char(6) not null,
InCome float not null,
OutCome float not null,
check(InCome>=OutCome)
)


<7>创建一个表Employees6,只考虑“工号”和“出生日期”两列,出生日期必须晚于1980年1月1日

create table Employees6
(
EmployeeID char(6) not null,
Birthday date not null check(Birthday >'1980-01-01'),

)

<8>对YGGL数据库中的Employees表进行修改,为其增加“DepartmentID”字段的check约束

use YGGL
go
alter table Employees
add constraint depart check(DepartmentID>=1 and

DepartmentID<=5)

<9>创建一个规则对象,用以限制输入到该规则所绑定的列中的值只能是该规则中列出的值

create rule list_rule
as @list in('财务部','研发部','人力资源部','销售部')
go
exec sp_bindrule 'list_rule','Departments.DepartmentName'
go

<10>建立一个一个规则对象,限制值在0~20之间。然后把它绑定到Employees表的WorkYear字段上

create rule time_rule
as @time like '[0-2][0-9]'
go
exec sp_bindrule 'time_rule','Employees.WorkYear'
go

<11>删除上述建立的规则对象

exec sp_unbindrule'Employees.WorkYear'
exec sp_unbindrule'time_rule'
go
drop rule time_rule

<12>创建一个表Salary3,要求所有Salary3表上EmployeeID列的值都要出现在Salary表中,利用参照完整性约束实现,
要求当删除或修改Salary表上的EmployeeID列时,Salary3表中的EmployeeID值也会随之变化

create table Salary3
(
EmployeeID char(6) not null primary key,
InCome float not null,
OutCome float(8) not null,
foreign key(EmployeeID)
references Salary(EmployeeID)
on update cascade
on delete cascade
)
13>使用alter table语句向Salary表中的EmployeeID列添加一个外键,要求当Employees表中要删除或修改与EmployeeID值
有关的行为时,检查Salary表有没有与该EmployeeID值相关的记录,如果存在则拒绝更新Employees表
alter table salary
add constraint aa_foreign foreign key(employeeid)
references Employees(employeeid)
on update no action
on delete no action


相关文档
相关文档 最新文档