pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
How to handle a requirement for nextval
7+ messages / 5 participants
[nested] [flat]

* How to handle a requirement for nextval
@ 1998-05-18 20:33  The Web Administrator <wwwadmin@wizard.ca>
  0 siblings, 3 replies; 7+ messages in thread

From: The Web Administrator @ 1998-05-18 20:33 UTC (permalink / raw)
  To: pgsql-sql

I am still struggling on a few issues, have got my first little database
up and running, and am quite happy with PostGres, but I cannot figure
out how to create a table which can have a field that autoincrements..
I am sure that somewhere in the oid terminology it is built in, but I
want to have a counter on some table entries..  ie...
CREATE TABLE clothing_type (
      type_id INT NOT NULL,
      description TEXT,
      for_sex CHAR
);
INSERT INTO clothing_type (
   description,
   for_sex
) VALUES (
   'pants',
   'm'
);

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' ?




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

* Re: [SQL] How to handle a requirement for nextval
@ 1998-05-19 06:56  Jerome Knobl <jknobl@mandanet.ch>
  parent: The Web Administrator <wwwadmin@wizard.ca>
  2 siblings, 1 reply; 7+ messages in thread

From: Jerome Knobl @ 1998-05-19 06:56 UTC (permalink / raw)
  To: wwwadmin@wizard.ca; +Cc: pgsql-sql

I have got the same trouble. I found a first inellegant solution. Is to
create a function which make an increment like that :
create function last98_20_rubriques() returns int4
        as 'Select max(ref98_20_rubriques) as next from DB98_20_rubriques;'
language 'sql';

But I read yesterday that I have to use e SEQUENCE like :

    create table toto (id int4 PRIMARY KEY, test varchar);
    create sequence step1 increment 1 start 1000 minvalue 1000 maxvalue
999999;

And after that I have normaly to do :
    insert into toto (id,test) values (step1.nextval,'sdf');
But It's don't work.
If you can go further send me a mail (and sorry for my very bad english).

JK

The Web Administrator wrote:

> I am still struggling on a few issues, have got my first little database
> up and running, and am quite happy with PostGres, but I cannot figure
> out how to create a table which can have a field that autoincrements..
> I am sure that somewhere in the oid terminology it is built in, but I
> want to have a counter on some table entries..  ie...
> CREATE TABLE clothing_type (
>       type_id INT NOT NULL,
>       description TEXT,
>       for_sex CHAR
> );
> INSERT INTO clothing_type (
>    description,
>    for_sex
> ) VALUES (
>    'pants',
>    'm'
> );
>
> 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' ?






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

* Re: [SQL] How to handle a requirement for nextval
@ 1998-05-19 07:27  Herouth Maoz <herouth@oumail.openu.ac.il>
  parent: The Web Administrator <wwwadmin@wizard.ca>
  2 siblings, 2 replies; 7+ messages in thread

From: Herouth Maoz @ 1998-05-19 07:27 UTC (permalink / raw)
  To: pgsql-sql; +Cc: maillist@candle.pha.pa.us

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





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

* Re: [SQL] How to handle a requirement for nextval
@ 1998-05-19 09:45  Jose' Soares Da Silva <sferac@bo.nettuno.it>
  parent: Jerome Knobl <jknobl@mandanet.ch>
  0 siblings, 0 replies; 7+ messages in thread

From: Jose' Soares Da Silva @ 1998-05-19 09:45 UTC (permalink / raw)
  To: Jerome Knobl <jknobl@mandanet.ch>; +Cc: pgsql-sql

On Tue, 19 May 1998, Jerome Knobl wrote:

> I have got the same trouble. I found a first inellegant solution. Is to
> create a function which make an increment like that :
> create function last98_20_rubriques() returns int4
>         as 'Select max(ref98_20_rubriques) as next from DB98_20_rubriques;'
> language 'sql';
> 
> But I read yesterday that I have to use e SEQUENCE like :
> 
>     create table toto (id int4 PRIMARY KEY, test varchar);
>     create sequence step1 increment 1 start 1000 minvalue 1000 maxvalue
> 999999;
> 
> And after that I have normaly to do :
>     insert into toto (id,test) values (step1.nextval,'sdf');
> But It's don't work.
> If you can go further send me a mail (and sorry for my very bad english).

