Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL - Finding FK details of column

Tags:

sql

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            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.

like image 411
Ash Burlaczenko Avatar asked Dec 10 '25 20:12

Ash Burlaczenko


2 Answers

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.

like image 178
Ash Burlaczenko Avatar answered Dec 12 '25 08:12

Ash Burlaczenko


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'
like image 37
Bohemian Avatar answered Dec 12 '25 10:12

Bohemian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!