Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 All tables in a DB with/without a PK

Author  Topic 

igorblackbelt
Constraint Violating Yak Guru

407 Posts

Posted - 2008-05-15 : 09:07:23
Is there a way to check (Any sys table) what tables in my DB have and/or don't have a PK?


---
http://www.ssisdude.blogspot.com/

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2008-05-15 : 09:27:32
[code]
-- to find tables without PK
select t.table_name
from information_schema.tables t left join information_schema.TABLE_CONSTRAINTS tc on t.table_name = tc.table_name and tc.Constraint_Type = 'Primary key'
where t.table_type = 'Base Table' and tc.table_name is null[/code]

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

mathiyazhagan.sekar@gmail
Starting Member

11 Posts

Posted - 2008-05-15 : 09:36:52
Hi ,
I think there is no systable . But , we can get this using Sysobjects and Information schema .The Query to display all tables with their primary key is :
SELECT information_schema.tables.table_name,name as PK FROM sysobjects
INNER JOIN information_schema.tables
ON object_name(parent_obj) = information_schema.tables.table_name
WHERE xtype='PK'

Cheers,
Mathi
Go to Top of Page
   

- Advertisement -