0

I am using the following query to list the table constraint(Primary Keys, Unique Keys, Default keys), however, I am having hard time in getting a list of foreign keys and its references (columns).

Can anyone please advise me how I can modified this code so that I could get a list of foreign keys and its references (columns) while keeping PK and UQ the same.

**I will need to use the code for my project, so I would appreciate if you could should me how to modified the query.

UPDATED Modified version : added query suggested by @Deepak

I replaced foreign key sub query (after first union all) with @Deepak's code, but there's an error

"select table_view,
    object_type, 
    constraint_type,
    constraint_name,
    details,
    fk_columns,
    pk_columns
from (
    select schema_name(t.schema_id) + '.' + t.[name] as table_view, 
        case when t.[type] = 'U' then 'Table'
            when t.[type] = 'V' then 'View'
            end as [object_type],
        case when c.[type] = 'PK' then 'Primary key'
            when c.[type] = 'UQ' then 'Unique constraint'
            when i.[type] = 1 then 'Unique clustered index'
            when i.type = 2 then 'Unique index'
            end as constraint_type, 
        isnull(c.[name], i.[name]) as constraint_name,
        substring(column_names, 1, len(column_names)-1) as [details]
    from sys.objects t
        left outer join sys.indexes i
            on t.object_id = i.object_id
        left outer join sys.key_constraints c
            on i.object_id = c.parent_object_id 
            and i.index_id = c.unique_index_id
       cross apply (select col.[name] + ', '
                        from sys.index_columns ic
                            inner join sys.columns col
                                on ic.object_id = col.object_id
                                and ic.column_id = col.column_id
                        where ic.object_id = t.object_id
                            and ic.index_id = i.index_id
                                order by col.column_id
                                for xml path ('') ) D (column_names)
    where is_unique = 1
    and t.is_ms_shipped <> 1
    union all 
    SELECT schema_name(fk_tab.schema_id) + '.' + fk_tab.name as foreign_table,
        'Table',
        'Foreign key',
        fk.name as fk_constraint_name,
        schema_name(pk_tab.schema_id) + '.' + pk_tab.name,
        col.[name] as [fk_columns],
        col2.[name] as [pk_columns],
        schema_name(objects.schema_id) + '.' + objects.[name] + '.' + col.[name] + ' --> ' + schema_name(pk_tab.schema_id) + '.' + pk_tab.name + '.' + col2.[name] as details
    FROM
        sys.objects objects
        INNER JOIN sys.tables fk_tab on objects.object_id = fk_tab.object_id
        INNER JOIN sys.foreign_keys fk on fk_tab.object_id = fk.parent_object_id
        INNER JOIN sys.tables pk_tab on pk_tab.object_id = fk.referenced_object_id
        INNER JOIN sys.foreign_key_columns fk_c on fk_c.parent_object_id = fk.parent_object_id
                       and fk_c.constraint_object_id = fk.object_id
            inner join sys.columns col
                            on fk_c.parent_object_id = col.object_id
                            and fk_c.parent_column_id = col.column_id
        left join sys.columns col2
                            on fk_c.referenced_object_id = col2.object_id
                            and fk_c.referenced_column_id = col2.column_id
    union all
    select schema_name(t.schema_id) + '.' + t.[name],
        'Table',
        'Check constraint',
        con.[name] as constraint_name,
        con.[definition]
    from sys.check_constraints con
        left outer join sys.objects t
            on con.parent_object_id = t.object_id
        left outer join sys.all_columns col
            on con.parent_column_id = col.column_id
            and con.parent_object_id = col.object_id
    union all
    select schema_name(t.schema_id) + '.' + t.[name],
        'Table',
        'Default constraint',
        con.[name],
        col.[name] + ' = ' + con.[definition]
    from sys.default_constraints con
        left outer join sys.objects t
            on con.parent_object_id = t.object_id
        left outer join sys.all_columns col
            on con.parent_column_id = col.column_id
            and con.parent_object_id = col.object_id) a
order by table_view, constraint_type, constraint_name"

Here's the error message:

SQLServerException: All queries combined using a UNION, INTERSECT or EXCEPT operator must have an equal number of expressions in their target lists.

My original Code :refer from Source: Dataedo's List all table constraint

"select table_view,
    object_type, 
    constraint_type,
    constraint_name,
    details
