<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic How to list foreign key and its reference column in an existing SQL Server query in Talend Studio</title>
    <link>https://community.qlik.com/t5/Talend-Studio/How-to-list-foreign-key-and-its-reference-column-in-an-existing/m-p/2271036#M48757</link>
    <description>&lt;DIV class="votecell post-layout--left"&gt;
 &lt;DIV class="js-voting-container grid fd-column ai-stretch gs4 fc-black-200"&gt;
  &amp;nbsp;
 &lt;/DIV&gt;
&lt;/DIV&gt;
&lt;DIV class="postcell post-layout--right"&gt;
 &lt;DIV class="post-text"&gt;
  &lt;P&gt;I am using the following query to list the table constraint,&lt;/P&gt;
  &lt;P&gt;however, I am having hard time in getting a&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;list of foreign keys and its references (columns)&lt;/STRONG&gt;.&lt;/P&gt;
  &lt;P&gt;&amp;nbsp;&lt;/P&gt;
  &lt;P&gt;Can anyone please advise me how I can modified this code so that I could get a list of foreign keys(column) and its references (columns).&lt;/P&gt;
  &lt;P&gt;**I will need to use this code, so a modified version of this code is much appreciated.&lt;/P&gt;
  &lt;PRE&gt;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 &amp;lt;&amp;gt; 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
order by table_view, constraint_type, constraint_name&lt;/PRE&gt;
  &lt;P&gt;&lt;A href="https://dataedo.com/kb/query/sql-server/list-all-table-constraints" target="_blank" rel="nofollow noopener noreferrer noopener noreferrer"&gt;Source: Dataedo's List all table constraint&lt;/A&gt;&lt;/P&gt;
  &lt;P&gt;&amp;nbsp;&lt;/P&gt;
  &lt;P&gt;Please let me know if anything is unclear.&lt;/P&gt;
  &lt;P&gt;Thank you so much&lt;/P&gt;
  &lt;P&gt;&amp;nbsp;&lt;/P&gt;
 &lt;/DIV&gt;
&lt;/DIV&gt;</description>
    <pubDate>Sat, 16 Nov 2024 02:51:40 GMT</pubDate>
    <dc:creator>Anonymous</dc:creator>
    <dc:date>2024-11-16T02:51:40Z</dc:date>
    <item>
      <title>How to list foreign key and its reference column in an existing SQL Server query</title>
      <link>https://community.qlik.com/t5/Talend-Studio/How-to-list-foreign-key-and-its-reference-column-in-an-existing/m-p/2271036#M48757</link>
      <description>&lt;DIV class="votecell post-layout--left"&gt;
 &lt;DIV class="js-voting-container grid fd-column ai-stretch gs4 fc-black-200"&gt;
  &amp;nbsp;
 &lt;/DIV&gt;
&lt;/DIV&gt;
&lt;DIV class="postcell post-layout--right"&gt;
 &lt;DIV class="post-text"&gt;
  &lt;P&gt;I am using the following query to list the table constraint,&lt;/P&gt;
  &lt;P&gt;however, I am having hard time in getting a&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;list of foreign keys and its references (columns)&lt;/STRONG&gt;.&lt;/P&gt;
  &lt;P&gt;&amp;nbsp;&lt;/P&gt;
  &lt;P&gt;Can anyone please advise me how I can modified this code so that I could get a list of foreign keys(column) and its references (columns).&lt;/P&gt;
  &lt;P&gt;**I will need to use this code, so a modified version of this code is much appreciated.&lt;/P&gt;
  &lt;PRE&gt;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 &amp;lt;&amp;gt; 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
order by table_view, constraint_type, constraint_name&lt;/PRE&gt;
  &lt;P&gt;&lt;A href="https://dataedo.com/kb/query/sql-server/list-all-table-constraints" target="_blank" rel="nofollow noopener noreferrer noopener noreferrer"&gt;Source: Dataedo's List all table constraint&lt;/A&gt;&lt;/P&gt;
  &lt;P&gt;&amp;nbsp;&lt;/P&gt;
  &lt;P&gt;Please let me know if anything is unclear.&lt;/P&gt;
  &lt;P&gt;Thank you so much&lt;/P&gt;
  &lt;P&gt;&amp;nbsp;&lt;/P&gt;
 &lt;/DIV&gt;
&lt;/DIV&gt;</description>
      <pubDate>Sat, 16 Nov 2024 02:51:40 GMT</pubDate>
      <guid>https://community.qlik.com/t5/Talend-Studio/How-to-list-foreign-key-and-its-reference-column-in-an-existing/m-p/2271036#M48757</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2024-11-16T02:51:40Z</dc:date>
    </item>
  </channel>
</rss>

