Wednesday, August 14, 2013

List all tables in the SQL Database without Primary Key

Last week I was facing some issues with the application which I have been developing. Query was not working properly when somebody loaded huge test data in a table. when I checked the table I found that primary key was not created for that table even though it was not mentioned the database schema we have prepared. So I thought I will run a query to find out all the tables in the database which doesn’t have primary key.

Since I already created the query to find out all the tables without primary key in MS SQL database of course I have to share that with you also…

SELECT [schema_id] AS [Schema Name],[name] AS [Table Name], [type_desc] as [Table Type]
FROM sys.tables
WHERE OBJECTPROPERTY(OBJECT_ID,'TableHasPrimaryKey') = 0
 

Output of above query will be,

Table Names without primary key

No comments: