SQL默认值约束INFORMATION_SCHEMA

时间:2012-11-14 11:25:20

标签: sql sql-server information-schema

我的表中有一列有默认值约束'DF_DOC_DMA_PLACE_1_dma_id'

我想删除该列,但首先我需要删除该约束。问题是该表存在于许多数据库中,其中一些数据库不存在约束。如何首先检查约束是否存在,然后将其删除?

我知道INFORMATION_SCHEMA中的视图但我找不到具有此约束的视图?你能告诉我默认值约束在哪里吗?感谢

2 个答案:

答案 0 :(得分:2)

请使用以下查询查找每个表的默认约束。

SELECT OBJECT_NAME(OBJECT_ID) AS NameofConstraint,
SCHEMA_NAME(schema_id) AS SchemaName,
OBJECT_NAME(parent_object_id) AS TableName,
type_desc AS ConstraintType
FROM sys.objects
WHERE type_desc = 'DEFAULT_CONSTRAINT'

答案 1 :(得分:1)

根据你会发现的内容尝试一些......

select t.name as 'table', c.[name] as 'column' ,dc.[name],dc.[definition]
from sys.tables as t
join sys.columns as c on (c.[object_id] = t.[object_id])
join sys.default_constraints as dc on (dc.[object_id] = c.[default_object_id])