Re: unique across two tables

From: tv(at)fuzzy(dot)cz
To: "Tarlika Elisabeth Schmitz" <postgresql4(at)numerixtechnology(dot)de>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: unique across two tables
Date: 2011-06-20 15:18:47
Message-ID: 9928b10d5090b793d6ae56dcd62f0fc7.squirrel@sq.gransy.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

> I have two tables, town and townalias, the latter containing alternative
> town names.
> I would like to ensure that a town name is unique per
> country-region across the two tables.
>
> Can I do this with a constraint ot do I need to implement the logic via
> trigger?

You can't have a constraint spreading multiple tables. And fixing this
using a trigger is harder than it looks, because a trigger does not see
uncommited changes made by other sessions.

So for example this will be hard to catch:

Session A: INSERT INTO town(name) VALUES ('Chicago');
Session B: INSERT INTO town(name) VALUES ('Chicago');
Session A: COMMIT;
Session B: COMMIT;

What I'd do is I'd keep the primary name in the 'townalias' table too,
maybe with a 'primary=true' flag. That way you can use traditional UNIQUE
constraint.

regards
Tomas

In response to

Browse pgsql-general by date

  From Date Subject
Next Message salah jubeh 2011-06-20 15:57:22 Re: connection time out
Previous Message Tarlika Elisabeth Schmitz 2011-06-20 15:03:07 unique across two tables