Re: help with serial type - Mailing list pgsql-general

From Peter Eisentraut
Subject Re: help with serial type
Date
Msg-id Pine.LNX.4.30.0104261729510.758-100000@peter.localdomain
Whole thread Raw
In response to help with serial type  (Jason <gee308@mediaone.net>)
List pgsql-general
Jason writes:

> Hi , I'm a postgreSQL newbie.  I have a table called "atable" that has
> the columns:
> title varchar(20)
> name  varchar(20)
> id serial
> if I do:
> INSERT INTO TABLE atable VALUES('SQL1','Jason')
> the 'id' gets updated with a new number automatically.  I then later
> added a new column called 'date'.  Now if I do an insert with:
> INSERT INTO TABLE atable VALUES('SQL2','Toy','',date('now'))
> the id will update the first time to '0',

This is not really valid.  What you are telling PostgreSQL is to insert a
value of '' (empty string) into the id column.  This gets converted to 0
(zero) by the implicit type converter.  The serial type only generates a
sequence number if you do not override it explicitly with a different
value.  So what you want is something like this:

INSERT INTO TABLE atable (title, name, date_field)
  VALUES ('SQL2', 'Toy', current_date);

--
Peter Eisentraut   peter_e@gmx.net   http://funkturm.homeip.net/~peter


pgsql-general by date:

Previous
From: Tom Lane
Date:
Subject: Re: newbie ?'s
Next
From: Jeff Waugh
Date:
Subject: Re: Classes of returned rows