Re: system maintained keys

From: Jason Earl <jdearl(at)yahoo(dot)com>
To: Stefan Lindner <lindner(at)visionet(dot)de>, pgsql-sql(at)postgresql(dot)org
Subject: Re: system maintained keys
Date: 2001-10-19 18:57:44
Message-ID: 20011019185744.10807.qmail@web10002.mail.yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

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(at)visionet(dot)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

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Andre Schnabel 2001-10-19 19:05:44 Re: Diferent databases on same query...
Previous Message Josh Berkus 2001-10-19 17:46:50 Re: system maintained keys