代碼實現為表添加相關約束
select * from Student --刪除表中的某一列 alter table Student drop column stdAddress --為某個表增加一列 alter table Student add StdAddress nvarchar(50) --修改某一列的數據類型 alter table Student alter column StdAddress varchar(50) --為某個字段增加一個主鍵約束 alter table Student add constraint PK_Student_StdId primary key (StdId) --為某個字段增加一個非空約束 alter table Student alter column StdId int not null; alter table Student alter column StdUserName nvarchar(10) not null; --為某個字段增加一個唯一約束 alter table Student add constraint UK_Student_StdUserName unique (StdUserName) --為某一列添加一個默認約束 alter table Student add constraint DF_Student_StdGender default('男') for StdGender --為某一列添加一個檢查約束 alter table Student add constraint CF_Student_StdGender check(StdGender='男' or StdGender='女') --為某一列添加一個外鍵約束 alter table Student add constraint FK_Student_Class_ClassId foreign key(ClassId) references Class(ClassId)
作者:Jeremy.Wu
出處:https://www.cnblogs.com/jeremywucnblog/
本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權利。
