Find Tables with No Primary Key – SQL Server

Sometime back I had done a post called Find all tables in a database without a primary key where I used the INFORMATION_SCHEMA.TABLES.

Megan commented asking how to rewrite the same query using sys.objects. Well here it is!

USE NerdDinner;
name as [TableName], create_date
FROM sys.objects
WHERE type = 'U'
AND OBJECTPROPERTY([object_id], 'TableHasPrimaryKey') = 0;


No Primary Key

If you do not get any results, then all your tables in your database have primary keys

You may also want to check the following related posts written some time ago:

Find Primary Key of a SQL Server Table

List all the Non-Clustered Indexes in a SQL Server Database

Find out all the Primary Key and Foreign Key Constraints in a table

About The Author

Suprotim Agarwal, MCSD, MCAD, MCDBA, MCSE, is the founder of DotNetCurry, DNC Magazine for Developers, SQLServerCurry and DevCurry. He has also authored a couple of books 51 Recipes using jQuery with ASP.NET Controls and a new one recently at The Absolutely Awesome jQuery CookBook.

Suprotim has received the prestigous Microsoft MVP award for nine times in a row now. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that represents premium web sites and digital publications comprising of Professional web, windows, mobile and cloud developers, technical managers, and architects.

Get in touch with him on Twitter @suprotimagarwal, LinkedIn or befriend him on Facebook


Jamorama said...

Hi, The above articles is very impressive, and I really enjoyed reading your blog and points that you expressed. I love to come back on a regular basis, pl. post more on the subject. Thanks.

Rocket Spanish said...

Thanks for providing such a nice post.
I liked your post and will definately link your blog in my website.
keep updating yourself.