SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 primary keys & foreign keys
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

subbu_mareedu
Starting Member

India
10 Posts

Posted - 04/17/2014 :  13:05:47  Show Profile  Reply with Quote
how to find all primary key columns & foreign key columns in all database tables?

Regards
subbu

subbu

TG
Flowing Fount of Yak Knowledge

USA
6062 Posts

Posted - 04/17/2014 :  13:45:17  Show Profile  Reply with Quote
this information_schema view is one way:

[INFORMATION_SCHEMA].[CONSTRAINT_COLUMN_USAGE]

EDIT:
to limit the results to just the constraint types you mentioned then join in with table_constraints:

select c.table_name, c.constraint_name, c.constraint_type, u.column_name
from   information_schema.table_constraints c
join   information_schema.constraint_column_usage u on u.constraint_name = c.constraint_name
where  c.constraint_type in ('primary key', 'foreign key')
order by 1,3,2,4



Be One with the Optimizer
TG

Edited by - TG on 04/17/2014 14:03:26
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000