Postgresql 复制生成的列
Postgresql copy generated column
让我们假设 table 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
关键字和更新或在插入触发器之前使用。
你打算做的事情会引入冗余,所以我建议你不要这样做。相反,在查询 table 时使用 coalesce
。一个方便的解决方案是在 table 上执行此操作的视图。
如果您在 table 中坚持坚持,您将不得不使用触发器。
让我们假设 table 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
关键字和更新或在插入触发器之前使用。
你打算做的事情会引入冗余,所以我建议你不要这样做。相反,在查询 table 时使用 coalesce
。一个方便的解决方案是在 table 上执行此操作的视图。
如果您在 table 中坚持坚持,您将不得不使用触发器。