Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 冲突时,批量插入时忽略do NOTING子句_Sql_Postgresql_Foreign Keys_Sql Insert_Bulkinsert - Fatal编程技术网

Sql 冲突时,批量插入时忽略do NOTING子句

Sql 冲突时,批量插入时忽略do NOTING子句,sql,postgresql,foreign-keys,sql-insert,bulkinsert,Sql,Postgresql,Foreign Keys,Sql Insert,Bulkinsert,只有产品表中存在的产品才能添加到价目表中 剧本 update pricelistnew set timestamp=to_char(now(), 'YYYYMMDDHH24MISS'); truncate pricelist; insert into pricelist select * from pricelistnew on conflict do nothing; 抛出错误 错误:在表pricelist上插入或更新与外键冲突 约束价格表\u产品\u fkey 细节:关键 produc

只有产品表中存在的产品才能添加到价目表中

剧本

update pricelistnew set timestamp=to_char(now(), 'YYYYMMDDHH24MISS');

truncate pricelist;

insert into pricelist
select * from pricelistnew
on conflict do nothing;
抛出错误

错误:在表pricelist上插入或更新与外键冲突 约束价格表\u产品\u fkey 细节:关键 product=TMMEM0EM00691BDS不在表product中

带有附加外键检查的脚本

insert into pricelist
select * from pricelistnew
where product in (select product  from product)
on conflict do nothing;
成功了

为什么在冲突中不作为条款被忽略? 如何仅添加与外键匹配的产品而不使用附加检查

product in (select product  from product )
价目表有300000行,在fast服务器上完成此命令需要几分钟。它锁定表,以便延迟同时运行的其他查询。如何有效地进行批量插入

使用

PostgreSQL 13.2,用Visual C++编译生成1900, 64位< /P> 更新

表结构:

create table product (
   product char(20) primary key );

create table pricelist ( product char(20) references product on update cascade on delete cascade deferrable, 
timestamp char(14), 
price numeric(12,2) );

create table pricelistnew ( product char(20) , timestamp char(14), 
price numeric(12,2) );

on conflict仅处理违反唯一约束的情况。如果在插入期间从其他事务中的product表中删除了product,则在select product from product中带有where product的脚本仍会失败。如何解决这个问题而不在单独的事务中运行每一个insert?@Andrus请同时发布您的表结构。更新了问题并添加了表结构