【发布时间】:2021-02-10 19:48:25
【问题描述】:
让我们假设一个表t:
create table if not exists t(
a integer primary key generated by default as identity,
b integer,
c text
);
然后简单插入insert into t (b, c) values (2, 'abc');
是否有任何选项如何重写此插入,因此列 b 将是 coalesce(b, a)?
所以当b 是null 时,b 等于生成的列a。
我知道有一个选项可以使用 RETURNING 关键字并更新或在插入触发器之前使用。
【问题讨论】:
-
否:
insert into t (b, c) values (coalesce(null, a), 'abc'); HINT: There is a column named "a" in table "t", but it cannot be referenced from this part of the query.
标签: postgresql sql-insert