Re: Question about serial vs. int datatypes

From: nolan(at)celery(dot)tssi(dot)com
To: lynna(at)gallery44(dot)org (Lynna Landstreet)
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Question about serial vs. int datatypes
Date: 2003-06-08 05:11:10
Message-ID: 20030608051110.16485.qmail@celery.tssi.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Joe Conway 2003-06-08 05:39:15 Re: Question about serial vs. int datatypes
Previous Message alvis 2003-06-08 03:06:52 relation model vs SQL1999 conformance vs PostgreSQL