Get Primary key – Foreign key relations table in sql server 2005

Why it is required?

The main purpose of this is finding related constraint table from database.

Check below Query.

–get table list with constraint(primary and foreign key)

select * from information_schema.constraint_column_usage

–get table list with foreign key constraint

select * from information_schema.referential_constraints

–get the relation

select

tblAll.table_name as PrimaryTableName,

tblAll.column_name as PrimaryTableColumn,

tblFK.table_name as ForeignKeyTable,

tblFK.column_name as ForeignKeyColumn

from information_schema.constraint_column_usage tblAll

inner join information_schema.referential_constraints tblAllFK on tblAllFK.unique_constraint_name = tblAll.constraint_name

inner join information_schema.constraint_column_usage tblFK on tblAllFK.constraint_name=tblFK.constraint_name

Thnx

6 responses to “Get Primary key – Foreign key relations table in sql server 2005”

  1. ALpesh Acharya Avatar
    ALpesh Acharya

    nice 1………i hv chk this query…

  2. DotNetURL Avatar

    Hi,

    Nice blog..publish you new content url at http://www.dotneturl.com and get reader and back link form us for free.

  3. Logician Avatar
    Logician

    thx patriwala,

    You realy save my time by using you queries you mentioned above.

    have great time.
    bye

  4. nzc.gnt Avatar
    nzc.gnt

    Thank u …..good one

  5. Pablo Avatar
    Pablo

    Thanks for you query

  6. Godwin Avatar
    Godwin

    Thanks a Lot.. Very useful blog.. :):)

Leave a Reply

Discover more from The Engineering Behind Enterprise AI

Subscribe now to keep reading and get access to the full archive.

Continue reading