Please start any new threads on our new site at We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

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

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

Starting Member

11 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?



Flowing Fount of Yak Knowledge

6065 Posts

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


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

Edited by - TG on 04/17/2014 14:03:26
Go to Top of Page
  Previous Topic Topic Next 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.02 seconds. Powered By: Snitz Forums 2000