Re: what about uniqueness of inherited primary keys

From: Seastian Böck <sebastianboeck(at)freenet(dot)de>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: what about uniqueness of inherited primary keys
Date: 2003-12-29 13:31:00
Message-ID: 3FF02C94.6010404@freenet.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Andreas wrote:
> Hello list,
>
> what about uniqueness of inherited primary keys ?
>
> eg you have :
>
> create table objects (
> id int4,
> date_created timestamp(0),
> primary key (id)
> );
>
> create table persons (
> firstname varchar(100),
> lastname varchar(100)
> ) inherits (objects);
> now ...
>
> insert into objects (id) values (1);
> A repetition of this line would cause an unique-constraint error of
> objects_pkey.
>
> insert into persons (id, firstname, lastname) values (1, 'Super', 'Man');
> insert into persons (id, firstname, lastname) values (1, 'Bat', 'Man');
> works though it violates the inherited constraint.
>
> A select * from objects; shows id=1 three times.
>
> delete * from objects; empties both tables.
>
> after ALTER TABLE public.persons ADD CONSTRAINT persons_pkey PRIMARY
> KEY(id);
> I can still insert at least one record with id=1 in each table.
>
> select count(*) from objects where id=1;
> results 2
>
> Shouldn't we expect to have unique entries in a primary key collumn ?

Hello Andreas,

for primary keys there is a simple (and at least working for me)
solution as long as you can use the SERIAL type for your primary
key.

CREATE TABLE objects (
id SERIAL PRIMARY KEY,
date_created timestamp(0)
);
CREATE TABLE persons (
id INTEGER PRIMARY KEY,
firstname varchar(100),
lastname varchar(100)
) inherits (objects);

Now the id column gets merged and you should have the desired
behaviour.

If you want objects.id to get referenced by other tables you have
to work around with triggers and an extra table. For persons.id
everything is working fine.

This solution (workaround) is only working as long you don't
insert id-values without updating the corresponding sequence.

HTH

Sebastian

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Ericson Smith 2003-12-29 14:40:38 Re: Is my MySQL Gaining ?
Previous Message Karsten Hilbert 2003-12-29 13:01:14 Re: Is my MySQL Gaining ?