Re: Nextval

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]



On Thu, 11 Oct 2001, Zavier Sheran wrote:

> I try to do the following:
>
> Fetch a SERIAL field (ie. record_id) and get the
> highest value currently stored (ie. 1000), increment
> the value by 1 and store a record that will have the
> same value (1001) in record_id. So there must be a way
> with concurrency control.
>
> I went through the manuals and found the solution with
> NEXTVAL('serial'), but you have to create a sequence
> for that, and I don't know if it is the right way to
> do for what I want.
>
> It is my first PHP project involving a Database, so
> it's a newbie question. Have mercy...
>
> Thanks
>

marco=# create table test_serial ( id serial );
NOTICE:  CREATE TABLE will create implicit sequence 'test_serial_id_seq' for SERIAL column 'test_serial.id'
NOTICE:  CREATE TABLE/UNIQUE will create implicit index 'test_serial_id_key' for table 'test_serial'

The serial type automagically creates both a sequence and an index:
the sequence is named <table>_<column>_seq and the index
<table>_<column>_key.

You can use nextval() on the sequence if you like, as in:
insert into test_serial values ( nextval('test_serial_id_seq') );

or have the column default to that so that you don't even bother on
inserts.

.TM.
-- 
      ____/  ____/   /
     /      /       /			Marco Colombo
    ___/  ___  /   /		      Technical Manager
   /          /   /			 ESI s.r.l.
 _____/ _____/  _/		       Colombo@xxxxxx



[Index of Archives]     [Postgresql General]     [Postgresql Admin]     [PHP Users]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Databases]     [Yosemite Backpacking]     [Postgresql Jobs]

  Powered by Linux