【问题标题】:Postgresql copy generated columnPostgresql 复制生成的列
【发布时间】: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)? 所以当bnull 时,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


【解决方案1】:

您打算做的事情会引入冗余,因此我建议您不要这样做。而是使用coalesce wjen 查询表。一个方便的解决方案是桌子上的一个视图。

如果您坚持将其保留在表中,则必须使用触发器。

【讨论】:

    猜你喜欢
    • 2020-09-08
    • 1970-01-01
    • 2016-08-25
    • 1970-01-01
    • 2017-02-08
    • 2021-03-18
    • 2013-10-01
    相关资源
    最近更新 更多