insert into toto (id,test) values (nextval('step1'),'sdf');

                                                     Jose'




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

* Re: [SQL] How to handle a requirement for nextval
@ 1998-05-19 09:50  Jose' Soares Da Silva <sferac@bo.nettuno.it>
  parent: The Web Administrator <wwwadmin@wizard.ca>
  2 siblings, 0 replies; 7+ messages in thread

From: Jose' Soares Da Silva @ 1998-05-19 09:50 UTC (permalink / raw)
  To: The Web Administrator <wwwadmin@wizard.ca>; +Cc: pgsql-sql

On Mon, 18 May 1998, The Web Administrator wrote:

> I am still struggling on a few issues, have got my first little database
> up and running, and am quite happy with PostGres, but I cannot figure
> out how to create a table which can have a field that autoincrements..
> I am sure that somewhere in the oid terminology it is built in, but I
> want to have a counter on some table entries..  ie...
> CREATE TABLE clothing_type (
>       type_id INT NOT NULL,
>       description TEXT,
>       for_sex CHAR
> );
> INSERT INTO clothing_type (
>    description,
>    for_sex
> ) VALUES (
>    'pants',
>    'm'
> );
> 
> 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' ?
   CREATE SEQUENCE serial START 101;

   CREATE TABLE distributors (
          did      DECIMAL(03) DEFAULT NEXTVAL('serial'),
	  name     VARCHAR(40)
	  );

    -- Use sequence in insert:
    --
    INSERT INTO distributors VALUES (NEXTVAL('serial'));
                                                            Jose'




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

* Re: [SQL] How to handle a requirement for nextval
@ 1998-05-19 13:22  The Web Administrator <wwwadmin@wizard.ca>
  parent: Herouth Maoz <herouth@oumail.openu.ac.il>
  1 sibling, 0 replies; 7+ messages in thread

From: The Web Administrator @ 1998-05-19 13:22 UTC (permalink / raw)
  To: Herouth Maoz <herouth@oumail.openu.ac.il>; +Cc: pgsql-sql; maillist@candle.pha.pa.us

Ok, first of all you have given me the EXACT answer I was looking for. But
trust me, I poured over the docs, and there was no ref to a man page on
'create_sequence', otherwise I would have jumped all over it.. (I am using 6.2,
that might be the problem :>) Yes, I saw the OID thing, but that didn't seem
too standard SQL. The biggest problem I see in PostGres is the wide mixture of
docs. Maybe all the examples and readme's should be put together in a more
common package.  Possibly a doc package?

Herouth Maoz wrote:

> 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' ?
>
> 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
> );
>
>






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

* Re: [SQL] How to handle a requirement for nextval
@ 1998-05-19 18:38  Bruce Momjian <maillist@candle.pha.pa.us>
  parent: Herouth Maoz <herouth@oumail.openu.ac.il>
  1 sibling, 0 replies; 7+ messages in thread

From: Bruce Momjian @ 1998-05-19 18:38 UTC (permalink / raw)
  To: herouth@oumail.openu.ac.il; +Cc: wwwadmin@wizard.ca; pgsql-sql

Update FAQ.  See web site for new heading.

> 
> 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?"


-- 
Bruce Momjian                          |  830 Blythe Avenue
maillist@candle.pha.pa.us              |  Drexel Hill, Pennsylvania 19026
  +  If your life is a hard drive,     |  (610) 353-9879(w)
  +  Christ can be your backup.        |  (610) 853-3000(h)



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


end of thread, other threads:[~1998-05-19 18:38 UTC | newest]

Thread overview: 7+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
1998-05-18 20:33 How to handle a requirement for nextval The Web Administrator <wwwadmin@wizard.ca>
1998-05-19 06:56 ` Jerome Knobl <jknobl@mandanet.ch>
1998-05-19 09:45   ` Jose' Soares Da Silva <sferac@bo.nettuno.it>
1998-05-19 07:27 ` Herouth Maoz <herouth@oumail.openu.ac.il>
1998-05-19 13:22   ` The Web Administrator <wwwadmin@wizard.ca>
1998-05-19 18:38   ` Bruce Momjian <maillist@candle.pha.pa.us>
1998-05-19 09:50 ` Jose' Soares Da Silva <sferac@bo.nettuno.it>

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