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 2008 Forums
 Transact-SQL (2008)
 find Missing relationships?

Author  Topic 

learntsql

524 Posts

Posted - 2011-01-10 : 00:31:48
Hi All,
I have to find out the missing Primary keys and foreign keys for respected tables for whole database.
Please tell me what is the best process to follow?
TIA.

RobertKaucher
Posting Yak Master

169 Posts

Posted - 2011-01-10 : 10:10:27
LearnTSQL, are you saying you need to find tables that do not have primary keys? If all you are looking for is the heap tables you can query sys.indexes and sys.objects. This example will show the tables and the schema in which they reside that are "heaps".


USE MyDB
GO

SELECT SCHEMA_NAME(o.schema_id) AS "Schema Name"
,OBJECT_NAME(i.object_id ) AS "Table Name"

FROM sys.indexes i
INNER JOIN sys.objects o ON i.object_id = o.object_id

WHERE i.type_desc = 'HEAP'


You may also want to see :
http://msdn.microsoft.com/en-us/library/ms175068.aspx
http://msdn.microsoft.com/en-us/library/ms186301.aspx

===
http://www.ElementalSQL.com/
Go to Top of Page

learntsql

524 Posts

Posted - 2011-01-11 : 05:53:48
Thanks RobertKaucher,
Yea I want to find out missing primary keys and bit more intelligently IS IT possible to find out the respected foreign keys.
Please guide me.
TIA.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-01-11 : 11:19:43
see

http://www.mssqltips.com/tip.asp?tip=1237

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -