How to get all tables which have primary key in SQL Server?


Sometimes we need to get all the tables that have primary key constraints. There are many ways to get this information.


Select
       ISTC.Table_Catalog as BD_Name,
       ISTC.Table_Schema AS TBL_SCHEMA,
       ISTC.Table_Name AS TBL_NAME,
       ISCCU.Column_Name AS COL_NAME,
       ISTC.Constraint_Name AS CONSTRAINT_NAME
From
       information_Schema.Table_Constraints ISTC 
INNER JOIN
       Information_Schema.constraint_column_usage ISCCU 
       on ISTC.Constraint_Name=ISCCU.Constraint_Name 
       AND ISTC.Table_Name=ISCCU.Table_Name 
where
       Constraint_Type='PRIMARY KEY'




This one is the best query to find out the tables, which have a primary key because information schema provides more information, and all the data can be retrieved with the filtered filter.

Related Posts

What is the Use of isNaN Function in JavaScript? A Comprehensive Explanation for Effective Input Validation

In the world of JavaScript, input validation is a critical aspect of ensuring that user-provided data is processed correctly. One indispensa...