SQL 在 postgresql 上将默认值设置为 null

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14838482/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 13:40:16  来源:igfitidea点击:

set default value to null on postgresql

sqlpostgresql

提问by 1252748

I read herethat 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

错误:“地址”处或附近的语法错误第 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?

我做错了什么?另外,如何指定多个列以使其默认值为NULL

采纳答案by álvaro González

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

你没有运行完整的语句。你错过了ALTER TABLE部分:

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:
[...]

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

其中动作是以下之一:
[...]

回答by a_horse_with_no_name

The correct syntax is:

正确的语法是:

ALTER TABLE table_name ALTER COLUMN column_name SET DEFAULT NULL;

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

对于多个列,您可以ALTER COLUMN按照手册中的说明重复该部分:

ALTER TABLE table_name 
    ALTER COLUMN foo SET DEFAULT NULL,
    ALTER COLUMN bar SET DEFAULT 0;

回答by Pandian

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

像下面这样尝试......它会起作用......

ALTER TABLE address ALTER COLUMN IsActive SET DEFAULT NULL

回答by Tim

alter table dogs
alter column breed set default 'boxer'

alter table dogs
alter column breed set default null