Re: Best way to create unique primary keys across schemas?

From: Rob Sargent <robjsargent(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Best way to create unique primary keys across schemas?
Date: 2012-01-25 00:10:32
Message-ID: 4F1F4878.4010409@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 01/24/2012 04:23 PM, Merlin Moncure wrote:
> On Tue, Jan 24, 2012 at 5:23 AM, panam <panam(at)gmx(dot)net> wrote:
>> Wow, this is pretty useful. Just to fit it more to my original use case, I
>> used this:
>>
>> CREATE schema schema1;
>> CREATE schema schema2;
>> CREATE TABLE tbl (ID serial primary key,foo varchar,bar varchar); --in
>> public schema
>> CREATE TABLE schema1.tbl (LIKE public.tbl INCLUDING ALL); --draws ids from
>> sequence in public schema
>> CREATE TABLE schema2.tbl (LIKE public.tbl INCLUDING ALL); --draws ids from
>> sequence in public schema
>> INSERT INTO schema1.tbl (foo,bar) VALUES ('asdf','qwer');
>> INSERT INTO schema2.tbl (foo,bar) VALUES ('hello','world');
>> INSERT INTO schema1.tbl (foo,bar) VALUES ('asdf','qwer');
>> INSERT INTO schema2.tbl (foo,bar) VALUES ('hello','world');
>>
>> Thanks, I now consider this my best practice. This way, I don't have to
>> allocate ranges any more a priori :)
> Another quirky way to do it is with domains;
>
> create sequence global_seq;
> create domain gid bigint default nextval('global_seq');
> create table foo (gid gid, f1 text);
> create table bar (gid gid, f2 int);
> etc.
>
> This looks very appealing on the surface but domains have some quirks
> that should give pause. In particular, you can't make arrays of them,
> although you can make arrays of rowtypes that have a domain in them.
>
> Barring domains, you can just manually apply the default instead of
> using a serial type:
>
> create table foo (gid bigint default nextval('global_seq'));
>
> merlin
>
And UUIDs don't work because....?

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Nick 2012-01-25 00:45:14 How to know if update is from a foreign key cascade in plperl?
Previous Message raf 2012-01-24 23:47:54 any plans to support more rounding methods in sql?