Thread: how does setval() work???

how does setval() work???

From
chris Günther
Date:
Hi there,

I have a couple of tables in my database, which are part-filled from the
beginning on. In subsequent live there are more datasets to come into
theses tables, but the first ones need to have specific keys in the
primary key column. I use a serial on this column and after creating the
database with the default values in the table I need to set my sequence
by hand using setval() I think. But I just can't figure out how this works.

Can anyone give me an example???

    chris

--

Re: how does setval() work???

From
"Oliver Elphick"
Date:
chris Gnther wrote:
  >database with the default values in the table I need to set my sequence
  >by hand using setval() I think. But I just can't figure out how this works.
  >
  >Can anyone give me an example???


bray=# create table junk (id serial, name text);
NOTICE:  CREATE TABLE will create implicit sequence 'junk_id_seq' for SERIAL
column 'junk.id'
NOTICE:  CREATE TABLE/UNIQUE will create implicit index 'junk_id_key' for
table 'junk'


bray=# insert into junk (name) values ('Fred');
INSERT 194510 1
bray=# select * from junk;
 id | name
----+------
  1 | Fred
(1 row)


bray=# select setval('junk_id_seq',5);
 setval
--------
      5
(1 row)

bray=# insert into junk (name) values ('Joe');
INSERT 194511 1
bray=# select * from junk;
 id | name
----+------
  1 | Fred
  6 | Joe
(2 rows)

--
Oliver Elphick                                Oliver.Elphick@lfix.co.uk
Isle of Wight                              http://www.lfix.co.uk/oliver
PGP: 1024R/32B8FAA1: 97 EA 1D 47 72 3F 28 47  6B 7E 39 CC 56 E4 C1 47
GPG: 1024D/3E1D0C1C: CA12 09E0 E8D5 8870 5839  932A 614D 4C34 3E1D 0C1C
                 ========================================
     "Be patient therefore, brethren, unto the coming of the
      Lord...Be patient; strengthen your hearts, for
      the coming of the Lord draweth nigh."
                                   James 5:7,8