如何在T-SQL中删除默认值或类似约束?

我知道这个语法:

ALTER TABLE [TheTable] DROP CONSTRAINT [TheDefaultConstraint] 

但是当我不知道它的名字时,如何删除默认的约束呢? (也就是说,它是在CREATE TABLE时间自动生成的。)

如果您想手动执行此操作,则可以使用Management Studio来查找它(位于表格内的Constraints节点下)。

使用SQL来做到这一点:

  • 如果约束是默认约束,则可以使用sys.default_constraints来查找它:

     SELECT OBJECT_NAME(parent_object_id) AS TableName, name AS ConstraintName FROM sys.default_constraints ORDER BY TableName, ConstraintName 
  • 如果您还在查找其他约束(检查,唯一,外键,默认主键),则可以使用sysconstraints

     SELECT OBJECT_NAME(id) AS TableName, OBJECT_NAME(constid) AS ConstraintName FROM sysconstraints ORDER BY TableName, ConstraintName 

你不会说你使用的是哪个版本的SQL Server。 上述工作在SQL 2005和SQL 2008上。

您可以使用此代码自动执行此操作:

 DECLARE @tableName VARCHAR(MAX) = '<MYTABLENAME>' DECLARE @columnName VARCHAR(MAX) = '<MYCOLUMNAME>' DECLARE @ConstraintName nvarchar(200) SELECT @ConstraintName = Name FROM SYS.DEFAULT_CONSTRAINTS WHERE PARENT_OBJECT_ID = OBJECT_ID(@tableName) AND PARENT_COLUMN_ID = ( SELECT column_id FROM sys.columns WHERE NAME = @columnName AND object_id = OBJECT_ID(@tableName)) IF @ConstraintName IS NOT NULL EXEC('ALTER TABLE '+@tableName+' DROP CONSTRAINT ' + @ConstraintName) 

只要根据需要replace<MYTABLENAME><MYCOLUMNNAME>

您可以通过sp_help [table name]find约束的名称,然后按名称将其删除。

或者你可以通过pipe理工作室做到这一点。

或者你可以使用sys.check_constraints目录视图来find它。

对于单行中的单个表和列,请使用以下内容

 declare @sql nvarchar(max); set @sql = ''; SELECT @sql+='ALTER TABLE [dbo].[YOURTABLENAME] DROP CONSTRAINT ' + ((SELECT OBJECT_NAME(constid) FROM sysconstraints WHERE OBJECT_NAME(id) = 'YOURTABLENAME'AND colid IN (SELECT ORDINAL_POSITION FROM INFORMATION_SCHEMA.COLUMNS Where Table_Name = 'YOURTABLENAME' and COLUMN_NAME = 'YOURCOLUMNNAM'))) + ';'; EXEC sp_executesql @sql; 

如果你在列上有多个约束条件,你将需要在你的约束条件上进行区分,但是如果你只有一个默认的约束条件,那么这个方法就行了。

查看information_schema中可用的其他列,以便进一步区分。

这里有我自己的版本,它会删除所有依赖的约束 – 默认约束 (如果存在的话)和所有受影响的检查约束 (正如SQL标准似乎暗示的一样,和其他一些数据库似乎是这样)

 declare @constraints varchar(4000); declare @sql varchar(4000); with table_id_column_position as ( select object_id table_id, column_id column_position from sys.columns where object_id is not null and object_id = object_id('TableName') and name = 'ColumnToBeDropped' ) select @constraints = coalesce(@constraints, 'constraint ') + '[' + name + '], ' from sysobjects where ( -- is CHECK constraint type = 'C' -- dependeds on the column and id is not null and id in ( select object_id --, object_name(object_id) from sys.sql_dependencies, table_id_column_position where object_id is not null and referenced_major_id = table_id_column_position.table_id and referenced_minor_id = table_id_column_position.column_position ) ) OR ( -- is DEFAULT constraint type = 'D' and id is not null and id in ( select object_id from sys.default_constraints, table_id_column_position where object_id is not null and parent_object_id = table_id_column_position.table_id and parent_column_id = table_id_column_position.column_position ) ); set @sql = 'alter table TableName drop ' + coalesce(@constraints, '') + ' column ColumnToBeDropped'; exec @sql 

(注意:在上面的代码中, TableNameColumnToBeDropped都会出现两次)

这通过构build单个ALTER TABLE TableName DROP CONSTRAINT c1, ..., COLUMN ColumnToBeDropped并执行它。