Friday, 15 March 2013

tsql - Query to find all FK constraints, and their referenced table columns -


I have a large SQL 2012 database (100 tables), in which I need to find all the obstacles where the reversion action Set

  select name, delete_referential_action_desc, select, sys.foreign_keys from Update_referential_action_desc WHERE update_referential_action_desc = 'CASCADE' or delete_referential_action_desc = 'CASCADE';  

This provides a useful list:

  name delete_referential_action_desc update_referential_action_desc ------------------ - ------------------------------------------------ - FK_name001 Cascade Cascade FK_name002 Cascade No_action FK_NN003 Cascade No_Action  

However, it is possible to expand the code, so that each barrier contains the corresponding table name and column name, e.g.

name delete_referential_action_desc update_referential_action_desc ParentTable ParentCol ChildTable ChildCall --------------------------------- --- ----------------------------------------------- --- -------------------------------------- FK_name001 Cascade CASAD Table 1 Call 1 Table 2 Call 2

I have tried various edits that drag values ​​from but do not know how it works, or that it is also the right approach

Update

Please note that I do not want Do not to ALTER anything Change a schema I may just would like to see a list so I manually further action. I do not have confidence or experience to remove major points.

Two days after experiment

  select C.CONSTRAINT_NAME, PK.TABLE_NAME, CCU.COLUMN_NAME, FK.TABLE_NAME, CU.COLUMN_NAME, C.UPDATE_RULE, INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS Join C.DELETE_RULE from within C.CONSTRAINT_NAME = FK.CONSTRAINT_NAME join INFORMATION_SCHEMA.TABLE_CONSTRAINTS FK INFORMATION_SCHEMA.TABLE_CONSTRAINTS P At C.UNIQUE_CONSTRAINT_NAME = PK.CONSTRAINT_NAME Join INFORMATION_SCHEMA C.CONSTRAINT_NAME = CU.CONSTRAINT_NAME at INKEY_COLUMN_USAGE CU INNER JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE PK at CCU. Order PK by CONSTRAINT_NAME = CCU.CONSTRAINT_NAME WHERE ((C.UPDATE_RULE = 'CASCADE') or (CDELTULE = 'Cascade')) and (FK .CONSTRAINT_TYPE = 'foreign key'). TABLE_NAME, F.K. TABLE_NAME;  

Praise be done for digital deities ... (!)


No comments:

Post a Comment