from (
    select schema_name(t.schema_id) + '.' + t.[name] as table_view, 
        case when t.[type] = 'U' then 'Table'
            when t.[type] = 'V' then 'View'
            end as [object_type],
        case when c.[type] = 'PK' then 'Primary key'
            when c.[type] = 'UQ' then 'Unique constraint'
            when i.[type] = 1 then 'Unique clustered index'
            when i.type = 2 then 'Unique index'
            end as constraint_type, 
        isnull(c.[name], i.[name]) as constraint_name,
        substring(column_names, 1, len(column_names)-1) as [details]
    from sys.objects t
        left outer join sys.indexes i
            on t.object_id = i.object_id
        left outer join sys.key_constraints c
            on i.object_id = c.parent_object_id 
            and i.index_id = c.unique_index_id
       cross apply (select col.[name] + ', '
                        from sys.index_columns ic
                            inner join sys.columns col
                                on ic.object_id = col.object_id
                                and ic.column_id = col.column_id
                        where ic.object_id = t.object_id
                            and ic.index_id = i.index_id
                                order by col.column_id
                                for xml path ('') ) D (column_names)
    where is_unique = 1
    and t.is_ms_shipped <> 1
    union all 
    select schema_name(fk_tab.schema_id) + '.' + fk_tab.name as foreign_table,
        'Table',
        'Foreign key',
        fk.name as fk_constraint_name,
        schema_name(pk_tab.schema_id) + '.' + pk_tab.name
    from sys.foreign_keys fk
        inner join sys.tables fk_tab
            on fk_tab.object_id = fk.parent_object_id
        inner join sys.tables pk_tab
            on pk_tab.object_id = fk.referenced_object_id
        inner join sys.foreign_key_columns fk_cols
            on fk_cols.constraint_object_id = fk.object_id
    union all
    select schema_name(t.schema_id) + '.' + t.[name],
        'Table',
        'Check constraint',
        con.[name] as constraint_name,
        con.[definition]
    from sys.check_constraints con
        left outer join sys.objects t
            on con.parent_object_id = t.object_id
        left outer join sys.all_columns col
            on con.parent_column_id = col.column_id
            and con.parent_object_id = col.object_id
    union all
    select schema_name(t.schema_id) + '.' + t.[name],
        'Table',
        'Default constraint',
        con.[name],
        col.[name] + ' = ' + con.[definition]
    from sys.default_constraints con
        left outer join sys.objects t
            on con.parent_object_id = t.object_id
        left outer join sys.all_columns col
            on con.parent_column_id = col.column_id
            and con.parent_object_id = col.object_id) a
where table_view like 'dbo.AREA%'
order by table_view, constraint_type, constraint_name"

Please let me know if anything is unclear.

Thank you so muchh

