pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Jason Earl <jdearl@yahoo.com>
To: Stefan Lindner <lindner@visionet.de>
To: pgsql-sql@postgresql.org
Subject: Re: system maintained keys
Date: Fri, 19 Oct 2001 11:57:44 -0700 (PDT)
Message-ID: <20011019185744.10807.qmail@web10002.mail.yahoo.com> (raw)
In-Reply-To: <9qf06v$20bh$1@news.tht.net>

Check out the SERIAL type.  It does precisely what you
want.  An idea as to how this is used would be like
this:

CREATE TABLE foo (
  prim_key     SERIAL PRIMARY KEY,
  bar          text
);

I tend to create sequences by hand like this:

CREATE SEQUENCE my_sequence_seq;

And then I create my table with a definition like
this:

CREATE TABLE foo (
  prim_key     int DEFAULT nextval('my_sequence_seq')
                 PRIMARY KEY,
  bar          text,
);

But that's just because I have been using PostgreSQL
long enough that it didn't have the SERIAL type when I
started.  The SERIAL type is just syntactic sugar for
what I generally do the long way.

Either way you simply pretend that the column isn't
there when you do inserts (unless you know what you
are doing) like so:

INSERT INTO foo (bar) VALUES ('hello');
INSERT INTO foo (bar) VALUES ('goodbye');

And then when you select you get:

processdata=> SELECT * FROM foo;
 prim_key |   bar   
----------+---------
        1 | hello
        2 | goodbye
(2 rows)

I hope that is helpful,
Jason Earl

--- Stefan Lindner <lindner@visionet.de> wrote:
> Is there any way to get system maintained keys from
> postgres? e.g. to
> have a table with a primary key column (varchar or
> int) and let postgres
> chose the next unique value for this column?
> 
> 
> 
> ---------------------------(end of
> broadcast)---------------------------
> TIP 6: Have you searched our list archives?
> 
> http://archives.postgresql.org


__________________________________________________
Do You Yahoo!?
Make a great connection at Yahoo! Personals.
http://personals.yahoo.com



view thread (5+ messages)  latest in thread

Message-ID: <20011019185744.10807.qmail@web10002.mail.yahoo.com>
Permalink:  ../20011019185744.10807.qmail@web10002.mail.yahoo.com/
Also on:    postgresql.org/message-id/20011019185744.10807.qmail@web10002.mail.yahoo.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: jdearl@yahoo.com, lindner@visionet.de
  Subject: Re: system maintained keys
  In-Reply-To: <20011019185744.10807.qmail@web10002.mail.yahoo.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox