No SQL Server basta apenas executar esse comando.



SELECT tc.TABLE_NAME AS PrimaryKeyTable, tc.CONSTRAINT_NAME AS PrimaryKey, 
           COALESCE(rc1.CONSTRAINT_NAME, 'N/A' ) AS ForeignKey , 
           COALESCE(tc2.TABLE_NAME, 'N/A' ) AS ForeignKeyTable FROM
           INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc LEFT JOIN
           INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS rc1 ON 
           tc.CONSTRAINT_NAME = rc1.UNIQUE_CONSTRAINT_NAME LEFT JOIN
           INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc2 ON 
           tc2.CONSTRAINT_NAME = rc1.CONSTRAINT_NAME 
           WHERE TC.CONSTRAINT_TYPE = 'PRIMARY KEY' 
           ORDER BY tc.TABLE_NAME,tc.CONSTRAINT_NAME,rc1.CONSTRAINT_NAME



Anúncios