How can I list all foreign keys referencing a given table in SQL Server?

后端 未结 26 2683
梦毁少年i
梦毁少年i 2020-11-22 07:13

I need to remove a highly referenced table in a SQL Server database. How can I get a list of all the foreign key constraints I will need to remove in order to drop the tabl

相关标签:
26条回答
  • 2020-11-22 07:43
    SELECT
    OBJECT_NAME(parent_object_id) 'Parent table',
    c.NAME 'Parent column name',
    OBJECT_NAME(referenced_object_id) 'Referenced table',
    cref.NAME 'Referenced column name'
    FROM 
    sys.foreign_key_columns fkc 
    INNER JOIN 
    sys.columns c 
       ON fkc.parent_column_id = c.column_id 
          AND fkc.parent_object_id = c.object_id
    INNER JOIN 
    sys.columns cref 
       ON fkc.referenced_column_id = cref.column_id 
          AND fkc.referenced_object_id = cref.object_id  where   OBJECT_NAME(parent_object_id) = 'tablename'
    

    If you want to get the foreign key relation of all the tables exclude the where clause else write your tablename instead of tablename

    0 讨论(0)
  • 2020-11-22 07:44

    This gets any foreign key that involves the chosen table. *Assumes a _FIRSTABLENAME_SECONDTABLENAME format.

     declare @tablename as varchar(MAX)
     SET @tablename = 'yourtablename'
     SELECT name
     FROM YOURDATABASE.sys.objects
     WHERE type_desc = 'FOREIGN_KEY_CONSTRAINT' and (name LIKE '%_' + @tablename + 'empdb_%' or name LIKE '%_' + @tablename )
    

    This is a more general form:

     SELECT name
     FROM YOURDATABASE_PROD.sys.objects
     WHERE type_desc = 'FOREIGN_KEY_CONSTRAINT' and name LIKE '%' + @tablename + '%' and
     name NOT LIKE '[a-zA-Z0-9]' + @tablename + '%' and name NOT LIKE '%' + @tablename + '[a-zA-Z0-9]' 
    
    0 讨论(0)
  • 2020-11-22 07:45

    This gives you:

    • The FK itself itself
    • Schema that the FK belongs to
    • The "referencing table" or the table that has the FK
    • The "referencing column" or the column inside referencing table that points to the FK
    • The "referenced table" or the table that has the key column that your FK is pointing to
    • The "referenced column" or the column that is the key that your FK is pointing to

    Code below:

    SELECT  obj.name AS FK_NAME,
        sch.name AS [schema_name],
        tab1.name AS [table],
        col1.name AS [column],
        tab2.name AS [referenced_table],
        col2.name AS [referenced_column]
    FROM sys.foreign_key_columns fkc
    INNER JOIN sys.objects obj
        ON obj.object_id = fkc.constraint_object_id
    INNER JOIN sys.tables tab1
        ON tab1.object_id = fkc.parent_object_id
    INNER JOIN sys.schemas sch
        ON tab1.schema_id = sch.schema_id
    INNER JOIN sys.columns col1
        ON col1.column_id = parent_column_id AND col1.object_id = tab1.object_id
    INNER JOIN sys.tables tab2
        ON tab2.object_id = fkc.referenced_object_id
    INNER JOIN sys.columns col2
        ON col2.column_id = referenced_column_id AND col2.object_id = tab2.object_id
    
    0 讨论(0)
  • 2020-11-22 07:45

    You should also mind the references to other objects.

    If the table was highly referenced by other tables than it’s probably also highly referenced by other objects such as views, stored procedures, functions and more.

    I’d really recommend GUI tool such as ‘view dependencies’ dialog in SSMS or free tool like ApexSQL Search for this because searching for dependencies in other objects can be error prone if you want to do it only with SQL.

    If SQL is the only option you could try doing it like this.

    select O.name as [Object_Name], C.text as [Object_Definition]
    from sys.syscomments C
    inner join sys.all_objects O ON C.id = O.object_id
    where C.text like '%table_name%'
    
    0 讨论(0)
  • 2020-11-22 07:46

    Also try.

    EXEC sp_fkeys 'tableName', 'schemaName'
    

    with sp_fkeys you may filter the result by not only pk table name and schema but also with fk table name and schema. link

    0 讨论(0)
  • 2020-11-22 07:51

    Working off of what @Gishu did I was able to produce and use the following SQL in SQL Server 2005

    SELECT t.name AS TableWithForeignKey, fk.constraint_column_id AS FK_PartNo, 
           c.name AS ForeignKeyColumn, o.name AS FK_Name 
      FROM sys.foreign_key_columns AS fk
           INNER JOIN sys.tables AS t ON fk.parent_object_id = t.object_id
           INNER JOIN sys.columns AS c ON fk.parent_object_id = c.object_id 
                                      AND fk.parent_column_id = c.column_id
           INNER JOIN sys.objects AS o ON fk.constraint_object_id = o.object_id
      WHERE fk.referenced_object_id = (SELECT object_id FROM sys.tables 
                                            WHERE name = 'TableOthersForeignKeyInto')
      ORDER BY TableWithForeignKey, FK_PartNo;
    

    Which Displays the tables, columns and Foreign Key names all in 1 query.

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