Sonntag, 22. Mai 2011

Get tables without primary key

Sometimes it is useful to get all tables which has no primary key set.

With this short script you can get tables with a missing primary key even on SQL Server 2000.

USE myDatabase 
GO 
SELECT 
so.NAME 
,so.CRDATE 
FROM sysobjects so 
WHERE 
    so.xtype = 'U' 
AND so.NAME NOT IN ( 
    SELECT 
    so.NAME 
    FROM sysobjects so 
    inner join sysindexes si ON 
        so.ID = si.ID 
    WHERE 
        so.xtype = 'U' 
    AND si.indid = 1 
    ) 
ORDER BY NAME

1 Kommentar:

  1. Thanks Christian
    Nice script for MS SQL. Amost identical to Oracle ones I often use. Just a question will it show table without primary keys and unique keys?

    -- Kirill Loifman, dadbm.com

    AntwortenLöschen