X-Original-To: pgsql-admin-postgresql.org@localhost.postgresql.org Received: from localhost (mx2.hub.org [200.46.204.254]) by postgresql.org (Postfix) with ESMTP id 6A51F11F692F for ; Wed, 19 Apr 2006 11:12:28 -0300 (ADT) X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Score: -2.524 X-Spam-Level: X-Spam-Status: No, score=-2.524 required=5 tests=[AWL=0.075, BAYES_00=-2.599] Received: from postgresql.org ([200.46.204.71]) by localhost (mx2.hub.org [200.46.204.254]) (amavisd-new, port 10024) with ESMTP id uSRNRuiXc-6f for ; Wed, 19 Apr 2006 11:12:25 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from megazone.bigpanda.com (megazone.bigpanda.com [64.147.171.210]) by postgresql.org (Postfix) with ESMTP id 1094611F6793 for ; Wed, 19 Apr 2006 11:12:21 -0300 (ADT) Received: by megazone.bigpanda.com (Postfix, from userid 1001) id BC16535093; Wed, 19 Apr 2006 07:12:21 -0700 (PDT) Received: from localhost (localhost [127.0.0.1]) by megazone.bigpanda.com (Postfix) with ESMTP id BA14335091; Wed, 19 Apr 2006 07:12:21 -0700 (PDT) Date: Wed, 19 Apr 2006 07:12:21 -0700 (PDT) From: Stephan Szabo To: Thusitha Kodikara Cc: pgsql-admin@postgresql.org Subject: Re: Different results when specifying DEFAULT values through In-Reply-To: <200604190653.k3J6rHVo000891@rly18b.srv.mailcontrol.com> Message-ID: <20060419070851.F31019@megazone.bigpanda.com> References: <200604190653.k3J6rHVo000891@rly18b.srv.mailcontrol.com> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Archive-Number: 200604/183 X-Sequence-Number: 21352 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).