hans1125
  • 357
  • 8
  • 15
  • Does this answer your question? [How can I list all foreign keys referencing a given table in SQL Server?](https://stackoverflow.com/questions/483193/how-can-i-list-all-foreign-keys-referencing-a-given-table-in-sql-server) – Suraj Kumar Mar 27 '20 at 10:59
  • @SurajKumar I had checked that previously, but I'm new to SQL so I don't know how I can incorporate the query in the link provided to my current query. Can you kindly show me changes I need to make to it work? Thank you – hans1125 Mar 27 '20 at 11:05
  • @Deepak I have updated as suggested, please have a look and let me know how I could correct the code. Thank you very much!! – hans1125 Mar 27 '20 at 15:57

1 Answers1

1

Here is the complete sql

select table_view,
    object_type, 
    constraint_type,
    constraint_name,
    details,
    fk_columns,
    refrenceTable,
    pk_columns
from (
    select schema_name(t.schema_id) + '.' + t.[name] as table_view, 
        case when t.[type] = 'U' then 'Table'
            when t.[type] = 'V' then 'View'
            end as [object_type],
        case when c.[type] = 'PK' then 'Primary key'
            when c.[type] = 'UQ' then 'Unique constraint'
            when i.[type] = 1 then 'Unique clustered index'
            when i.type = 2 then 'Unique index'
            end as constraint_type, 
        isnull(c.[name], i.[name]) as constraint_name,
        substring(column_names, 1, len(column_names)-1) as [details],
        '' fk_columns,
        '' refrenceTable,
        '' pk_columns
    from sys.objects t
        left outer join sys.indexes i
            on t.object_id = i.object_id
        left outer join sys.key_constraints c
            on i.object_id = c.parent_object_id 
            and i.index_id = c.unique_index_id
       cross apply (select col.[name] + ', '
                        from sys.index_columns ic
                            inner join sys.columns col
                                on ic.object_id = col.object_id
                                and ic.column_id = col.column_id
                        where ic.object_id = t.object_id
                            and ic.index_id = i.index_id
                                order by col.column_id
                                for xml path ('') ) D (column_names)
    where is_unique = 1
    and t.is_ms_shipped <> 1
    union all 
    SELECT schema_name(fk_tab.schema_id) + '.' + fk_tab.name as foreign_table,
        'Table',
        'Foreign key',
        fk.name as fk_constraint_name,
        schema_name(objects.schema_id) + '.' + objects.[name] + '.' + col.[name] + ' --> ' + schema_name(pk_tab.schema_id) + '.' + pk_tab.name + '.' + col2.[name] as details,
        col.[name] as [fk_columns],
        schema_name(pk_tab.schema_id) + '.' + pk_tab.name,
        col2.[name] as [pk_columns]
    FROM
        sys.objects objects
        INNER JOIN sys.tables fk_tab on objects.object_id = fk_tab.object_id
        INNER JOIN sys.foreign_keys fk on fk_tab.object_id = fk.parent_object_id
        INNER JOIN sys.tables pk_tab on pk_tab.object_id = fk.referenced_object_id
        INNER JOIN sys.foreign_key_columns fk_c on fk_c.parent_object_id = fk.parent_object_id
                       and fk_c.constraint_object_id = fk.object_id
            inner join sys.columns col
                            on fk_c.parent_object_id = col.object_id
                            and fk_c.parent_column_id = col.column_id
        left join sys.columns col2
                            on fk_c.referenced_object_id = col2.object_id
                            and fk_c.referenced_column_id = col2.column_id
    union all
    select schema_name(t.schema_id) + '.' + t.[name],
        'Table',
        'Check constraint',
        con.[name] as constraint_name,
        con.[definition],
         '' as [fk_columns],
        '' refrenceTable,
        '' as [pk_columns]
    from sys.check_constraints con
        left outer join sys.objects t
            on con.parent_object_id = t.object_id
        left outer join sys.all_columns col
            on con.parent_column_id = col.column_id
            and con.parent_object_id = col.object_id
    union all
    select schema_name(t.schema_id) + '.' + t.[name],
        'Table',
        'Default constraint',
        con.[name],
        col.[name] + ' = ' + con.[definition],
         '' as [fk_columns],
        '' refrenceTable,
        '' as [pk_columns]
    from sys.default_constraints con
        left outer join sys.objects t
            on con.parent_object_id = t.object_id
        left outer join sys.all_columns col
            on con.parent_column_id = col.column_id
            and con.parent_object_id = col.object_id) a
order by table_view, constraint_type, constraint_name
Deepak
  • 548
  • 3
  • 15
  • it is partially.. However, I'm looking for a way to add the query above into my current code as I will need the other constraint in my project. I tried to added the query you provided me to my current code but 'The multi-part identifier "col.name" could not be bound' error occurred. Can you help guide me how I could add your query correctly into my quote? I updated what I tried added in my post, please have a look at it. – hans1125 Mar 27 '20 at 13:02
  • Thanks! I tried using the code you shared and it works perfectly for foreign keys and its reference, however, I could not list primary key, unique key, default constraint anymore. Is there a way to keep PK and UQ list, while getting foreign keys and its reference? Thank you very much! – hans1125 Mar 27 '20 at 15:09
  • I have updated code in my answer if you will replace your foreign key sub query after first union all in your original code. This will return all keys. Hope this help... – Deepak Mar 27 '20 at 15:20
  • I tried to replace foreign key sub query (after first union all) with your code, but still it didn't work . . . "All queries combined using a UNION, INTERSECT or EXCEPT operator must have an equal number of expressions in their target lists." . I updated what I tried in my post, please have a look at it. Thank you very much!! – hans1125 Mar 27 '20 at 15:55
  • 1
    I have updated complete running sql code, you can adjust columns as per your requirement. – Deepak Mar 28 '20 at 05:05