views:

30

answers:

2

Hello,

I'm working with SQL Server and trying to do a little "reflection," if you will. I've found the system view identity_columns, which contains all of the identity columns for all of my tables.

However, I need to be able to select information about primary keys that aren't identity columns. Is there a view that contains data about all primary keys and only primary keys? If not, how else can I get this data?

TIA,
Benjy

+5  A: 

This works for SQL Server 2005 and higher:

select OBJECT_SCHEMA_NAME(i.object_id), OBJECT_NAME(i.object_id), i.name
from sys.indexes i
where i.is_primary_key = 1
order by 1, 2, 3
devio
This is fantastic, thanks. Apparently I have to wait 9 minutes to accept your answer as correct, but I will.
benjy
A: 
SELECT name FROM sys.key_constraints WHERE type = 'PK';
SELECT name FROM sys.key_constraints WHERE type = 'UQ';
dportas