sql server - Finding targeted columns in an FK -
if have pair of sql tables, 1 of these tables has pk on 1 column , unique index on (maybe guid , id or other unique key).
the other table has fk unique index, not pk. there way can query information schema find column targeted fk?
if right, want use metadatabase:
select k_table = fk.table_name, fk_column = cu.column_name, pk_table = pk.table_name, pk_column = pt.column_name, constraint_name = c.constraint_name information_schema.referential_constraints c inner join information_schema.table_constraints fk on c.constraint_name = fk.constraint_name inner join information_schema.table_constraints pk on c.unique_constraint_name = pk.constraint_name inner join information_schema.key_column_usage cu on c.constraint_name = cu.constraint_name inner join ( select i1.table_name, i2.column_name information_schema.table_constraints i1 inner join information_schema.key_column_usage i2 on i1.constraint_name = i2.constraint_name i1.constraint_type = 'primary key' ) pt on pt.table_name = pk.table_name
Comments
Post a Comment