AW: QUESTION: Automatically generating Primary keys !!!

From: "Hartwig Jens" <Jens(dot)Hartwig(at)t-systems(dot)de>
To: "Tom Janssens" <tom-janssens(at)freegates(dot)be>, <pgsql-general(at)postgresql(dot)org>
Subject: AW: QUESTION: Automatically generating Primary keys !!!
Date: 2001-06-07 06:02:54
Message-ID: 819FEEAC94E6DC459F1FDC83C0C1611F1D7D6E@mail2000.b3.dsh.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hello Tom,

use a sequece. A sequence starts with a defined value, e.g. 1 and
increments with every next-value-call.

Example:

base=# CREATE SEQUENCE table_seq INCREMENT 1 MINVALUE 1;
base=# SELECT nextval('table_seq');
nextval
---------
1
(1 row)

base=# SELECT nextval('table_seq');
nextval
---------
2
(1 row)

etc.

Have a closer look to the creation of sequences at:

http://www.de.postgresql.org/users-lounge/docs/7.1/reference/sql-creates
equence.html

By the way you even can define the default-value for a column to be
taken from a sequence. In this case you do not need to specify the
primary key on insert:

CREATE TABLE t_table (
id numeric(10) default nextval('s_id'),
text text,
primary key (id)
);

INSERT INTO t_table (text) VALUES ('abcd');

Hope this helps ...

Best regards, Jens Hartwig

-----------------------------------------------------

T-Systems
Project Manager
debis Systemhaus GEI GmbH
Address: Eichhornstrae 3, 10785 Berlin
Postal Address: 10785 Berlin
Phone: (004930) 25 54-32 82
Fax: (004930) 25 54-31 87
Mobile: (0170) 167 26 48
E-Mail: jens(dot)hartwig(at)t-systems(dot)de
Internet: http://www.t-systems.de

> Hello,
>
> I'm working on some sort of search-engine which is accesable
> by a browser.
> On of the features of the engine is that you can insert new
> data into it
> just by using your browser. For all this I use PHP, my database is in
> Postgresql.
>
> My problem is that I should be able to let the Database
> System (Postgresql)
> generate its own primairy keys (from the rest of the data).
>
> eg. I've got a table like this:
>
> workplace_ID, name, street, nr, zipcode, city, country
>
> workplace_ID is the primairy key of that table. Some other
> tables have a
> foreign key refering to it. As a primairy key this ID is of
> course unique.
> My Question is: How can I see which values (of the primairy
> key) are already
> taken, and which is available for new data.
>
> Thanks a lot
>
> STom

Browse pgsql-general by date

  From Date Subject
Next Message Martín Marqués 2001-06-07 06:26:52 Re: [HACKERS] something smells bad
Previous Message Somazx Interesting 2001-06-07 04:26:08 Slow droping tables