postgresql - 插入现有表时自动更新主键?
问题描述
实际上,我的问题与此处基本相同,但我需要将嵌套查询中的值插入现有表中,而且我对 sql 并不熟悉,不知道如何将setval
部分合并到我的查询中:
insert into table_a
select *
from table_b
where val_1 IN (select "val_1" from table_c where "val_2" is null)
返回
ERROR: duplicate key value violates unique constraint "table_a_pkey"
DETAIL: Key (qid)=(470971) already exists.
现在我知道我可以按照此处所述使用 drop column 和 autgenerate 解决方法,但必须有一种更优雅的方式。我正在使用 Postgresql/Postgis 2.4 顺便说一句。
解决方案
如果主键是自动生成的,请不要插入 PK 列:
insert into table_a (some_column, other_column, third_column)
select some_column, other_column, third_column
from table_b
where val_1 IN (select "val_1" from table_c where "val_2" is null)
(我不得不猜测列名,因为您没有提供真实的列名)
推荐阅读
- python - Connexion 视图函数抛出 500 错误而不是捕获异常
- html - 我无法在 html 上链接图像
- python-3.x - 如何通过python中的pynput跟踪和模拟箭头键
- linux - 我的 rPi LAMP 盒子上的 Apache 无缘无故停止工作
- hadoop - 使用 derby 初始化时出现 Hive 异常
- tcl - TCL:使用 cmdline 包中的“帮助”选项
- docker - 如何复制没有一些内部文件夹的文件夹(不使用.dockerignore)?
- python - 运行文件并运行选择为 Spyder 中的同一段代码提供不同的输出
- python - 如何将文件与在 Python 中引用它们的 xml 关联?
- python - “命令”对象没有属性“has_permissions”