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 

source: http://blog.sqlauthority.com/2006/11/01/sql-server-query-to-display-foreign-key-relationships-and-name-of-the-constraint-for-each-table-in-database/


Comments

Popular posts from this blog

c# - Operator '==' incompatible with operand types 'Guid' and 'Guid' using DynamicExpression.ParseLambda<T, bool> -