How to get list of Tables without Primary Key Constraint in SQL Server Database - SQL Server / T-SQL Tutorial Part 58

Scenario:

You are working as SQL Server developer for software development company. You are asked to provided list of Tables from a Database which do not have Primary Key Constraints created on them.


Solution:

There are different ways to get list of tables without primary key constraints in sql server database. In below query we are going to use system views to get List of tables in a database without Primary Key Constraints.

Select
   Table_CataLog as DatabaseName,
   Table_Schema as TableSchema,
   Table_Name as TableName      
from
   information_schema.tables T     
where
   Not Exists(
      Select
         1 
      from
         information_Schema.Table_Constraints C     
      where
         Constraint_Type='PRIMARY KEY'     
         and C.Table_Name=T.Table_Name     
         and C.Table_Schema=T.Table_Schema
   )     
   and Table_Type='BASE TABLE'



I execute above query on one of my database and got list of tables which do not have Primary Key Constraint created on them.
How to get list of tables without Primary Key Constraints in SQL Server Database - SQL Server / TSQL Tutorial


Video Demo: How to get list of tables without Primary Key Constraint in SQL Server

1 comment:

  1. Now it's difficult to create something completely new, innovative, so a good option is to use existing technologies for your own purposes. I decided to learn more about NFT, because millions of people are investing in NFT, and trading platforms like OpenSea are becoming more and more in demand. After reading this article https://4irelabs.com/articles/how-to-build-an-nft-marketplace-like-opensea/ I'm thinking about building a marketplace and I'm pretty sure it will be profitable.

    ReplyDelete