如何为 postgres 中的列设置非空约束

Hei*_*erg 10 sql postgresql ddl alter-table

我尝试将 null 设置为如下所示的列。

ALTER TABLE myschema.table ALTER COLUMN (test_id,type) SET NOT NULL;

但它返回语法错误,例如 Syntax error at or near Line 3, Position 47

有没有适当的方法来实现这一目标?

如果有人有意见请告诉我。

谢谢

小智 13

您无法在括号中提供列列表,您需要使用多个 ALTER COLUMN 选项,并用逗号分隔:

ALTER TABLE the_table
    ALTER COLUMN test_id set not null, 
    ALTER COLUMN type SET NOT NULL;
Run Code Online (Sandbox Code Playgroud)


小智 6

尝试对两列分别执行此操作:

ALTER TABLE myschema.table ALTER COLUMN test_id SET NOT NULL;

ALTER TABLE myschema.table ALTER COLUMN type SET NOT NULL;
Run Code Online (Sandbox Code Playgroud)