pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Stephan Szabo <sszabo@megazone.bigpanda.com>
To: Thusitha Kodikara <tkodikara@openworld.org>
Cc: pgsql-admin@postgresql.org
Subject: Re: Different results when specifying DEFAULT values through
Date: Wed, 19 Apr 2006 07:12:21 -0700 (PDT)
Message-ID: <20060419070851.F31019@megazone.bigpanda.com> (raw)
In-Reply-To: <200604190653.k3J6rHVo000891@rly18b.srv.mailcontrol.com>
References: <200604190653.k3J6rHVo000891@rly18b.srv.mailcontrol.com>

On Wed, 19 Apr 2006, Thusitha Kodikara wrote:

> Hi,
>
> I observed the following on PostgreSQL 8.1.3 (on Windows 2000).
>
> (1) alter table invoice add column active boolean default true;
>
> This will add a new column "active" with a default clause as "true" and
> also sets the value "true" to all existing rows of the table.
>
> BUT
>
> (2) ALTER TABLE invoice  ADD COLUMN active boolean;
> ALTER TABLE invoice  ALTER COLUMN active SET DEFAULT true;
>
> Will result in only adding the column "active" with default clause as
> "true". It will NOT set the value "true" for existing rows.
>
> Is there a particular reason for this difference in behaviour? (Or am I
> missing something in the syntax?)

AFAICS that's what the spec requires (at least in SQL92)

Add column says:
2) Let C be the column added to T. Every value in C is the default
value for C.

So, in the first case, every value in the new column is true.  In the
second, at the point of the add column every value is null (since there's
no default). Set default doesn't appear to change the value of existing
rows, so no further change is made when you set default (ie, it doesn't
matter if something was set to the default in the past, changing the
default doesn't change the value as stored).



view thread (3+ messages)  latest in thread

Message-ID: <20060419070851.F31019@megazone.bigpanda.com>
Permalink:  ../20060419070851.F31019@megazone.bigpanda.com/
Also on:    postgresql.org/message-id/20060419070851.F31019@megazone.bigpanda.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-admin@postgresql.org
  Cc: sszabo@megazone.bigpanda.com, tkodikara@openworld.org
  Subject: Re: Different results when specifying DEFAULT values through
  In-Reply-To: <20060419070851.F31019@megazone.bigpanda.com>
  References: <200604190653.k3J6rHVo000891@rly18b.srv.mailcontrol.com> <20060419070851.F31019@megazone.bigpanda.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox