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 | 19 Comments
19 Responses
Leave a Reply Cancel reply
free community tool
About Pinal Dave
Pinal Dave is a Microsoft Technology Evangelist (Database and BI). He has written over 2000 articles on the subject on his blog at http://blog.sqlauthority.com. Along with 8+ years of hands on experience he holds a Masters of Science degree and a number of certifications, including MCTS, MCDBA and MCAD (.NET). He is co-author of three SQL Server books - SQL Server Programming, SQL Wait Stats and SQL Server Interview Questions and Answers. Prior to joining Microsoft he was awarded Microsoft MVP award for three continuous years for his contribution in community.
Follow @pinaldave Send +Pinal Dave an email at pinal@sqlauthority.com
-
Blog Stats
- 36,690,339 (36 Million+)
Next Office Hours
Books I Authored
SQLAuthority Links
Subscribe to Newsletter
My Homepage
Windows Live Blog
--------------------
Top Downloads
PDF Downloads
Script Downloads
Script Bank
Favorite Scripts
All Scripts - 1
All Scripts - 2
All Scripts - 3
Top Articles
Best Articles
Favorite Articles - 1
Favorite Articles - 2
--------------------
> SQL Interview Q & A <
SQL Coding Standards
SQL FAQ Download
--------------------
Jobs @ SQLAuthority
About Nupur Dave
Nupur Dave loves technology simply because it makes life more convenient. She is devoted to technology because it touches our heart makes our daily lives easier. Among the many technological programs she uses and embraces Windows Live most because she can do lots of things with ease – from photo management to movies; business emails to personal social media connections.
Disclaimer
This is a personal weblog. The opinions expressed here represent my own and not those of my employer. For accuracy and official reference refer to MSDN/ TechNet/ BOL. My employer do not endorse any tools, applications, books, or concepts mentioned on the blog. I have documented my personal experience on this blog.
Categories
- About Me (145)
- Best Practices (143)
- Business Intelligence (36)
- CodeProject (10)
- Data Warehousing (49)
- Database (321)
- DBA (137)
- DMV (13)
- Joes 2 Pros (47)
- MVP (147)
- PASS (14)
- Readers Contribution (114)
- Readers Question (126)
- SharePoint (7)
- Software Development (69)
- SQL Add-On (99)
- SQL Azure (15)
- SQL Backup and Restore (79)
- SQL BOL (11)
- SQL Coding Standards (21)
- SQL Constraint and Keys (57)
- SQL Cursor (28)
- SQL Data Storage (59)
- SQL DateTime (47)
- SQL DMV (23)
- SQL Documentation (299)
- SQL Download (310)
- SQL Error Messages (161)
- SQL Function (161)
- SQL Humor (29)
- SQL in Sixty Seconds (1)
- SQL Index (155)
- SQL Interview Questions and Answers (139)
- SQL Joins (77)
- SQL Milestone (25)
- SQL Optimization (152)
- SQL PASS (20)
- SQL Performance (340)
- SQL Puzzle (94)
- SQL Security (127)
- SQL Server DBCC (42)
- SQL Server Management Studio (44)
- SQL Service Pack (13)
- SQL Stored Procedure (116)
- SQL String (26)
- SQL System Table (61)
- SQL Trigger (24)
- SQL User Group (57)
- SQL Utility (153)
- SQL View (26)
- SQL Wait Stats (41)
- SQL Wait Types (42)
- SQL White Papers (67)
- SQL XML (12)
- SQLAuthority (632)
- SQL Training (19)
- SQLAuthority Author Visit (141)
- SQLAuthority Book Review (39)
- SQLAuthority News (577)
- SQLAuthority Website Review (42)
- SQLServer (227)
- Tech (1587)
- Pinal Dave (1574)
- SQL Scripts (860)
- Technology (2032)
- PostADay (453)
- SQL (2032)
- SQL Authority (2032)
- SQL Query (2031)
- SQL Server (2032)
- SQL Tips and Tricks (2032)
- T SQL (2032)
- Video (5)
Top 3 Commenters










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 [...]
For sql server 2000:
use databasename;
select table_catalog,table_schema,table_name from information_schema.tables where
objectproperty(object_id(table_name),’TableHasPrimaryKey’)=0
Thanks Pinal,
thanks.. But i got some table names with a prefix ‘conflict_’. Any idea on this?
sir… iam not getting…
my requirement is getting tables list which were created by me in oracle DB
i posted this query
SELECT * FROM SYS.TABLES WHERE NAME=’USERNAME’;
is this right or any other query i have to type..plz ..send me ur reply..
THANK YOU
@raman
The name COLUMN there is the TABLE name, if you are looking for the SCHEMA, try: SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = ”;