Re: Question about serial vs. int datatypes - Mailing list pgsql-general

From nolan@celery.tssi.com
Subject Re: Question about serial vs. int datatypes
Date
Msg-id 20030608051110.16485.qmail@celery.tssi.com
Whole thread Raw
In response to Question about serial vs. int datatypes  (Lynna Landstreet <lynna@gallery44.org>)
List pgsql-general
> But... when converting an existing database that already has several hundred
> records in it, I can't make that field serial in PostgreSQL, can I?

I guess you haven't actually tried this yet, but if you do an insert
with an explicit value for a column of type serial, it inserts that value.
If you leave that column off the list of columns in the insert statement,
it uses the nextval of the implicit sequence, which is the default value
of the column.  If you use NULL, you will get an error.  You can also
explicitly select the sequence value.

Here's a sample table, test1.  Note the two modifiers for 'keyval'.

 Column  |         Type     |                         Modifiers

---------+------------------+----------------------------------------------
 keyval  | integer          | not null
                              default nextval('public.test1_keyval_seq'::text)
 dataval | character varying(30)

INSERT into test1 values ('15','TEST');
INSERT into test1 (dataval) values ('FISH');
INSERT into test1 values (null,'MOUSE');
INSERT into test1 values (nextval('test1_keyval_seq'),'CAT');

select * from test1;
 keyval | dataval
--------+---------
     15 | TEST
      1 | FISH
      2 | CAT
(3 rows)
--
Mike Nolan

pgsql-general by date:

Previous
From: Martin_Hurst@dom.com
Date:
Subject: Linux 2.6 kernel, tuned for use with databases - does that apply to Postgresql too?
Next
From: Avi Schwartz
Date:
Subject: Temporary tables inside functions problem