This is very simple but effective script. It list all the table without primary keys.
USE DatabaseName;
GO
SELECT SCHEMA_NAME(schema_id) AS SchemaName,name AS TableName
FROM sys.tables
WHERE OBJECTPROPERTY(OBJECT_ID,'TableHasPrimaryKey') = 0
ORDER BY SchemaName, TableName;
GO
Reference : Pinal Dave (http://blog.SQLAuthority.com), BOL
SQL SERVER – 2005 – List Tables in Database Without Primary Key
August 7, 2007 by pinaldave
Posted in Database, Pinal Dave, SQL, SQL Authority, SQL Constraint and Keys, SQL Query, SQL Scripts, SQL Server, SQL System Table, SQL Tips and Tricks, T SQL, Technology | 15 Comments
15 Responses
Leave a Reply
-
About Pinal Dave
Pinalkumar Dave is Microsoft SQL Server MVP, Solid Quality Mentor and a prominent author of over 1000 SQL Server articles at SQLAuthority. He is a dynamic and proficient Principal Database Architect, Corporate Trainer and Project Manager specializing in SQL Server Programming with over 7 years of hands-on experience. He holds a degree in Masters of Science and has accomplished a number of certifications including MCDBA and MCAD (.NET). He has also been awarded Regional Mentor for PASS Asia.
-
Blog Stats
- 8,445,646 Readers
-
SQLAuthority Links

My Homepage
My Resume
My Other Blog
--------------------
Top Downloads
PDF Downloads
Script Downloads
Script Bank
Favorite Scripts
All Scripts - 1
All Scripts - 2
Top Articles
Best Articles
Favorite Articles - 1
Favorite Articles - 2
--------------------
SQL Interview Q & A
SQL Coding Standards
SQL FAQ Download
--------------------
Jobs @ SQLAuthority
Categories
- About Me (50)
- Best Practices (82)
- Business Intelligence (6)
- Data Warehousing (27)
- Database (257)
- DBA (121)
- DigiCorp (7)
- MVP (82)
- Poll (5)
- Readers Contribution (22)
- Readers Question (28)
- Software Development (62)
- SQL Add-On (88)
- SQL Backup and Restore (49)
- SQL BOL (8)
- SQL Coding Standards (21)
- SQL Constraint and Keys (49)
- SQL Cursor (30)
- SQL Data Storage (38)
- SQL DateTime (36)
- SQL Documentation (193)
- SQL Download (198)
- SQL Error Messages (116)
- SQL Function (104)
- SQL Humor (22)
- SQL Index (82)
- SQL Interview Questions and Answers (54)
- SQL Joins (61)
- SQL Optimization (51)
- SQL Performance (204)
- SQL Puzzle (18)
- SQL Security (115)
- SQL Server DBCC (42)
- SQL Server Management Studio (17)
- SQL Stored Procedure (97)
- SQL String (17)
- SQL System Table (27)
- SQL Trigger (27)
- SQL User Group (41)
- SQL Utility (116)
- SQL White Papers (8)
- SQLAuthority (309)
- SQLAuthority Author Visit (62)
- SQLAuthority Book Review (19)
- SQLAuthority News (279)
- SQLAuthority Website Review (24)
- SQLServer (43)
- Tech (776)
- Pinal Dave (765)
- SQL Scripts (490)
- Technology (1027)
- SQL (1027)
- SQL Authority (1027)
- SQL Query (1027)
- SQL Server (1027)
- SQL Tips and Tricks (1027)
- T SQL (1027)
-
Top Posts
- SQL Server Interview Questions and Answers Complete List Download
- SQL SERVER - 2008 - Interview Questions and Answers Complete List Download
- SQL SERVER - Insert Data From One Table to Another Table - INSERT INTO SELECT - SELECT INTO TABLE
- SQL SERVER - 2005 NorthWind Database or AdventureWorks Database - Samples Databases
- SQL SERVER - 2005 - Create Script to Copy Database Schema and All The Objects - Stored Procedure, Functions, Triggers, Tables, Views, Constraints and All Other Database Objects
- SQL SERVER - Insert Multiple Records Using One Insert Statement - Use of UNION ALL
- SQL SERVER - Import CSV File Into SQL Server Using Bulk Insert - Load Comma Delimited File Into SQL Server
- SQL SERVER - Retrieve Current Date Time in SQL Server CURRENT_TIMESTAMP, GETDATE(), {fn NOW()}
- SQL SERVER - Convert Text to Numbers (Integer) - CAST and CONVERT
- SQL SERVER - Shrinking Truncate Log File - Log Full
- SQL SERVER - Restore Database Backup using SQL Script (T-SQL)
- SQL Server Interview Questions and Answers - Introduction
-
Authors
-
pinaldave
- SQL SERVER – BLOB – Pointer to Image, Image in Database, FILESTREAM Storage
- SQLAuthority News – Big Thinkers – Robert Cain
- SQL SERVER – Standby Servers and Types of Standby Servers
- SQLAuthority News – Request SQLAuthority.com Stickers and SQL Server Cheat Sheet
- SQLAuthority News – Authors Visit – K-MUG TechEd Trivandrum on June 27, 2009
- SQLAuthority News – Book Review – Murach’s SQL Server 2008 for Developers
- SQLAuthority News – Authors Visit – DotNet Buzz Delhi TechEd Delhi on July 11, 2009
- SQL SERVER – Languages for BI – MDX, DMX, XMLA
- SQLAuthority News – FIX : Error : HP OfficeJet Scanning and Printing Gray or Pink Shades
- SQL SERVER – Disk Partition Alignment Best Practices
-
Archives
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
- June 2007
- May 2007
- April 2007
- March 2007
- February 2007
- January 2007
- December 2006
- November 2006
Pages
Category Cloud
About Me Best Practices Database DBA MVP Pinal Dave Software Development SQL SQL Add-On SQL Authority SQLAuthority Author Visit SQLAuthority News SQL Documentation SQL Download SQL Error Messages SQL Function SQL Index SQL Interview Questions and Answers SQL Joins SQL Optimization SQL Performance SQL Query SQL Scripts SQL Security SQL Server SQL Stored Procedure SQL Tips and Tricks SQL Utility Technology T SQL



Just a thought: if you use INFORMATION_SCHEMA.TABLES instead of sys.tables your script will be working in sql server 2000 as well.
Hi Plamen,
That is correct. However, sys.tables is standard SQL Server 2005 syntax.
Regards,
Pinal Dave (http://www.SQLAuthority.com)
Hi Pinal,
What will be the equivalent syntax for SQL Server 2000?
You can replace sys.tables with systables.
I have stop using SQL Server 2000 so please verify with your local DBA.
Regards,
Pinal Dave ( http://www.SQLAuthority.com )
very helpful, thanks.
I have an issue with a SQL Server 2005 database that created by importing some tables from a SQL Server 2000 database and creating other tables in SQL 2005.
The issue is that the imported tables have ansi nulls set off and the other tables have ansi nulls set on. Besides the confusion of what ansi nulls does, this prevents me from using indexed views.
The sys.tables view shows a uses_ansi_nulls column but ad hoc updates are not permitted on the view.
Is there a reasonable way to change the ansi nulls setting programatically.
I want to see the table of Humanresources.Employee.i.e.Select*from humanresources.Employee
this is just what i needed. thanks
We were in a meeting working implementing Replication and had a hitch concerning tables without primary keys. We needed to get a list quick for our reporting team to review and after a quick Google we found and used your code…thanks for posting it…saved us some time.
Hi
Thanks a lot this is what i was searching for 2 days
Regards
Avinash
Thanks Pinal
Right on! this is the answer to my question, you solved it
i want to retrive records of table.
n i want to pass this table name as an argument,
so how to do this….
I am trying to run a web application on my local computer. The application is in ASP.Net. The manual of the application says, only thing I need to modify in the application is the following line in the web.config file:
It says the above line will depend on what Server is installed on my computer.
I have SQL Server 2005 installed. Can you tell me what should be the correct syntax of the above line. Also where should I save the database ‘acdb’ on the computer and how do I link or view this table in the SQL server.
This is just what i needed. thanks..
It helped me a lot….
[...] SQL SERVER – 2005 – List Tables in Database Without Primary Key [...]