Unix Technical Forum

Different results when specifying DEFAULT values through ALTER TABLE

This is a discussion on Different results when specifying DEFAULT values through ALTER TABLE within the pgsql Admins forums, part of the PostgreSQL category; --> Hi, I observed the following on PostgreSQL 8.1.3 (on Windows 2000). (1) alter table invoice add column active boolean ...


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > pgsql Admins

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-10-2008, 07:01 AM
Thusitha Kodikara
 
Posts: n/a
Default Different results when specifying DEFAULT values through ALTER TABLE

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.

---------------------------(end of broadcast)---------------------------
TIP 6: explain analyze is your friend

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-10-2008, 07:01 AM
Stephan Szabo
 
Posts: n/a
Default Re: Different results when specifying DEFAULT values through

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

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-10-2008, 07:02 AM
Thusitha Kodikara
 
Posts: n/a
Default Re: Different results when specifying DEFAULT values through

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


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 04:08 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com