SQL SERVER - 2005 - List Tables in Database Without Primary Key
August 7, 2007 by pinaldave
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://www.SQLAuthority.com), BOL
Posted in Database, Pinal Dave, SQL, SQL Authority, SQL Constraint and Keys, SQL Documentation, SQL Download, SQL Error Messages, SQL Joins, SQL Performance, SQL Query, SQL Scripts, SQL Security, SQL Server, SQL Server DBCC, SQL Tips and Tricks, T SQL, Technology | 14 Comments
Leave a Reply
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….