This article is half-done without your Comment! *** Please share your thoughts via Comment ***
In this post, I am sharing a script to find all the tables without having a Primary Key in a database of SQL Server.
This script is very important because Primary Key is a one of a major part of the table. I never recommended like creating a table without Primary Key.
Using below script, I found all the tables that do not have a primary key and which are created by other database development team, so I was finding and adding missing Primary Keys.
Below are two different scripts:
1 2 3 4 5 6 7 8 9 10 11 |
SELECT SCHEMA_NAME(schema_id) AS SchemaName ,Name AS TableName FROM sys.objects WHERE [type]='U' AND object_id NOT IN ( SELECT parent_object_id FROM sys.objects WHERE [type]='PK' ) GO |
1 2 3 4 5 6 |
SELECT SCHEMA_NAME(schema_id) AS SchemaName ,Name AS TableName FROM sys.tables WHERE OBJECTPROPERTY(object_id,'TableHasPrimaryKey') = 0 GO |