Re: Using nextval(seq) in more than one column - Mailing list pgsql-novice

From A. Kretschmer
Subject Re: Using nextval(seq) in more than one column
Date
Msg-id 20071010124221.GK9541@a-kretschmer.de
Whole thread Raw
In response to Using nextval(seq) in more than one column  (Sean Davis <sdavis2@mail.nih.gov>)
List pgsql-novice
am  Wed, dem 10.10.2007, um  8:18:33 -0400 mailte Sean Davis folgendes:
> I am trying to design some tables that have keys that look like:
>
> ASDF-####
>
> where #### should be derived from a sequence.  However, I would like the
> primary key to be an integer for speed of indexing, etc.  I don't see a
> way using standard DDL followed by inserts to have the #### be the same
> number as the integer primary key.  Is that the case?
>
> In other words, I would like the rows to look like:
>
> 1   ASDF-1   ....
> 2   ASDF-2   ....

Do you mean something like this:

test=# create table asdf (id serial primary key, foo text);
NOTICE:  CREATE TABLE will create implicit sequence "asdf_id_seq" for serial column "asdf.id"
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "asdf_pkey" for table "asdf"
CREATE TABLE
test=*# insert into asdf (foo) values ('foo1');
INSERT 0 1
test=*# insert into asdf (foo) values ('foo2');
INSERT 0 1
test=*# insert into asdf (foo) values ('foo3');
INSERT 0 1
test=*# select id, 'ASDF-'||id::text, foo from asdf;
 id | ?column? | foo
----+----------+------
  1 | ASDF-1   | foo1
  2 | ASDF-2   | foo2
  3 | ASDF-3   | foo3
(3 rows)



Andreas
--
Andreas Kretschmer
Kontakt:  Heynitz: 035242/47150,   D1: 0160/7141639 (mehr: -> Header)
GnuPG-ID:   0x3FFF606C, privat 0x7F4584DA   http://wwwkeys.de.pgp.net

pgsql-novice by date:

Previous
From: Sean Davis
Date:
Subject: Using nextval(seq) in more than one column
Next
From: "Greg Sabino Mullane"
Date:
Subject: Re: Using nextval(seq) in more than one column