Technical Article

How to get Foreign keys

,

If you ever have been put in a situation where your database has a lot of objects and some of those do not have primary keys, trying to find feels like needle in the haystack.  I have modified the script contributed by Mr.Tandrei and Mr.Rahul sharma.  The following script gives all the tables that do not have primary keys.

select name from sysobjects where type='U' and name not in(
SELECT  Distinct table_name 
FROM information_schema.key_column_usage 
WHERE constraint_catalog = db_name()
AND lower(constraint_name)  like 'pk%'
AND table_name not like 'tbl_core_%' 
        AND table_name not like 'db_version_1%'
        AND table_name not like 'dtproperties' 
AND LOWER(constraint_name)  like 'pk%')
order by name

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating