Monday, March 18, 2013

Tables without Primary Key in Database


Some times you are analyzing the database to check which tables don't have primary keys then  in that case follwing query will help you to find out the tables:

Below SQL will give you the list of table in you Database not having Primary Keys.
SELECT SCHEMA_NAME(schema_id) AS SchemaName,name AS TableName
FROM sys.tables
WHERE OBJECTPROPERTY(OBJECT_ID,'TableHasPrimaryKey') = 0
ORDER BY SchemaName, TableName;

Happy Coding!! 

No comments:

Post a Comment