|作用|語句|
|--|--|
|禁止所有表約束的SQL|select 'alter table '+name+' nocheck constraint all' from sysobjects where type='U'|
|刪除所有表數據的SQL|select 'TRUNCATE TABLE '+name from sysobjects where type='U'|
|恢復所有表約束的SQL|select 'alter table '+name+' check constraint all' from sysobjects where type='U'|
|刪除某字段的約束|declare @name varchar(100)<br/>--DF為約束名稱前綴<br/>select @name=b.name from syscolumns a,sysobjects b where a.id=object_id('表名') and b.id=a.cdefault and a.name='字段名' and b.name like 'DF%'<br>--刪除約束<br/>alter table 表名 drop constraint @name<br/>--為字段添加新默認值和約束<br/>ALTER TABLE 表名 ADD CONSTRAINT @name DEFAULT (0) FOR [字段名]|
|--DF為約束名稱前綴|select @name=b.name from syscolumns a,sysobjects b where a.id=object_id('表名') and b.id=a.cdefault and a.name='字段名' and b.name like 'DF%'|
|--刪除約束|alter table 表名 drop constraint @name|
|--為字段添加新默認值和約束|ALTER TABLE 表名 ADD CONSTRAINT @name DEFAULT (0) FOR [字段名]|
|--刪除約束|ALTER TABLE tablename Drop CONSTRAINT 約束名|
|--修改表中已經存在的列的屬性(不包括約束,但可以為主鍵或遞增或唯一)|ALTER TABLE tablename alter column 列名 int not null|
|--添加列的約束|ALTER TABLE tablename ADD CONSTRAINT DF_tablename_列名 DEFAULT(0) FOR 列名|
|--添加范圍約束|alter table tablename add check(性別 in ('M','F'))|