检查表上的约束

时间:2009-03-05 19:06:37

标签: sql-server

如果我按如下方式检查列的存在,在添加之前,我将如何处理UNIQUE约束的等效项?

IF NOT EXISTS (SELECT * FROM sys.columns WHERE object_id=OBJECT_ID('[Table]') AND [Name]='Column')
    ALTER TABLE [Table] 
    ADD 
    [Column] varchar(500)

2 个答案:

答案 0 :(得分:1)

对于约束FK_myTable(SQL 2000):

if not exists ( select *
    from sysconstraints sc
    inner join sysobjects tbl on sc.id = tbl.id
    inner join sysobjects con on sc.constid = con.id
    where tbl.name = 'myTable' and con.name = 'FK_myTable' )

答案 1 :(得分:1)

从sys.objects中选择*,其中type ='uq',parent_object_id = OBJECT_ID('[Table]')