pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
How to autoincrement a primary key...
5+ messages / 5 participants
[nested] [flat]

* How to autoincrement a primary key...
@ 2006-09-22 19:13  Doug Hyde <doug.hyde@e-cocreate.com>
  0 siblings, 1 reply; 5+ messages in thread

From: Doug Hyde @ 2006-09-22 19:13 UTC (permalink / raw)
  To: pgsql-sql

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. 

Thanks,

Doug




^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: How to autoincrement a primary key...
@ 2006-09-22 19:25  Richard Broersma Jr <rabroersma@yahoo.com>
  parent: Doug Hyde <doug.hyde@e-cocreate.com>
  0 siblings, 2 replies; 5+ messages in thread

From: Richard Broersma Jr @ 2006-09-22 19:25 UTC (permalink / raw)
  To: Doug Hyde <doug.hyde@e-cocreate.com>; pgsql-sql

> 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('bar'));


Also see:
http://www.postgresql.org/docs/8.1/interactive/sql-createtable.html
http://www.postgresql.org/docs/8.1/interactive/sql-createsequence.html

Regards,

Richard Broersma Jr.



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: How to autoincrement a primary key...
@ 2006-09-22 22:00  Andrew Chilton <andychilton@gmail.com>
  parent: Richard Broersma Jr <rabroersma@yahoo.com>
  1 sibling, 0 replies; 5+ messages in thread

From: Andrew Chilton @ 2006-09-22 22:00 UTC (permalink / raw)
  To: pgsql-sql

On 23/09/06, Richard Broersma Jr <rabroersma@yahoo.com> wrote:
> Is just shorthand notation for:
>
> CREATE SEQUENCE foo START 1;
> CREATE TABLE bar (id integer PRIMARY KEY DEFAULT nextval('bar'));

Doug, just a slight typo correction in that second line Richard gave
you (bar should be foo), it should read in full:

CREATE SEQUENCE foo START 1;
CREATE TABLE bar (id integer PRIMARY KEY DEFAULT nextval('foo'));

Andy

-- 
name: Andrew Chilton
web: http://kapiti.geek.nz/



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: How to autoincrement a primary key...
@ 2006-09-22 22:12  Thomas Kellerer <spam_eater@gmx.net>
  parent: Richard Broersma Jr <rabroersma@yahoo.com>
  1 sibling, 1 reply; 5+ messages in thread

From: Thomas Kellerer @ 2006-09-22 22:12 UTC (permalink / raw)
  To: pgsql-sql

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('bar'));
> 

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




^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: How to autoincrement a primary key...
@ 2006-09-23 06:27  Aarni Ruuhimäki <aarni@kymi.com>
  parent: Thomas Kellerer <spam_eater@gmx.net>
  0 siblings, 0 replies; 5+ messages in thread

From: Aarni Ruuhimäki @ 2006-09-23 06:27 UTC (permalink / raw)
  To: pgsql-sql; +Cc: Doug Hyde <doug.hyde@e-cocreate.com>

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**




^ permalink  raw  reply  [nested|flat] 5+ messages in thread


end of thread, other threads:[~2006-09-23 06:27 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2006-09-22 19:13 How to autoincrement a primary key... Doug Hyde <doug.hyde@e-cocreate.com>
2006-09-22 19:25 ` Richard Broersma Jr <rabroersma@yahoo.com>
2006-09-22 22:00   ` Andrew Chilton <andychilton@gmail.com>
2006-09-22 22:12   ` Thomas Kellerer <spam_eater@gmx.net>
2006-09-23 06:27     ` Aarni Ruuhimäki <aarni@kymi.com>

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