select fkc.constraint_column_id, cop.name col_fk, cor.name col_pk, fkk.name foreign_key_name, ret.name pk_table, pat.name fk_table, fkc.parent_column_id col_pk_id, fkc.referenced_column_id col_fk_id from sys.foreign_key_columns fkc join sys.foreign_keys fkk on fkc.constraint_object_id = fkk.object_id join sys.objects pat on fkc.parent_object_id = pat.object_id join sys.objects ret on fkc.referenced_object_id = ret.object_id join sys.columns cop on cop.object_id = pat.object_id and cop.column_id = fkc.parent_column_id join sys.columns cor on cor.object_id = ret.object_id and cor.column_id = fkc.referenced_column_id where -- fkk.name = 'tq84_fk' -- pat.name = 'tq84_f' -- Child table anme ret.name = 'tq84_p' -- Parent table name order by fkc.constraint_column_id;
create table tq84_p ( col_1 numeric, pk_1 numeric, col_2 numeric, pk_2 numeric, col_3 numeric, constraint tq84_pk primary key (pk_1, pk_2) );
create table tq84_f ( col_1 numeric, fk_1 numeric, col_2 numeric, fk_2 numeric, col_3 numeric, constraint tq84_fk foreign key (fk_2, fk_1) references tq84_p );