sql SQL未命名的约束

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql SQL未命名的约束相关的知识,希望对你有一定的参考价值。

PRINT N'Dropping unnamed constraints on [dbo].[Animals]...';

DECLARE @TableName AS NVARCHAR(255)
DECLARE @ColumnName AS NVARCHAR(255)
DECLARE @ConstraintName AS NVARCHAR(255)
DECLARE @DropConstraintSQL AS NVARCHAR(255)

SET @TableName = 'Animals'
SET @ColumnName = 'NumberOfLegs'

--Get the name of the constraint that will be dropped
SET @ConstraintName = 
     (SELECT TOP 1 o.name FROM sysobjects o 
     JOIN syscolumns c 
     ON o.id = c.cdefault 
     JOIN sysobjects t 
     ON c.id = t.id 
     WHERE o.xtype = 'd' 
     AND c.name = @ColumnName 
     AND t.name = @TableName)

SELECT @ConstraintName

--Build a query string that will drop the constraint
SET @DropConstraintSQL = 'ALTER TABLE dbo.' + @TableName + ' DROP ' + @ConstraintName

--Execute the sql to drop the constraint
EXEC (@DropConstraintSQL)

以上是关于sql SQL未命名的约束的主要内容,如果未能解决你的问题,请参考以下文章

删除未命名的约束

删除未命名的约束

Sql Server数据库之约束

我可以在 SQL Server 的添加列语句中创建命名默认约束吗?

SQL Server使用sp_rename重命名约束注意事项

SQL ORA-02291: 违反完整性约束 - 未找到父键