Sequences and inheritance

Started by Bruce Richardsonover 24 years ago2 messagesgeneral
Jump to latest
#1Bruce Richardson
brichardson@lineone.net

If I have a set of tables and I set their primary keys all to the same
sequence, as in

create sequence common_seq;

create table alpha (
ID integer primary key default nextval('common_seq),
other_field text
);

create table beta (
ID integer primary key default nextval('common_seq),
other_field real
);
create table gamma (
ID integer primary key default nextval('common_seq),
other_field oid
);

Would this guarantee that each value of ID in any row of any of the
tables would be unique amongst all of them?

Would I get the same effect by creating a table like this:

create table common (
ID serial primary key
);

and then having alpha, beta and gamma inherit from it?

--

Bruce

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Bruce Richardson (#1)
Re: Sequences and inheritance

Bruce Richardson <brichardson@lineone.net> writes:

If I have a set of tables and I set their primary keys all to the same
sequence, as in
...
Would this guarantee that each value of ID in any row of any of the
tables would be unique amongst all of them?

As long as the sequence doesn't wrap around, yes.

IIRC, you can set up a sequence to error out rather than wrap when
it hits the max...

regards, tom lane