How can I find out what FOREIGN KEY constraint references a table in SQL Server?

前端 未结 15 743
再見小時候
再見小時候 2020-12-04 05:12

I am trying to drop a table but getting the following message:

Msg 3726, Level 16, State 1, Line 3
Could not drop object \'dbo.UserProfile\' bec

相关标签:
15条回答
  • 2020-12-04 05:42

    You could use this query to display Foreign key constaraints:

    SELECT
    K_Table = FK.TABLE_NAME,
    FK_Column = CU.COLUMN_NAME,
    PK_Table = PK.TABLE_NAME,
    PK_Column = PT.COLUMN_NAME,
    Constraint_Name = C.CONSTRAINT_NAME
    FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS C
    INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS FK ON C.CONSTRAINT_NAME = FK.CONSTRAINT_NAME
    INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS PK ON C.UNIQUE_CONSTRAINT_NAME = PK.CONSTRAINT_NAME
    INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE CU ON C.CONSTRAINT_NAME = CU.CONSTRAINT_NAME
    INNER JOIN (
    SELECT i1.TABLE_NAME, i2.COLUMN_NAME
    FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS i1
    INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE i2 ON i1.CONSTRAINT_NAME = i2.CONSTRAINT_NAME
    WHERE i1.CONSTRAINT_TYPE = 'PRIMARY KEY'
    ) PT ON PT.TABLE_NAME = PK.TABLE_NAME
    ---- optional:
    ORDER BY
    1,2,3,4
    WHERE PK.TABLE_NAME='YourTable'
    

    Taken from http://blog.sqlauthority.com/2006/11/01/sql-server-query-to-display-foreign-key-relationships-and-name-of-the-constraint-for-each-table-in-database/

    0 讨论(0)
  • 2020-12-04 05:43

    I found this answer quite simple and did the trick for what I needed: https://stackoverflow.com/a/12956348/652519

    A summary from the link, use this query:

    EXEC sp_fkeys 'TableName'
    

    Quick and simple. I was able to locate all the foreign key tables, respective columns and foreign key names of 15 tables pretty quickly.

    As @mdisibio noted below, here's a link to the documentation that details the different parameters that can be used: https://docs.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/sp-fkeys-transact-sql

    0 讨论(0)
  • 2020-12-04 05:44

    Try this

    SELECT
      object_name(parent_object_id) ParentTableName,
      object_name(referenced_object_id) RefTableName,
      name 
    FROM sys.foreign_keys
    WHERE parent_object_id = object_id('Tablename')
    
    0 讨论(0)
  • 2020-12-04 05:47

    I am using this script to find all details related to foreign key. I am using INFORMATION.SCHEMA. Below is a SQL Script:

    SELECT 
        ccu.table_name AS SourceTable
        ,ccu.constraint_name AS SourceConstraint
        ,ccu.column_name AS SourceColumn
        ,kcu.table_name AS TargetTable
        ,kcu.column_name AS TargetColumn
    FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE ccu
        INNER JOIN INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS rc
            ON ccu.CONSTRAINT_NAME = rc.CONSTRAINT_NAME 
        INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE kcu 
            ON kcu.CONSTRAINT_NAME = rc.UNIQUE_CONSTRAINT_NAME  
    ORDER BY ccu.table_name
    
    0 讨论(0)
  • 2020-12-04 05:48

    Here is the best way to find out Foreign Key Relationship in all Database.

    exec sp_helpconstraint 'Table Name'
    

    and one more way

    select * from INFORMATION_SCHEMA.KEY_COLUMN_USAGE where TABLE_NAME='Table Name'
    --and left(CONSTRAINT_NAME,2)='FK'(If you want single key)
    
    0 讨论(0)
  • 2020-12-04 05:49

    You can also return all the information about the Foreign Keys by adapating @LittleSweetSeas answer:

    SELECT 
       OBJECT_NAME(f.parent_object_id) ConsTable,
       OBJECT_NAME (f.referenced_object_id) refTable,
       COL_NAME(fc.parent_object_id,fc.parent_column_id) ColName
    FROM 
       sys.foreign_keys AS f
    INNER JOIN 
       sys.foreign_key_columns AS fc 
          ON f.OBJECT_ID = fc.constraint_object_id
    INNER JOIN 
       sys.tables t 
          ON t.OBJECT_ID = fc.referenced_object_id
    order by
    ConsTable
    
    0 讨论(0)
提交回复
热议问题