gpt4 book ai didi

sql - 如何更改 Postgresql 中新的非空列?

转载 作者:行者123 更新时间:2023-11-29 12:03:13 24 4
gpt4 key购买 nike

我有一个表,我想添加新的 not null varchar(255)

我的查询是:

alter table poll_management.DASHLETS add column name varchar(255) not null;
update poll_management.DASHLETS as dashlet set name = report.name
from poll_management.REPORTS as report
WHERE dashlet.id = report.reportdashletid

但是我有一个错误:

ERROR:  column "name" contains null values
********** Error **********

ERROR: column "name" contains null values
SQL state: 23502

最佳答案

为避免您的错误,同时提供两种解决方案:

BEGIN;
alter table poll_management.DASHLETS add column name varchar(255);
update poll_management.DASHLETS as dashlet set name = report.name
from poll_management.REPORTS as report;
--mind I removed where, cos you need to update ALL rows to have some avlue
alter table poll_management.DASHLETS alter column "name" set not null;
END;

和另一个:

  alter table poll_management.DASHLETS add column name varchar(255) NOT NULL default 'not set';

关于sql - 如何更改 Postgresql 中新的非空列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43705012/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com