728x90

SELECT DISTINCT
  C.TABLE_NAME
, C.CONSTRAINT_TYPE
, C.CONSTRAINT_NAME
, COL.COLUMN_NAME
, ( CASE
 
WHEN C.CONSTRAINT_TYPE = 'PRIMARY KEY' THEN 1
  WHEN C.CONSTRAINT_TYPE = 'FOREIGN KEY' THEN 2
  ELSE 3
 END
) AS DISPLAY
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS C
 INNER JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE AS COL ON C.CONSTRAINT_NAME = COL.CONSTRAINT_NAME
WHERE
 C.TABLE_NAME NOT IN ('dtproperties')
ORDER BY C.TABLE_NAME, DISPLAY

+ Recent posts