数据库系列学习(十)-约束

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)
)
更多精彩内容请看:http://www.cnblogs.com/2star
原文地址:https://www.cnblogs.com/kimisme/p/4733167.html