How can I drop a table if there is a foreign key constraint in SQL Server?

后端 未结 8 1317
陌清茗
陌清茗 2020-12-15 17:43

I have the following:

DROP TABLE [dbo].[ExtraUserInformation];
DROP TABLE [dbo].[UserProfile];
DROP TABLE [dbo].[webpages_Membership];
DROP TABLE [dbo].[webp         


        
相关标签:
8条回答
  • 2020-12-15 17:50

    You must drop the constraint before you can drop the table.Other wise its rule violation. How to get foreign key relationships see this old question. SQL DROP TABLE foreign key constraint

    0 讨论(0)
  • 2020-12-15 17:54

    You have to drop the constraint before drop your table.

    You can use those queries to find all FKs in your table and find the FKs in the tables in which your table is used.

    Declare @SchemaName VarChar(200) = 'Your Schema name'
    Declare @TableName VarChar(200) = 'Your Table Name'
    
    -- Find FK in This table.
    SELECT 
        ' IF  EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = 
    OBJECT_ID(N''' + 
          '[' + OBJECT_SCHEMA_NAME(FK.parent_object_id) + '].[' + FK.name + ']'
    + ''') AND parent_object_id = OBJECT_ID(N''' + 
          '[' + OBJECT_SCHEMA_NAME(FK.parent_object_id) + '].[' + 
    OBJECT_NAME(FK.parent_object_id) + ']' + ''')) ' +
    
        'ALTER TABLE ' +  OBJECT_SCHEMA_NAME(FK.parent_object_id) +
        '.[' + OBJECT_NAME(FK.parent_object_id) + 
        '] DROP CONSTRAINT ' + FK.name
        , S.name , O.name, OBJECT_NAME(FK.parent_object_id)
    FROM sys.foreign_keys AS FK
    INNER JOIN Sys.objects As O 
      ON (O.object_id = FK.parent_object_id )
    INNER JOIN SYS.schemas AS S 
      ON (O.schema_id = S.schema_id)  
    WHERE 
          O.name = @TableName
          And S.name = @SchemaName
    
    
    -- Find the FKs in the tables in which this table is used
      SELECT 
        ' IF  EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id =   
          OBJECT_ID(N''' + 
          '[' + OBJECT_SCHEMA_NAME(FK.parent_object_id) + '].[' + FK.name + ']'
      + ''') AND parent_object_id = OBJECT_ID(N''' + 
          '[' + OBJECT_SCHEMA_NAME(FK.parent_object_id) + '].[' + 
     OBJECT_NAME(FK.parent_object_id) + ']' + ''')) ' +
    
        ' ALTER TABLE ' +  OBJECT_SCHEMA_NAME(FK.parent_object_id) +
        '.[' + OBJECT_NAME(FK.parent_object_id) + 
        '] DROP CONSTRAINT ' + FK.name
        , S.name , O.name, OBJECT_NAME(FK.parent_object_id)
    FROM sys.foreign_keys AS FK
    INNER JOIN Sys.objects As O 
      ON (O.object_id = FK.referenced_object_id )
    INNER JOIN SYS.schemas AS S 
      ON (O.schema_id = S.schema_id)  
    WHERE 
          O.name = @TableName
          And S.name = @SchemaName 
    
    0 讨论(0)
  • 2020-12-15 17:58
        --Find and drop the constraints
    
        DECLARE @dynamicSQL VARCHAR(MAX)
        DECLARE MY_CURSOR CURSOR 
    
        LOCAL STATIC READ_ONLY FORWARD_ONLY 
        FOR
            SELECT dynamicSQL = 'ALTER TABLE [' +  OBJECT_SCHEMA_NAME(parent_object_id) + '].[' + OBJECT_NAME(parent_object_id) + '] DROP CONSTRAINT [' + name + ']'
            FROM sys.foreign_keys
            WHERE object_name(referenced_object_id)  in ('table1', 'table2', 'table3')
        OPEN MY_CURSOR
        FETCH NEXT FROM MY_CURSOR INTO @dynamicSQL
        WHILE @@FETCH_STATUS = 0
        BEGIN
    
            PRINT @dynamicSQL
            EXEC (@dynamicSQL)
    
            FETCH NEXT FROM MY_CURSOR INTO @dynamicSQL
        END
        CLOSE MY_CURSOR
        DEALLOCATE MY_CURSOR
    
    
        -- Drop tables
        DROP 'table1'
        DROP 'table2'
        DROP 'table3'
    
    0 讨论(0)
  • 2020-12-15 18:05

    1-firstly, drop the foreign key constraint after that drop the tables.

    2-you can drop all foreign key via executing the following query:

    DECLARE @SQL varchar(4000)=''
    SELECT @SQL = 
    @SQL + 'ALTER TABLE ' + s.name+'.'+t.name + ' DROP CONSTRAINT [' + RTRIM(f.name) +'];' + CHAR(13)
    FROM sys.Tables t
    INNER JOIN sys.foreign_keys f ON f.parent_object_id = t.object_id
    INNER JOIN sys.schemas     s ON s.schema_id = f.schema_id
    
    --EXEC (@SQL)
    
    PRINT @SQL
    

    if you execute the printed results @SQL, the foreign keys will be dropped.

    0 讨论(0)
  • 2020-12-15 18:07

    The Best Answer to dropping the table containing foreign constraints is :

    • Step 1 : Drop the Primary key of the table.
    • Step 2 : Now it will prompt whether to delete all the foreign references or not.
    • Step 3 : Delete the table.
    0 讨论(0)
  • 2020-12-15 18:11

    To drop a table if there is a foreign key constraint in MySQL Server?

    Run the sql query:

    SET FOREIGN_KEY_CHECKS = 0; DROP TABLE table_name

    Hope it helps!

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