70

我知道语法:

ALTER TABLE [TheTable] DROP CONSTRAINT [TheDefaultConstraint]

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

4

6 回答 6

67

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

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)

只需更换<MYTABLENAME><MYCOLUMNNAME>酌情。

于 2012-05-25T16:46:46.897 回答
50

如果要手动执行此操作,可以使用 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 上。

于 2009-07-14T09:26:23.460 回答
4

您可以通过 sp_help [table name] 找到约束的名称,然后按名称删除它。

或者您可以通过 Management Studio 执行此操作。

于 2009-07-14T01:47:23.000 回答
4

或者您可以使用 sys.check_constraints 目录视图找到它。

于 2009-07-14T01:48:54.900 回答
2

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

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 中可用的其他列,以便您进一步区分。

于 2013-12-03T03:05:12.023 回答
0

这是我自己的版本,它删除了所有相关的约束——默认约束(如果存在)和所有受影响的检查约束(正如 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

(Beware: both TableName and ColumnToBeDropped appear twice in the code above)

This works by constructing single ALTER TABLE TableName DROP CONSTRAINT c1, ..., COLUMN ColumnToBeDropped and executing it.

于 2015-01-05T08:42:32.197 回答