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

前端 未结 15 744
再見小時候
再見小時候 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:51

    In Object Explorer, expand the table, and expand the Keys:

    0 讨论(0)
  • 2020-12-04 05:54
    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-12-04 05:59

    --The following may give you more of what you're looking for:

    create Procedure spShowRelationShips 
    ( 
        @Table varchar(250) = null,
        @RelatedTable varchar(250) = null
    )
    as
    begin
        if @Table is null and @RelatedTable is null
            select  object_name(k.constraint_object_id) ForeginKeyName, 
                    object_name(k.Parent_Object_id) TableName, 
                    object_name(k.referenced_object_id) RelatedTable, 
                    c.Name RelatedColumnName,  
                    object_name(rc.object_id) + '.' + rc.name RelatedKeyField
            from sys.foreign_key_columns k
            left join sys.columns c on object_name(c.object_id) = object_name(k.Parent_Object_id) and c.column_id = k.parent_column_id
            left join sys.columns rc on object_name(rc.object_id) = object_name(k.referenced_object_id) and rc.column_id = k.referenced_column_id
            order by 2,3
    
        if @Table is not null and @RelatedTable is null
            select  object_name(k.constraint_object_id) ForeginKeyName, 
                    object_name(k.Parent_Object_id) TableName, 
                    object_name(k.referenced_object_id) RelatedTable, 
                    c.Name RelatedColumnName,  
                    object_name(rc.object_id) + '.' + rc.name RelatedKeyField
            from sys.foreign_key_columns k
            left join sys.columns c on object_name(c.object_id) = object_name(k.Parent_Object_id) and c.column_id = k.parent_column_id
            left join sys.columns rc on object_name(rc.object_id) = object_name(k.referenced_object_id) and rc.column_id = k.referenced_column_id
            where object_name(k.Parent_Object_id) =@Table
            order by 2,3
    
        if @Table is null and @RelatedTable is not null
            select  object_name(k.constraint_object_id) ForeginKeyName, 
                    object_name(k.Parent_Object_id) TableName, 
                    object_name(k.referenced_object_id) RelatedTable, 
                    c.Name RelatedColumnName,  
                    object_name(rc.object_id) + '.' + rc.name RelatedKeyField
            from sys.foreign_key_columns k
            left join sys.columns c on object_name(c.object_id) = object_name(k.Parent_Object_id) and c.column_id = k.parent_column_id
            left join sys.columns rc on object_name(rc.object_id) = object_name(k.referenced_object_id) and rc.column_id = k.referenced_column_id
            where object_name(k.referenced_object_id) =@RelatedTable
            order by 2,3
    
    
    
    end
    
    0 讨论(0)
  • 2020-12-04 06:00

    The easiest way to get Primary Key and Foreign Key for a table is:

    /*  Get primary key and foreign key for a table */
    USE DatabaseName;
    
    SELECT CONSTRAINT_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
    WHERE CONSTRAINT_NAME LIKE 'PK%' AND
    TABLE_NAME = 'TableName'
    
    SELECT CONSTRAINT_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
    WHERE CONSTRAINT_NAME LIKE 'FK%' AND
    TABLE_NAME = 'TableName'
    
    0 讨论(0)
  • 2020-12-04 06:01

    Here it is:

    SELECT 
       OBJECT_NAME(f.parent_object_id) TableName,
       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
    WHERE 
       OBJECT_NAME (f.referenced_object_id) = 'YourTableName'
    

    This way, you'll get the referencing table and column name.

    Edited to use sys.tables instead of generic sys.objects as per comment suggestion. Thanks, marc_s

    0 讨论(0)
  • 2020-12-04 06:02

    In SQL Server Management Studio you can just right click the table in the object explorer and select "View Dependencies". This would give a you a good starting point. It shows tables, views, and procedures that reference the table.

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