17

I read here that I should be able to set the default value of a column like this:

ALTER [ COLUMN ] column SET DEFAULT expression

But this:

ALTER address.IsActive SET DEFAULT NULL

Gives me this error:

ERROR: syntax error at or near "address" LINE 1: ALTER address.IsActive SET DEFAULT NULL

What have I done wrong? Also, how can I specify multiple columns to have their default value be NULL?

2
  • 2
    By default values are NULL no real reason to default it to Null.. Commented Feb 12, 2013 at 17:48
  • @xQbert seems to be the way phpPgAdmin's working at least. Thanks! Commented Oct 13, 2013 at 13:56

4 Answers 4

26

The correct syntax is:

ALTER TABLE table_name ALTER COLUMN column_name SET DEFAULT NULL;

For several columns you repeat the ALTER COLUMN part as documented in the manual:

ALTER TABLE table_name 
    ALTER COLUMN foo SET DEFAULT NULL,
    ALTER COLUMN bar SET DEFAULT 0;
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. How could you do this for multiple column names?
@thomas - action [, ... ] means you can add as many as you want, separated by commas.
8

You're not running the complete statement. You're missing the ALTER TABLE part:

ALTER TABLE [ ONLY ] name [ * ]
    action [, ... ]
ALTER TABLE [ ONLY ] name [ * ]
    RENAME [ COLUMN ] column TO new_column
ALTER TABLE name
    RENAME TO new_name

where action is one of:
[...]

Comments

4

Try like below... it will work....

ALTER TABLE address ALTER COLUMN IsActive SET DEFAULT NULL

Comments

2
alter table dogs
alter column breed set default 'boxer'

alter table dogs
alter column breed set default null

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.