码迷,mamicode.com
首页 > 数据库 > 详细

删除SQL约束的方法

时间:2015-09-25 12:52:55      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:

在SQL数据库中,如果需要删除表约束,应该如何操作呢?下面就将为您介绍删除SQL表约束的方法,供您参考,希望对您有所帮助。

--1)禁止所有表约束的SQL
select alter table +name+ nocheck constraint all from sysobjects where type=U

--2)删除所有表数据的SQL
select TRUNCATE TABLE +name from sysobjects where type=U

--3)恢复所有表约束的SQL
select alter table +name+ check constraint all from sysobjects where type=U

--4)删除某字段的约束
declare @name varchar(100)
--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))

 

删除SQL约束的方法

标签:

原文地址:http://www.cnblogs.com/wolfocme110/p/4837752.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!