Advertisement
aliGo

Foreign keys

Nov 13th, 2019
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. select kcu.table_schema || '.' || kcu.table_name as foreign_table,
  2.        '>-' as rel,
  3.        rel_kcu.table_schema || '.' || rel_kcu.table_name as primary_table,
  4.        kcu.ordinal_position as no,
  5.        kcu.column_name as fk_column,
  6.        '=' as join,
  7.        rel_kcu.column_name as pk_column,
  8.        kcu.constraint_name
  9. from information_schema.table_constraints tco
  10.     join information_schema.key_column_usage kcu
  11.               on tco.constraint_schema = kcu.constraint_schema
  12.               and tco.constraint_name = kcu.constraint_name
  13.     join information_schema.referential_constraints rco
  14.               on tco.constraint_schema = rco.constraint_schema
  15.               and tco.constraint_name = rco.constraint_name
  16.     join information_schema.key_column_usage rel_kcu
  17.               on rco.unique_constraint_schema = rel_kcu.constraint_schema
  18.               and rco.unique_constraint_name = rel_kcu.constraint_name
  19.               and kcu.ordinal_position = rel_kcu.ordinal_position
  20. where tco.constraint_type = 'FOREIGN KEY'
  21.     and  rel_kcu.table_name='pim_position_role'
  22. order by kcu.table_schema,
  23.          kcu.table_name,
  24.          kcu.ordinal_position;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement