Re: How to have a unique primary key on two tables

From: "Bart Degryse" <Bart(dot)Degryse(at)indicator(dot)be>
To: <pgsql-sql(at)postgresql(dot)org>
Subject: Re: How to have a unique primary key on two tables
Date: 2007-11-22 11:11:20
Message-ID: 474571E8.A3DD.0030.0@indicator.be
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

When you use serial a kind of macro is performed: in fact an integer field is created, a sequence is created with a name based on the table's name and the nextval of that sequence is used as the default value for the field. Now you have to do these steps "manually".

CREATE SEQUENCE "public"."tbl_all_ID_seq" INCREMENT 1 MINVALUE 1 START 1 CACHE 1;

CREATE TABLE tbl_first (
id INTEGER DEFAULT nextval('public."tbl_all_ID_seq"'::text) NOT NULL,
testo text
);

CREATE TABLE tbl_second (
id INTEGER DEFAULT nextval('public."tbl_all_ID_seq"'::text) NOT NULL,
testo text
);
>>> "Daniel "bodom_lx" Graziotin" <daniel(dot)graziotin(at)gmail(dot)com> 2007-11-22 12:01 >>>
Hi everybody,
I need to have a primary key which has to be unique on two tables.
E.g.:

CREATE TABLE first
(
id serial NOT NULL,
testo text,
)

CREATE TABLE second
(
id serial NOT NULL,
testo text,
)

When I insert some text on "first", I would like first.id = second.id
+ 1, and vice versa.
A sort of primary key in common for both tables.

Any hints?
Thank you very much
--
Daniel "bodom_lx" Graziotin
- http://daniel.graziotin.net ( http://daniel.graziotin.net/ )
- http://daniel.graziotin.net/bodom_lx.asc - GPG public key

---------------------------(end of broadcast)---------------------------
TIP 3: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faq

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message A. Kretschmer 2007-11-22 11:19:56 Re: How to have a unique primary key on two tables
Previous Message Daniel "bodom_lx" Graziotin 2007-11-22 11:01:59 How to have a unique primary key on two tables