DotNet Friends

October 11, 2008

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

5 Comments »

  1. nice 1………i hv chk this query…

    Comment by ALpesh Acharya — November 3, 2008 @ 11:01 am

  2. Hi,

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

    Comment by DotNetURL — November 27, 2008 @ 7:02 am

  3. thx patriwala,

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

    have great time.
    bye

    Comment by Logician — December 12, 2008 @ 4:46 pm

  4. Thank u …..good one

    Comment by nzc.gnt — January 19, 2009 @ 10:37 am

  5. Thanks for you query

    Comment by Pablo — April 1, 2009 @ 1:26 am


RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.