2014-04-24 12 views

Odpowiedz

8

Możesz wykopać te informacje w numerach information_schema.table_constraints i information_schema.constraint_column_usage, sprawdzając wiele wierszy więzów PRIMARY KEY w tabeli, coś w stylu;

SELECT col.table_name 
FROM information_schema.table_constraints tc 
JOIN information_schema.constraint_column_usage col 
    ON col.constraint_name = tc.constraint_name 
AND col.table_name = tc.table_name 
AND tc.constraint_type = 'PRIMARY KEY ' 
GROUP BY col.table_name 
HAVING COUNT(*) > 1 

An SQLfiddle to test with.

+0

Doskonały fragment, bardzo dziękuję! –

2

Musisz użyć tabeli lub tabeli kolumn schematu eigher. Tutaj jest to.

SELECT K.TABLE_CATALOG, 
    K.TABLE_NAME, 
    K.COLUMN_NAME, 
    K.ORDINAL_POSITION  FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE K 
    INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC 
    ON K.TABLE_CATALOG = TC.TABLE_CATALOG 
     AND K.TABLE_SCHEMA = TC.TABLE_SCHEMA 
     AND K.CONSTRAINT_NAME = TC.CONSTRAINT_NAME 
WHERE TC.CONSTRAINT_TYPE = 'PRIMARY KEY' 

zobacz link - Retrieving column information (composite key) in SQL

http://blog.sqlauthority.com/2007/09/04/sql-server-2005-find-tables-with-primary-key-constraint-in-database/