Re: Unique values on multiple tables - Mailing list pgsql-general

From Emre Hasegeli
Subject Re: Unique values on multiple tables
Date
Msg-id CAE2gYzxq5aqKE43p8kpphKv8WDUs6UKSj8BZ+0UWrAVV5urrxw@mail.gmail.com
Whole thread Raw
In response to Unique values on multiple tables  ("Sterpu Victor" <victor@caido.ro>)
Responses Re: Unique values on multiple tables  ("Sterpu Victor" <victor@caido.ro>)
List pgsql-general
> I have 2 tables and I must make asure unique values like this.
>
> table1
> id
> nr - integer
>
> table2
> id
> id_table1 - FK in Table 1
> valid_from - timestamp
>
> There must be unique values for:
> - nr - from table1
> and
> - YEAR(MIN(valid_from)) from table 2

In situations like this, I add the required column to the other table
with a foreign key.  Assuming that (id) is the primary key of table1,
you would need another unique key on (nr, id).  Than you can add nr
column to table2 by changing the foreign key to (nr, id_table1)
references table1 (nr, id).

Obviously, its not an efficient solution.  It requires an additional
unique key and more storage on the referencing table.  Though, I
believe it is a safe one.  It doesn't allow the duplicated column to
be inconsistent.  There are many things that can go wrong under
concurrency with a trigger like you posted.


pgsql-general by date:

Previous
From: Andreas Kretschmer
Date:
Subject: Re: Multi Master Replication setup
Next
From: Pierre Chevalier
Date:
Subject: Re: Multi Master Replication setup