Displays the foreign key relationships between database tables.
Source: Internet
Author: User
Select foreign_key = B. Name,
Foreign_key_table = C. Name,
Foreign_key_columns
= Isnull (Select name from syscolumns
Where id = C. ID and colid = A. fkey1 ),'')
+ Isnull (select ',' + name from syscolumns
Where id = C. ID and colid = A. fkey2 ),'')
+ Isnull (select ',' + name from syscolumns
Where id = C. ID and colid = A. fkey3 ),'')
+ Isnull (select ',' + name from syscolumns
Where id = C. ID and colid = A. fkey4 ),'')
+ Isnull (select ',' + name from syscolumns
Where id = C. ID and colid = A. fkey5 ),'')
+ Isnull (select ',' + name from syscolumns
Where id = C. ID and colid = A. fkey6 ),'')
+ Isnull (select ',' + name from syscolumns
Where id = C. ID and colid = A. fkey7 ),'')
+ Isnull (select ',' + name from syscolumns
Where id = C. ID and colid = A. fkey8 ),'')
+ Isnull (select ',' + name from syscolumns
Where id = C. ID and colid = A. fkey9 ),'')
+ Isnull (select ',' + name from syscolumns
Where id = C. ID and colid = A. fkey10 ),'')
+ Isnull (select ',' + name from syscolumns
Where id = C. ID and colid = A. fkey11 ),'')
+ Isnull (select ',' + name from syscolumns
Where id = C. ID and colid = A. fkey12 ),'')
+ Isnull (select ',' + name from syscolumns
Where id = C. ID and colid = A. fkey13 ),'')
+ Isnull (select ',' + name from syscolumns
Where id = C. ID and colid = A. fkey14 ),'')
+ Isnull (select ',' + name from syscolumns
Where id = C. ID and colid = A. fkey15 ),'')
+ Isnull (select ',' + name from syscolumns
Where id = C. ID and colid = A. fkey16 ),''),
Primary_key_table = D. Name,
Primary_key_columns
= Isnull (Select name from syscolumns
Where id = D. id and colid = A. rkey1 ),'')
+ Isnull (select ',' + name from syscolumns
Where id = D. id and colid = A. rkey2 ),'')
+ Isnull (select ',' + name from syscolumns
Where id = D. id and colid = A. rkey3 ),'')
+ Isnull (select ',' + name from syscolumns
Where id = D. id and colid = A. rkey4 ),'')
+ Isnull (select ',' + name from syscolumns
Where id = D. id and colid = A. rkey5 ),'')
+ Isnull (select ',' + name from syscolumns
Where id = D. id and colid = A. rkey6 ),'')
+ Isnull (select ',' + name from syscolumns
Where id = D. id and colid = A. rkey7 ),'')
+ Isnull (select ',' + name from syscolumns
Where id = D. id and colid = A. rkey8 ),'')
+ Isnull (select ',' + name from syscolumns
Where id = D. id and colid = A. rkey9 ),'')
+ Isnull (select ',' + name from syscolumns
Where id = D. id and colid = A. rkey10 ),'')
+ Isnull (select ',' + name from syscolumns
Where id = D. id and colid = A. rkey11 ),'')
+ Isnull (select ',' + name from syscolumns
Where id = D. id and colid = A. rkey12 ),'')
+ Isnull (select ',' + name from syscolumns
Where id = D. id and colid = A. rkey13 ),'')
+ Isnull (select ',' + name from syscolumns
Where id = D. id and colid = A. rkey14 ),'')
+ Isnull (select ',' + name from syscolumns
Where id = D. id and colid = A. rkey15 ),'')
+ Isnull (select ',' + name from syscolumns
Where id = D. id and colid = A. rkey16 ),''),
Cascade_update = case when (B. Status/8192) % 2 = 1 then 'y' else' end,
Cascade_delete = case when (B. Status/4096) % 2 = 1 then 'y' else' end
From sysreferences A, sysobjects B, sysobjects C, sysobjects d
Where B. parent_obj in (select ID from sysobjects
Where xtype = 'U' and name <> N 'dtproperties ')
And a. constid = B. ID
And a. fkeyid = C. ID
And a. rkeyid = D. id
Order by foreign_key_table, foreign_key_columns
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.