网站首页> 日志> 数据库系列学习(十)-约束
数据库系列学习(十)-约束
编辑时间:2016-11-08 07:46:39 作者:李大宝 浏览量:715
1.主键约束(primary key)

create table T_User1
(    
    Id int primary key identity(1,1),
    UName nvarchar(10)
)


2.非空约束(not null)

create table T_User2
(
    Id int primary key identity(1,1),
    UName nvarchar(10) not null
)


3.1唯一约束(unique)-单列

create table T_User3
(
    Id int primary key identity(1,1),
    UNo nvarchar(10)unique
)


3.2唯一约束-多列

create table T_User4
(
    Id int primary key identity(1,1),
    UAddress nvarchar(10),
    UName nvarchar(10),
    constraint uniq_addr_name unique(UAddress,UName)
)


4.1Check约束(check)-单列

create table T_User5
(
    Id int primary key identity(1,1),
    UName nvarchar(10)check(len(UName)<4),
    UAge int check(UAge>0)
)


4.2Check约束-多列
create table T_User6
(
    Id int primary key identity(1,1),
    UWorkYear int,
    UAge int,
   constraint ck_wkyear_age check(UWorkYear<UAge)
)

5.外键约束(foreign key)

create table T_Author
(
    AId int primary key identity(1,1),
    AName nvarchar(10)
)



create table T_Blog
(
    BId int primary key identity(1,1),
    BAuthorId int,
    foreign key(BAuthorId) references T_Author(AId)
)


发表评论,请先 登录

袭人 

2018-08-16
00000000000000000