Found the below query helpful
1. To get all the foreign key constraints defined in the database.
SELECT RC.CONSTRAINT_NAME
, RCU.TABLE_NAME REFERENCING_TABLE
, RCU.COLUMN_NAME REFERENCING_COLUMN
, RCU1.TABLE_NAME REFERENCED_TABLE
, RCU1.COLUMN_NAME REFERENCED_COLUMN
FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS RC
JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE RCU ON RC.CONSTRAINT_CATALOG = RCU.CONSTRAINT_CATALOG AND RC.CONSTRAINT_NAME = RCU.CONSTRAINT_NAME
JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE RCU1 ON RC.UNIQUE_CONSTRAINT_CATALOG = RCU1.CONSTRAINT_CATALOG AND RC.UNIQUE_CONSTRAINT_NAME = RCU1.CONSTRAINT_NAME
2. To get all primary keys defined in the database
SELECT T.TABLE_NAME, T.CONSTRAINT_NAME, K.COLUMN_NAME, K.ORDINAL_POSITION
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS T
JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE K ON T.CONSTRAINT_NAME = K.CONSTRAINT_NAME
WHERE T.CONSTRAINT_TYPE = 'PRIMARY KEY'
ORDER BY T.TABLE_NAME, K.ORDINAL_POSITION
No comments:
Post a Comment