Duplicate foreign key constraints - reasons or against

I just came across a table in production that has 4 foreign keys. Two of the restrictions are exact duplicates of the other two.

t

fk1(a_id) references a(id)
fk2(a_id) references a(id)
fk3(b_id) references b(id)
fk4(b_id) references b(id)

I had never seen this before ... it seems completely wrong to me, and my gut feeling should be related to performance (esp when pasted into this table). In this case, the database is PostGres, but what interests me is what people think there will be common behavior.

And if someone experienced a time when you need foreign keys like me, I would also be interested - especially because I'm going to offer to get rid of duplicates!

+3
source share
3

. , FK, a_id.

.

, , ( PostGres)

+2

script ? , script , .

. . script, . , .

script, . .

+1
SELECT
    pc.conname as constraint_name, 
    --conrelid as child_table_id,   
    pclsc.relname as child_table,
    --pc.conkey as child_column_id,
    pac.attname as child_column,
    --confrelid as parent_table_id,
    pclsp.relname as parent_table,
    --pc.confkey as parent_column_id,
    pap.attname as parent_column,   
    nspname as schema_name
FROM 
    (
    SELECT
         connamespace,conname, unnest(conkey) as "conkey", unnest(confkey)
          as "confkey" , conrelid, confrelid, contype
     FROM
        pg_constraint
    ) pc
    JOIN pg_namespace pn ON pc.connamespace = pn.oid
    -- and pn.nspname = 'panmydesk4400'
    JOIN pg_class pclsc ON pc.conrelid = pclsc.oid
    JOIN pg_class pclsp ON      pc.confrelid = pclsp.oid
    JOIN pg_attribute pac ON pc.conkey = pac.attnum    and pac.attrelid =       pclsc.oid
    JOIN pg_attribute pap ON pc.confkey = pap.attnum and pap.attrelid = pclsp.oid

ORDER BY pclsc.relname

( ) FK, ,

+1

Source: https://habr.com/ru/post/1730145/


All Articles