pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Herouth Maoz <herouth@oumail.openu.ac.il>
To: wwwadmin@wizard.ca, PGsql <pgsql-sql@postgreSQL.org>
Cc: maillist@candle.pha.pa.us
Subject: Re: [SQL] How to handle a requirement for nextval
Date: Tue, 19 May 1998 10:27:50 +0300
Message-ID: <l03110702b186e1a326b8@[147.233.159.109]> (raw)
In-Reply-To: <35609B0F.52A63C2@wizard.ca>

At 23:33 +0300 on 18/5/98, The Web Administrator wrote:


> What I want is that the Primary Key (Only Key) be type_id, and int, and
> the first item that I insert should have type_id as '1', next will be
> '2'  etc..
> I could have every insert into this table include a type_id, but that
> seems unessary.
> Can I have something like default='nextval' ?

This is so much a faq, that I went to look at the FAQ. In fact, there is a
question there which is similar to this one, but I think its phrasing
defeats newbies rather than helps them. I for one have never encountered
fields of type SERIAL, and people might not think it's the same sort of
question. The answer is even more of a problem, because the most commonly
used method of doing this is summarised in "look at the create_sequence
manual", whereas the two less recommended methods (using OIDs and using an
auto-incrementing function) are discussed in detail.

Bruce, don't you agree? Perhaps change the phrasing of the question to "How
do I create an auto-incrementing field?"

As for the answer itself, here it is:

In order to create an auto-incrementing field - one which will
automatically receive the value 1 for the first row inserted, 2 for the
second, and so on - you have to define a sequence. For example:

CREATE SEQUENCE emp_no;

Then you define your table. Assuming you want an employee table in which
the emp_id field is autoincrementing, here is what you write:

CREATE TABLE emp
(
    emp_id    int4
              DEFAULT nextval( 'emp_no' )
              NOT NULL
    -- Other fields here
);

Following that, when you want to insert a row, insert values for all other
fields except the emp_id field. It will insert its own value automatically.

For more information, read the man page "create_sequence".

Herouth





view thread (7+ messages)  latest in thread

Message-ID: <l03110702b186e1a326b8@[147.233.159.109]>
Permalink:  ../l03110702b186e1a326b8@%5B147.233.159.109%5D/
Also on:    postgresql.org/message-id/l03110702b186e1a326b8@[147.233.159.109]

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: herouth@oumail.openu.ac.il, maillist@candle.pha.pa.us
  Subject: Re: [SQL] How to handle a requirement for nextval
  In-Reply-To: <l03110702b186e1a326b8@[147.233.159.109]>
  References: <35609B0F.52A63C2@wizard.ca> <l03110702b186e1a326b8@[147.233.159.109]>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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