Click here to Skip to main content
15,882,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can I check whether a column PKey OR UNIQUE constraint using only one sql query in MSSQL??

If so please give the query please.
Posted

1 solution

If I'm understanding your request correctly, you are looking for a query that will help you to identify when a column is in a primary key or a unique index constraint. The query below will help you to identify those columns.

SQL
SELECT
	SCHEMA_NAME(tables.[schema_id]) AS [schema_name],
	tables.name,
	columns.name,
	indexes.name
FROM sys.tables
INNER JOIN sys.columns
	ON tables.[object_id] = columns.[object_id]
INNER JOIN sys.index_columns
	ON tables.[object_id] = index_columns.[object_id]
	AND columns.column_id = index_columns.column_id
INNER JOIN sys.indexes
	ON tables.[object_id] = indexes.[object_id]
	AND index_columns.index_id = indexes.index_id
WHERE
	indexes.is_unique = 1
 
Share this answer
 
Comments
RTK The Limited Edition 21-Jul-15 11:03am    
Is this possible without join keyword ??
virusstorm 21-Jul-15 11:12am    
Is there a reason why you can't use a "JOIN"?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900