Re: foreign key to "some rows" of a second table

From: Achilleas Mantzios <achill(at)matrix(dot)gatewaynet(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: foreign key to "some rows" of a second table
Date: 2016-02-22 11:27:23
Message-ID: 56CAF09B.50609@matrix.gatewaynet.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 22/02/2016 13:03, Chris Withers wrote:
> Hi All,
>
> So, I have a table that looks like this:
>
> CREATE TABLE config (
> region varchar(10),
> name varchar(10),
> value varchar(40)
> );
>
> Another looks like this:
>
> CREATE TABLE tag (
> host varchar(10),
> type varchar(10),
> value varchar(10)
> );
>
> What's the best way to set up a constraint on the 'config' table such that the 'region' column can only contain values that exist in the 'tag' table's value column where the 'type' is 'region'?

Hi,
that's the reason CONSTRAINT TRIGGERS were introduced in PostgreSQL, I guess.
Just write an AFTER INSERT OR UPDATE TRIGGER ON config,
which checks for integrity.

>
> cheers,
>
> Chris

--
Achilleas Mantzios
IT DEV Lead
IT DEPT
Dynacom Tankers Mgmt

In response to

Browse pgsql-general by date

  From Date Subject
Next Message FarjadFarid(ChkNet) 2016-02-22 14:14:00 Re: Why is my database so big?
Previous Message Chris Withers 2016-02-22 11:03:56 foreign key to "some rows" of a second table