Re: Add a NOT NULL column with default only during add

From: Adrian Klaver <adrian(dot)klaver(at)gmail(dot)com>
To: BladeOfLight16 <bladeoflight16(at)gmail(dot)com>
Cc: Richard Broersma <richard(dot)broersma(at)gmail(dot)com>, "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: Add a NOT NULL column with default only during add
Date: 2013-08-02 00:15:51
Message-ID: 51FAFA37.8020909@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 08/01/2013 04:59 PM, BladeOfLight16 wrote:
> On Thu, Aug 1, 2013 at 7:53 PM, Adrian Klaver <adrian(dot)klaver(at)gmail(dot)com
> <mailto:adrian(dot)klaver(at)gmail(dot)com>> wrote:
>
> It fails because
>
> ALTER TABLE x
> ADD COLUMN data2 VARCHAR(10) NOT NULL DEFAULT 'foo',
>
> end in , instead of ;
>
> You have to add the column before you can alter it.
>
>
> =/ That's the way I have it in the SQL Fiddle sample I provided. I was
> asking what Mr. Broersma was suggesting.
>
> I appreciate the effort by both of you, but it seems my points aren't
> getting across. Is there something more I can do to clarify?

What you want is a default that only works during ALTER ADD COLUMN. At
that point though, there is no data added and DEFAULT only works with
INSERTS. Your example of USING with ALTER data_type works because there
actually may be rows already existing and you are not creating a column.

--
Adrian Klaver
adrian(dot)klaver(at)gmail(dot)com

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message James Sewell 2013-08-02 01:14:14 Re: Snapshot backups
Previous Message BladeOfLight16 2013-08-01 23:59:09 Re: Add a NOT NULL column with default only during add