Home > SQL server DBA administration > Below are the query to display the list of tables having PK(Primary key) & without PK

Below are the query to display the list of tables having PK(Primary key) & without PK


1. To display tables name & primary Key Name with having primary key:

select A.[name] as ‘table Name’, B.[name] as ‘PK Name’

from

(select [name],ID from Sysobjects where id in(select Parent_obj from Sysobjects where xtype=’PK’))A, (Select [name],parent_obj from Sysobjects where xtype=’PK’)B where A.id=Parent_obj

2. To display Tables without having PK:

select [Name] as ‘table Name’  from Sysobjects where id not in(select

Parent_obj from Sysobjects where xtype=’PK’) and xtype=’U’

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: