Re: How to setup a column constraint for an integer type? Default 0 if not defined

From: John R Pierce <pierce(at)hogranch(dot)com>
To: "Wang, Mary Y" <mary(dot)y(dot)wang(at)boeing(dot)com>
Cc: "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: How to setup a column constraint for an integer type? Default 0 if not defined
Date: 2010-04-02 01:51:48
Message-ID: 4BB54DB4.40704@hogranch.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Wang, Mary Y wrote:
> Hi,
>
>
> I'm trying to port some source code to Postgres 8.3.8 and have been encountering some surprises :-(
> I would like to set up a column constraint for an integer type, so that when the value is undefined, it would assign a default value of 0 to that column.
> How would I do that in 8.3.8? Please provide an example.
>

when you say, 'value is undefined' do you mean, if an insert is done
without specifying that field ?

in that case, ou wouldn't use a constraint, you'd use a DEFAULT value,
like...

CREATE TABLE tblname (
....
somefield INTEGER DEFAULT 0,
....
);

or, if the table already exists...

ALTER TABLE tblname ALTER COLUMN somefield SET DEFAULT 0;

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Craig Ringer 2010-04-02 04:43:29 Re: "1-Click" installer problems
Previous Message Wang, Mary Y 2010-04-02 01:18:55 How to setup a column constraint for an integer type? Default 0 if not defined