Обсуждение: primary key generation
Does postgres have any built-in methods for auto-generating unique integer primary keys or is managing this task the responsibility of the user? Thanks in advance...
Hi You need to create a sequence and set the default value of the primary key on the next value in the sequence. An easy way to do this is to define the primary key integer field as type SERIAL ( instead of integer) at time of table creation. Postgres will automaticly create the sequence it needs, and set the default value of the key to the proper value. HTH, Wieger On Mon, 2001-12-10 at 22:58, Ned Matson wrote: > Does postgres have any built-in methods for auto-generating unique integer > primary keys or is managing this task the responsibility of the user? > > Thanks in advance... > > > > > ---------------------------(end of broadcast)--------------------------- > TIP 4: Don't 'kill -9' the postmaster >
"Ned Matson" <ned@go.com> writes: > Does postgres have any built-in methods for auto-generating unique integer > primary keys or is managing this task the responsibility of the user? Sure, read up on the SERIAL column type. -Doug -- Let us cross over the river, and rest under the shade of the trees. --T. J. Jackson, 1863
Here is an example:
CREATE SEQUENCE format_id_count;
CREATE TABLE format (
FOREIGN KEY(vehicle_id) REFERENCES vehicle(vehicle_id),
format_id INTEGER PRIMARY KEY
DEFAULT
NEXTVAL('format_id_count
'),
vehicle_id INTEGER,
format_type VARCHAR(11),
description VARCHAR(31),
.......
--- Ned Matson <ned@go.com> wrote:
> Does postgres have any built-in methods for auto-generating unique
> integer
> primary keys or is managing this task the responsibility of the user?
>
> Thanks in advance...
>
>
>
>
> ---------------------------(end of
> broadcast)---------------------------
> TIP 4: Don't 'kill -9' the postmaster
=====
Chris Albertson
Home: 310-376-1029 chrisalbertson90278@yahoo.com
Cell: 310-990-7550
Office: 310-336-5189 Christopher.J.Albertson@aero.org
__________________________________________________
Do You Yahoo!?
Check out Yahoo! Shopping and Yahoo! Auctions for all of
your unique holiday gifts! Buy at http://shopping.yahoo.com
or bid at http://auctions.yahoo.com