7.索引和数据完整性

use YGGL

--建立索引
create index index_dep
on Employees(DepID)

--建立复合索引
create index index_com
on Employees(EmpName,EmpSex)

--建立唯一聚集索引
create unique clustered index index_uc
on Employees(EmpID)

--重建索引
alter index all
  on Employees rebuild

--删除索引
drop index index_com
 on Employees

--创建表是创建主键约束或唯一约束
 create table Employees5
 (
 EmployeeID int not null unique,
 EmpName varchar(6) not null primary key,--constraint pk_en primay key
 EmpSex bit not null,
 EmpEdu char(3) not null
 )

 --修改表时创建主键约束或唯一约束
 alter table Employees5
 add
 constraint uk_es unique(EmpSex)

 --删除主键约束或唯一约束
 alter table Employees5
 drop constraint uk_es

 --创建表是添加check约束
 create table student
 (
 sName varchar(6) not null,
 sAge int not null
 check(sAge<100 and sAge>0)
 )

  create table student1
 (
 sName varchar(6) not null,
 sSex char(2) not null
 check(sSex in('男','女'))
 )

 --修改表是添加check约束
 alter table student1
 add 
 constraint ck_age check(sage<100) 

 --删除check约束
 alter table student1
 drop constraint ck_age

--外键 foreign key

posted @ 2018-02-07 14:39  王雪亮  阅读(268)  评论(0编辑  收藏  举报