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

From John Turner
Subject Re: Unique values on multiple tables
Date
Msg-id CAMAP1QniggD9Vf3McUdXrGsj2z9botCFiwY2F3Fy04JcYX9fCQ@mail.gmail.com
Whole thread Raw
In response to Re: Unique values on multiple tables  ("Sterpu Victor" <victor@caido.ro>)
List pgsql-general
On Mon, Mar 28, 2016 at 2:32 AM, Sterpu Victor <victor@caido.ro> wrote:
 
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
Just trying to understand your goal here -

Is 'nr' supposed to be a count of records from table2 by 'id_table1' column?
And every single value for 'nr' within table1 must be unique - i.e., you need a table of unique counts?

I think I fixed the problem by executing the function AFTER insert or update but I'm not sure.
Until now the execution was before insert or update.

That doesn't sound right, I believe you would want to stick with execution of the DML to your presumable 'fact' table (table2) using a BEFORE trigger, *especially* if it's intended to enforce constraints.

It would be helpful if you could:
  • clarify/elaborate on how these 2 tables are supposed to relate to each other
  • post your actual trigger
  • provide us with some example inserts/updates
- John

pgsql-general by date:

Previous
From: Roman Scherer
Date:
Subject: How to quote the COALESCE function?
Next
From: Tom Lane
Date:
Subject: Re: How to quote the COALESCE function?