How to check if a Constraint exists in Sql server?

前端 未结 13 1961
无人及你
无人及你 2020-11-29 15:03

I have this sql:

ALTER TABLE dbo.ChannelPlayerSkins
    DROP CONSTRAINT FK_ChannelPlayerSkins_Channels

but apparently, on some other databa

相关标签:
13条回答
  • 2020-11-29 15:31

    Just something to watch out for......

    In SQL Server 2008 R2 SSMS, the "Script Constraint as -> DROP And CREATE To" command produces T-SQL like below

    USE [MyDatabase]
    GO
    
    IF  EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DEF_Detail_IsDeleted]') AND type = 'D')
    BEGIN
    ALTER TABLE [Patient].[Detail] DROP CONSTRAINT [DEF_Detail_IsDeleted]
    END
    
    GO
    
    USE [MyDatabase]
    GO
    
    ALTER TABLE [Patient].[Detail] ADD  CONSTRAINT [DEF_Detail_IsDeleted]  DEFAULT ((0)) FOR [IsDeleted]
    GO
    

    Out of the box, this script does NOT drop the constraint because the SELECT returns 0 rows. (see post Microsoft Connect).

    The name of the default constraint is wrong but I gather it also has something to do with the OBJECT_ID function because changing the name doesn't fix the problem.

    To fix this, I removed the usage of OBJECT_ID and used the default constraint name instead.

    (SELECT * FROM dbo.sysobjects WHERE [name] = (N'DEF_Detail_IsDeleted') AND type = 'D')
    
    0 讨论(0)
  • 2020-11-29 15:37
    IF EXISTS(SELECT 1 FROM sys.foreign_keys WHERE parent_object_id = OBJECT_ID(N'dbo.TableName'))
     BEGIN 
    ALTER TABLE TableName DROP CONSTRAINT CONSTRAINTNAME 
    END 
    
    0 讨论(0)
  • 2020-11-29 15:37
    IF EXISTS(SELECT TOP 1 1 FROM sys.default_constraints WHERE parent_object_id = OBJECT_ID(N'[dbo].[ChannelPlayerSkins]') AND name = 'FK_ChannelPlayerSkins_Channels')
    BEGIN
        DROP CONSTRAINT FK_ChannelPlayerSkins_Channels
    END
    GO
    
    0 讨论(0)
  • 2020-11-29 15:38
    SELECT tabla.name as Tabla,
    
            restriccion.name as Restriccion, 
            restriccion.type as Tipo, 
            restriccion.type_desc as Tipo_Desc
    FROM {DATABASE_NAME}.sys.objects tabla 
    
    INNER JOIN {DATABASE_NAME}.sys.objects restriccion
    
    ON tabla.object_id = restriccion.parent_object_id
    
    WHERE tabla.type = 'U' - Solo tablas creadas por el usuario.
    
    AND restriccion.type = 'UQ' --Tipo de Restriccion UNIQUE
    
    ORDER BY tabla.name, restriccion.type_desc                
    
    0 讨论(0)
  • 2020-11-29 15:39

    Easiest way to check for the existence of a constraint (and then do something such as drop it if it exists) is to use the OBJECT_ID() function...

    IF OBJECT_ID('dbo.[CK_ConstraintName]', 'C') IS NOT NULL 
        ALTER TABLE dbo.[tablename] DROP CONSTRAINT CK_ConstraintName
    

    OBJECT_ID can be used without the second parameter ('C' for check constraints only) and that may also work, but if your constraint name matches the name of other objects in the database you may get unexpected results.

    IF OBJECT_ID('dbo.[CK_ConstraintName]') IS NOT NULL 
        ALTER TABLE dbo.[tablename] DROP CONSTRAINT CK_ConstraintName
    

    OBJECT_ID can also be used with other "constraints" such as Foreign Key constraints or Primary Key constraints, etc. For best results, always include the appropriate object type as the second parameter for the OBJECT_ID function:

    Constraint Object Types:

    • C = CHECK constraint
    • D = DEFAULT (constraint or stand-alone)
    • F = FOREIGN KEY constraint
    • PK = PRIMARY KEY constraint
    • R = Rule (old-style, stand-alone)
    • UQ = UNIQUE constraint

    Also note that the schema is often required. The schema of constraints generally takes the schema of the parent table.

    Failure to put your constraints (or whatever you are checking) in brackets when using this method may also cause a false negative -- if your object uses unusual characters (such as a .), the brackets are required.

    0 讨论(0)
  • 2020-11-29 15:40

    You can use the one above with one caveat:

    IF EXISTS(
        SELECT 1 FROM sys.foreign_keys 
        WHERE parent_object_id = OBJECT_ID(N'dbo.TableName') 
            AND name = 'CONSTRAINTNAME'
    )
    BEGIN 
        ALTER TABLE TableName DROP CONSTRAINT CONSTRAINTNAME 
    END 
    

    Need to use the name = [Constraint name] since a table may have multiple foreign keys and still not have the foreign key being checked for

    0 讨论(0)
提交回复
热议问题