PostgreSQL, can't set default value using ALTER COLUMN SET DEFAULT -


i want set default value existed column of integer type in table.

according postgresql doc should use following statement:

alter [ column ] column_name set default expression  

therefore sql statement looks this:

alter table users alter column board_consecutive_share_days set default 0; 

after issued command nothing happened, default value was't set column:

ownpleasuretestdb=# select board_consecutive_share_days users;    board_consecutive_share_days  ------------------------------    (3 rows) 

so, please tell me wrong in sql statement?

you've set default column. means, whenever insert new row table, if omit column, default value used.

for example, if create table so:

create table foo (a int, b int default 0); 

then can issue:

# insert foo values (1, 2); # insert foo (a) values (2); # select * foo; ---+---  1 | 2  2 | 0 (2 rows) 

if, on other hand, want set existing values 0, can update statement. work:

update users set board_consecutive_share_days = 0; 

if want set value there no value yet (that is, it's null), that's easy, too:

update users set board_consecutive_share_days = 0 board_consecutive_share_days null;  

Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -