Re: How to autoincrement a primary key...

From: Aarni Ruuhimäki <aarni(at)kymi(dot)com>
To: pgsql-sql(at)postgresql(dot)org
Cc: "Doug Hyde" <doug(dot)hyde(at)e-cocreate(dot)com>
Subject: Re: How to autoincrement a primary key...
Date: 2006-09-23 06:27:57
Message-ID: 200609230927.58000.aarni@kymi.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Saturday 23 September 2006 01:12, Thomas Kellerer wrote:
> Richard Broersma Jr wrote on 22.09.2006 21:25:
> >> I am sure this is simple, but I don't get it. I am new to PGSQL, coming
> >> from MySQL - in mysql, you can autoincrement the primary key; in
> >> postgre, I am not sure how to do this. I have read the documentation,
> >> and tried "nextval" as the default - I have searched for the datatype
> >> SERIAL, but I am using navicat and this datatype is not supported. Can
> >> someone tell me how to do this - I just want the integer value for a
> >> primary key to autoincrement by one.
> >
> > CREATE TABLE bar (id SERIAL PRIMARY KEY);
> >
> >
> > Is just shorthand notation for:
> >
> > CREATE SEQUENCE foo START 1;
> > CREATE TABLE bar (id integer PRIMARY KEY DEFAULT nextval('foo'));
>
> Well the shorthand notation has a minor gotcha: you cannot drop the
> sequence that has been created automatically. Only if you drop the column
> itself. Should not be a problem, but it is a difference between a SERIAL
> PRIMARY KEY definition and the "verbose" mode
>
> Thomas
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 9: In versions below 8.0, the planner will ignore your desire to
> choose an index scan if your joining column's datatypes do not
> match

Verbosily you can have even more control over the sequence.

With SERIAL the default is something like

CREATE SEQUENCE foo
INCREMENT BY 1
NO MAXVALUE
NO MINVALUE
CACHE 1;

By hand you can define e.g.

CREATE SEQUENCE foo
START n
INCREMENT BY n
MAXVALUE n
MINVALUE n
CACHE 1;

BR,

Aarni
--
Aarni Ruuhimäki

**Kmail**
**Fedora Core Linux**

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message TJ O'Donnell 2006-09-23 14:43:18 Re: unique rows
Previous Message Thomas Kellerer 2006-09-22 22:12:00 Re: How to autoincrement a primary key...