SQL Server

 代码实现为表添加相关约束

  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/
  本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

原文地址:https://www.cnblogs.com/jeremywucnblog/p/12460908.html