我的查询:
insert into report_order_control_measure(cm_id,report_type,sort_order,temporal_start_date,temporal_end_date)
values(2,'WAR',220,null,null)
ON CONFLICT (cm_id,report_type,sort_order,temporal_start_date,temporal_end_date) DO NOTHING
输出ERROR: there is no unique or exclusion constraint matching the ON CONFLICT specification SQL state: 42P10
表中没有定义约束。但我想抛出一个错误,如果所有的列在插入使一行存在
在不改变表属性的情况下,有什么帮助吗?
使用INSERT INTO ... WHERE NOT EXISTS
语法:
INSERT INTO report_order_control_measure(cm_id,report_type,sort_order,temporal_start_date,temporal_end_date)
SELECT 2,'WAR',220,null,null
WHERE NOT EXISTS (SELECT 1
FROM report_order_control_measure
WHERE cm_id=2
AND report_type='WAR'
AND sort_order=220
AND temporal_start_date IS NULL
AND temporal_end_date IS NULL)
这只在没有具有相同值的同步/并发insert的设置中可用。
在所有情况下,唯一100%安全的无重复插入方法是添加唯一约束。