Re: what about uniqueness of inherited primary keys

From: Oliver Elphick <olly(at)lfix(dot)co(dot)uk>
To: Andreas <maps(dot)on(at)gmx(dot)net>
Cc: PostgreSQL general list <pgsql-general(at)postgresql(dot)org>
Subject: Re: what about uniqueness of inherited primary keys
Date: 2003-12-28 20:56:27
Message-ID: 1072644987.16627.43.camel@linda.lfix.co.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Sun, 2003-12-28 at 20:17, Andreas wrote:
> what about uniqueness of inherited primary keys ?
...
> Shouldn't we expect to have unique entries in a primary key collumn ?

We should, but it doesn't happen. This is a very long-standing defect
which has not yet been solved. To solve it would need an index attached
to multiple tables and the mechanism for that does not yet exist.

The same applies to foreign keys; these too are not inherited.

The solution for primary keys is something like this:

CREATE TABLE tree_index (tboid oid, id varchar(10),primary key
(id),unique (tboid, id));

CREATE TABLE person (id varchar(10) primary key, surname text,foreign
key (tableoid, id) references tree_index(tboid, id) match full on update
cascade on delete cascade);

CREATE TABLE parent (eldest_child varchar(10), primary key (id),foreign
key (tableoid, id) references tree_index(tboid, id) match full on update
cascade on delete cascade) inherits (person);

CREATE TABLE child (father varchar(10) references parent(id), mother
varchar(10) references parent(id), primary key (id), foreign key
(tableoid, id) references tree_index(tboid, id) match full on update
cascade on delete cascade) inherits (person);

alter table parent add constraint "child key" foreign key (eldest_child)
references child (id) on update cascade on delete restrict;

Then you need a trigger on each table to create a record in tree_index
when a new record is added to a table in the hierarchy.

--
Oliver Elphick Oliver(dot)Elphick(at)lfix(dot)co(dot)uk
Isle of Wight, UK http://www.lfix.co.uk/oliver
GPG: 1024D/3E1D0C1C: CA12 09E0 E8D5 8870 5839 932A 614D 4C34 3E1D 0C1C
========================================
"For God shall bring every work into judgment,
with every secret thing, whether it be good, or
whether it be evil." Ecclesiastes 12:14

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Gaetano Mendola 2003-12-28 21:03:45 Re: Is my MySQL Gaining ?
Previous Message Scott Ribe 2003-12-28 20:55:46 Re: what about uniqueness of inherited primary keys