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

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 primary keys & foreign keys

Author  Topic 

subbu_mareedu
Starting Member

11 Posts

Posted - 2014-04-17 : 13:05:47
how to find all primary key columns & foreign key columns in all database tables?

Regards
subbu

subbu

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2014-04-17 : 13:45:17
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
Go to Top of Page
   

- Advertisement -