我有什么:
ALTER TABLE countryb
ADD gnppercap real
;
INSERT INTO countryb (gnppercap)
SELECT gnp/population
FROM countryb
;
我成功创建了“gnppercap
”列,现在我想用变量填充每一行的值。新变量是 gnp 和 1/population 的乘积,变量 gnp 和 population 已经在我正在修改的表中,countryb。
这是错误:
ERROR: null value in column "code" violates not-null constraint
SQL state: 23502
Detail: Failing row contains (null, null, null, null, null, null, null, null, null, null, null, >null, null, null, null, 0.000263028).
我知道表 countryb 中有大量非空变量,所以我认为这就是那些空值。我认为既然我指定了我要插入值的列,那没关系......?
我迷路了。帮助赞赏!
最佳答案
您想更新表格,而不是插入新行。
update countryb set gnppercap = gnp/population
顺便说一句:您可能不想将计算值存储在单独的列中。如果更新 gnp 或人口会发生什么?您的 gnppercap 列将不准确。
关于sql - 新来的sql,如何将列值插入到新列中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16593731/