Email: Password:       Forgot Password 
    .netCodeSG
A Saarsha Group Online Community for dot net codes group like C#, Asp.NET, VB.NET, Sharepoint, JavaScript, JQuery, Ajax, SQL, WCF, WPF.
 
TECHNOLOGIES:
 

SQL Server - Know Primary Key Constraint And Foreign Key Constraint

Article:
Viewed:  493 
Posted On:  4/1/2016 6:28:06 PM 
How we can know Primary Key Constraint And Foreign Key Constraint in SQL Server ? 

 

If you want to know all primary key & Foreign key constraint in your SQL Server data base or a particular data table then use below SQL Statement.

 

Know Primary Key Constraint in Entire SQL Data Base:

1.png

Image 1.

SELECT I.name AS PrimaryKeyName,
OBJECT_NAME(IC.OBJECT_ID) AS TableName,
COL_NAME(IC.OBJECT_ID,IC.column_id) AS PrimaryKeyColumnName
FROM SYS.INDEXES AS I
INNER JOIN SYS.INDEX_COLUMNS AS IC
ON I.OBJECT_ID = IC.OBJECT_ID
AND I.index_id = IC.index_id
WHERE I.is_primary_key = 1 ORDER BY OBJECT_NAME(IC.OBJECT_ID)
 

If you want to know Primary key constraint on any particular table then use below statement:

2.png

Image 2.

 

To know Foreign Key Constraint use below SQL Statement:

SELECT FR.name AS ForeignKeyName,
OBJECT_NAME(FR.parent_object_id) AS TableName,
COL_NAME(FC.parent_object_id,
FC.parent_column_id) AS ColumnName,
OBJECT_NAME (FR.referenced_object_id) AS ReferenceTableName,
COL_NAME(fc.referenced_object_id,
fc.referenced_column_id) AS ReferenceColumnName
FROM SYS.FOREIGN_KEYS AS FR
INNER JOIN SYS.FOREIGN_KEY_COLUMNS AS FC
ON FR.OBJECT_ID = FC.constraint_object_id ORDER BY OBJECT_NAME(FR.parent_object_id)
 

 

  Comment:
         HOME   |   Submit Article   |   Contact Us   |   About Us   |   Terms & Condition   |   Advertise With us