In this blog we will learn how to retrieve the list of Primary Key and Foreign Key for a particular table and for the whole database.
List of Primary Key and Foreign Key for Whole Database:
Query:
- SELECT
-
- DISTINCT
-
- Constraint_Name AS [Constraint],
-
- Table_Schema AS [Schema],
-
- Table_Name AS [TableName] FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
-
- GO
Output:
List of Primary Key and Foreign Key for a particular table:
Query:
- SELECT
-
- DISTINCT
-
- Constraint_Name AS [Constraint],
-
- Table_Schema AS [Schema],
-
- Table_Name AS [TableName] FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
-
- WHERE INFORMATION_SCHEMA.KEY_COLUMN_USAGE.TABLE_NAME='tblCompany_General_Info'
-
- GO
Output: