How to check if a Constraint exists in Sql server?

前端 未结 13 1962
无人及你
无人及你 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:43

    I use this to check for and remote constraints on a column. It should have everything you need.

    DECLARE
      @ps_TableName VARCHAR(300)
      , @ps_ColumnName VARCHAR(300)
    
    SET @ps_TableName = 'mytable'
    SET @ps_ColumnName = 'mycolumn'
    
    DECLARE c_ConsList CURSOR LOCAL STATIC FORWARD_ONLY FOR
        SELECT
        'ALTER TABLE ' + RTRIM(tb.name) + ' drop constraint ' + sco.name AS csql
        FROM
            sys.Objects tb
            INNER JOIN sys.Columns tc on (tb.Object_id = tc.object_id)
            INNER JOIN sys.sysconstraints sc ON (tc.Object_ID = sc.id and tc.column_id = sc.colid)
            INNER JOIN sys.objects sco ON (sc.Constid = sco.object_id)
        where
            tb.name=@ps_TableName
            AND tc.name=@ps_ColumnName
    OPEN c_ConsList
    FETCH c_ConsList INTO @ls_SQL
    WHILE (@@FETCH_STATUS = 0) BEGIN
    
        IF RTRIM(ISNULL(@ls_SQL, '')) <> '' BEGIN
            EXECUTE(@ls_SQL)
        END
        FETCH c_ConsList INTO @ls_SQL
    END
    CLOSE c_ConsList
    DEALLOCATE c_ConsList
    
    0 讨论(0)
  • 2020-11-29 15:44

    If you are looking for other type of constraint, e.g. defaults, you should use different query (From How do I find a default constraint using INFORMATION_SCHEMA? answered by devio). Use:

    SELECT * FROM sys.objects WHERE type = 'D' AND name = @name
    

    to find a default constraint by name.

    I've put together different 'IF not Exists" checks in my post "DDL 'IF not Exists" conditions to make SQL scripts re-runnable"

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

    try this:

    SELECT
        * 
        FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS 
        WHERE CONSTRAINT_NAME ='FK_ChannelPlayerSkins_Channels'
    

    -- EDIT --

    When I originally answered this question, I was thinking "Foreign Key" because the original question asked about finding "FK_ChannelPlayerSkins_Channels". Since then many people have commented on finding other "constraints" here are some other queries for that:

    --Returns one row for each CHECK, UNIQUE, PRIMARY KEY, and/or FOREIGN KEY
    SELECT * 
        FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
        WHERE CONSTRAINT_NAME='XYZ'  
    
    
    --Returns one row for each FOREIGN KEY constrain
    SELECT * 
        FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS 
        WHERE CONSTRAINT_NAME='XYZ'
    
    
    --Returns one row for each CHECK constraint 
    SELECT * 
        FROM INFORMATION_SCHEMA.CHECK_CONSTRAINTS
        WHERE CONSTRAINT_NAME='XYZ'
    

    here is an alternate method

    --Returns 1 row for each CHECK, UNIQUE, PRIMARY KEY, FOREIGN KEY, and/or DEFAULT
    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 LIKE '%CONSTRAINT'
            AND OBJECT_NAME(OBJECT_ID)='XYZ'
    

    If you need even more constraint information, look inside the system stored procedure master.sys.sp_helpconstraint to see how to get certain information. To view the source code using SQL Server Management Studio get into the "Object Explorer". From there you expand the "Master" database, then expand "Programmability", then "Stored Procedures", then "System Stored Procedures". You can then find "sys.sp_helpconstraint" and right click it and select "modify". Just be careful to not save any changes to it. Also, you can just use this system stored procedure on any table by using it like EXEC sp_helpconstraint YourTableNameHere.

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

    I use the following query to check for an existing constraint before I create it.

    IF (NOT EXISTS(SELECT 1 FROM sysconstraints WHERE OBJECT_NAME(constid) = 'UX_CONSTRAINT_NAME' AND OBJECT_NAME(id) = 'TABLE_NAME')) BEGIN
    ...
    END
    

    This queries for the constraint by name targeting a given table name. Hope this helps.

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

    Are you looking at something like this, below is tested in SQL Server 2005

    SELECT * FROM sys.check_constraints WHERE 
    object_id = OBJECT_ID(N'[dbo].[CK_accounts]') AND 
    parent_object_id = OBJECT_ID(N'[dbo]. [accounts]')
    
    0 讨论(0)
  • 2020-11-29 15:48

    INFORMATION_SCHEMA is your friend. It has all kinds of views that show all kinds of schema information. Check your system views. You will find you have three views dealing with constraints, one being CHECK_CONSTRAINTS.

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