开发者

SQL - Finding FK details of column

I have some SQL that gets a few details about a table.

SELECT Column_Name, Is_Nullable, Data_Type, Character_Maximum_Length
FROM Information_Schema.Columns
WHERE Table_Name='GenSchool'

This is working ok so far and returns a row per column in the table. What I want however, is for it to also return some Foreign Key details too. For example, GenSchool has a column SchoolType which has a FK to GenSchoolType.Code.

As well as the columns selected above, I need the query to return the FK table and column name of linked table or NULL where the column doesn't have a FK.

This is returned from the query above.

Code开发者_C百科            NO  nvarchar    10
CodeDescription YES nvarchar    80
Deleted         NO  bit         NULL
Type            NO  nvarchar    20

And I'd like it to return something like

Code            NO  nvarchar    10      NULL             NULL
CodeDescription YES nvarchar    80      NULL             NULL
Deleted         NO  bit         NULL    NULL             NULL
Type            NO  nvarchar    20      GenSchoolType    Code

I've been trying for ages using inner joins on sys tables but I'm not getting anywhere. If you need me to show what I've tried I can.

Thank you in advance.


So I finally worked out the solution.

with fks as ( 
    select fk.name AS 'FKName', o_p.name AS 'ParentNameTable', 
        c_p.name AS 'ParentNameColumn', o_r.name AS 'ReferencedNameTable', 
        c_r.name AS 'ReferencedNameColumn', fkc.* 
    from sys.foreign_key_columns fkc 
    inner join sys.foreign_keys fk on fk.object_id = fkc.constraint_object_id 
    inner join sys.objects o_p on o_p.object_id = fkc.parent_object_id 
    inner join sys.objects o_r on o_r.object_id = fkc.referenced_object_id 
    inner join sys.columns c_p on c_p.object_id = fkc.parent_object_id and c_p.column_id = fkc.parent_column_id 
    inner join sys.columns c_r on c_r.object_id = fkc.referenced_object_id and c_r.column_id = fkc.referenced_column_id 
) 
select c.Table_Name, c.Column_Name, c.Is_Nullable, c.Data_Type,
    c.Character_Maximum_Length, COLUMNPROPERTY(OBJECT_ID(@table), 
    c.Column_Name,'IsIdentity') AS 'Is_Identity', 
    fks.FKName, fks.ReferencedNameTable, fks.ReferencedNameColumn 
from information_schema.columns c 
left join fks on c.Table_Name = fks.ParentNameTable and c.Column_Name = fks.ParentNameColumn 
where c.table_name = @table

This returns a row per column in the database with the following data

  1. Table Name
  2. Column Name
  3. Nullable
  4. Data Type
  5. Max Length
  6. Identity
  7. FK Name
  8. Table Referenced
  9. Column Referenced

Feel free to use this if it meets your needs.


Join to information_schema.REFERENTIAL_CONSTRAINTS:

SELECT Column_Name, Is_Nullable, Data_Type, Character_Maximum_Length, rc.REFERENCED_TABLE_NAME
FROM Information_Schema.Columns c
left join information_schema.REFERENTIAL_CONSTRAINTS rc on rc.TABLE_NAME = c.TABLE_NAME
WHERE Table_Name='GenSchool'


Here is a query from the MSDN page related to your query.

SELECT TABLE_NAME, CONSTRAINT_NAME , COLUMN_NAME , ORDINAL_POSITION
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE    
WHERE CONSTRAINT_NAME IN (    
    SELECT CONSTRAINT_NAME 
    FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS) 
ORDER BY TABLE_NAME, ORDINAL_POSITION


did you try sp_fkeys

exec sp_fkeys <TableName>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