pg.ddx.io pgsql-admin@postgresql.org mailing list archive
help / color / mirror / Atom feedDifferent results when specifying DEFAULT values through ALTER TABLE
3+ messages / 3 participants
[nested] [flat]
* Different results when specifying DEFAULT values through ALTER TABLE
@ 2006-04-19 06:58 Thusitha Kodikara <tkodikara@openworld.org>
0 siblings, 1 reply; 3+ messages in thread
From: Thusitha Kodikara @ 2006-04-19 06:58 UTC (permalink / raw)
To: pgsql-admin
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?)
Thanks and regards,
-Thusitha Kodikara
--
CONFIDENTIALITY NOTICE The information contained in this message is
confidential, intended only for the use of the individual or the entity
named as recipient. If the reader of this message is not that recipient,
you are notified that any dissemination, distribution or copy of this
message is strictly prohibited. If you have received this message in
error, please immediately notify us by telephone on the number above.
Your co-operation is appreciated.
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: Different results when specifying DEFAULT values through
@ 2006-04-19 14:12 Stephan Szabo <sszabo@megazone.bigpanda.com>
parent: Thusitha Kodikara <tkodikara@openworld.org>
0 siblings, 1 reply; 3+ messages in thread
From: Stephan Szabo @ 2006-04-19 14:12 UTC (permalink / raw)
To: Thusitha Kodikara <tkodikara@openworld.org>; +Cc: pgsql-admin
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).
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: Different results when specifying DEFAULT values through
@ 2006-04-20 08:08 Thusitha Kodikara <kthusi@yahoo.com>
parent: Stephan Szabo <sszabo@megazone.bigpanda.com>
0 siblings, 0 replies; 3+ messages in thread
From: Thusitha Kodikara @ 2006-04-20 08:08 UTC (permalink / raw)
To: Stephan Szabo <sszabo@megazone.bigpanda.com>; +Cc: pgsql-admin
Thanks Stephan. That appears to be the reason for the differences.
-Thusitha
Stephan Szabo <sszabo@megazone.bigpanda.com> wrote: 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).
---------------------------(end of broadcast)---------------------------
TIP 5: don't forget to increase your free space map settings
^ permalink raw reply [nested|flat] 3+ messages in thread
end of thread, other threads:[~2006-04-20 08:08 UTC | newest]
Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2006-04-19 06:58 Different results when specifying DEFAULT values through ALTER TABLE Thusitha Kodikara <tkodikara@openworld.org>
2006-04-19 14:12 ` Re: Different results when specifying DEFAULT values through Stephan Szabo <sszabo@megazone.bigpanda.com>
2006-04-20 08:08 ` Re: Different results when specifying DEFAULT values through Thusitha Kodikara <kthusi@yahoo.com>
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