I have this schema:
create table "cat" (
"name" varchar(64),
"owner" varchar(64),
primary key ("name", "owner")
);
create table "comment" (
"name" varchar(45),
"owner" varchar(45),
"id" uuid,
"comment" text,
primary key ("id"),
foreign key ("name", "owner") references "cat"("name", "owner")
);
I want to get a list of foreign keys from table "comment" to "cat", so I use:
SELECT
tc.constraint_name, tc.table_name, kcu.column_name,
ccu.table_name AS foreign_table_name,
ccu.column_name AS foreign_column_name
FROM
information_schema.table_constraints AS tc
JOIN information_schema.key_column_usage AS kcu
ON tc.constraint_name = kcu.constraint_name
JOIN information_schema.constraint_column_usage AS ccu
ON ccu.constraint_name = tc.constraint_name
WHERE constraint_type = 'FOREIGN KEY'
;
and got almost what I want:
constraint_name | table_name | column_name | foreign_table_name | foreign_column_name
-------------------+------------+-------------+--------------------+---------------------
comment_name_fkey | comment | owner | cat | name
comment_name_fkey | comment | name | cat | name
comment_name_fkey | comment | owner | cat | owner
comment_name_fkey | comment | name | cat | owner
But there are row 1 and row 4, which I would like to eliminate in the result, because it does not mirror the dependencies of column. How can I do it in Postgresql?