Wednesday, 23 January 2013

List all Primary and Foreign Key in a Table


List all Primary and Foreign Key in a Table


Method 1:

SELECT

DISTINCT

Constraint_Name AS [Constraint],

Table_Schema AS [Schema],

Table_Name AS [TableName]

FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE



Method 2:

SELECT OBJECT_NAME(OBJECT_ID) AS NameofConstraint,

SCHEMA_NAME(schema_id) AS SchemaName,

OBJECT_NAME(parent_object_id) AS TableName,

type_desc AS ConstraintType

FROM sys.objects

WHERE type_desc IN ('FOREIGN_KEY_CONSTRAINT','PRIMARY_KEY_CONSTRAINT')

No comments:

Post a Comment

Thank You for Your Comments. We will get back to you soon.

back to top