sql中為表加約束的sql語(yǔ)句收藏 --為表userinfo的loginName列加唯一約束
alter table userinfo
add constraint UQ_loginName unique(loginName)
--為表userinfo的loginName列加主鍵約束
alter table userinfo
add constraint PK_loginName primary key(loginName)
--為表userinfo的age列添加檢查約束
alter table userinfo
add constraint CK_age check(age between 0 and 100)
--為表userinfo的address列添加默認約束
alter table userinfo
add constraint DF_address default('地址不詳') for address
--為表userinfo的classId添加對應classinfo表的classId主鍵的外鍵
alter table userinfo
add constraint PK_classId foreign key(classId) references classinfo(classId)
--創(chuàng )建聚集索引
create clustered index Idx_Area_ID on Company(Area_ID)
--創(chuàng )建唯一聚集索引
create unique clustered index Idx_Un_Area_ID on Company(Area_ID)
--創(chuàng )建非聚集索引
create nonclustered index Idx_Area_ID on Company(Area_ID)
--刪除約束
if exists(select * from sys.default_constraints where name='DF_address' and parent_object_id=object_id('userinfo'))
begin
alter table userinfo drop constraint DF_address
end
本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請
點(diǎn)擊舉報。