【发布时间】:2011-08-22 12:08:36
【问题描述】:
我正在使用 DB2 DBMS。
场景 1:
myTable 有一个复合键 (key1, key2),其中 key1 和 key2 都是 yourTable 的外键。
我想将 yourTable 中的新数据插入 myTable,但前提是 myTable 中不存在 key1、key2 组合。
insert into myTable(key1, key2, someData)
values(x, y, z)
where NOT EXISTS (want to check if composite key is not already present)
场景 2:
我将数据从 yourTable 放入一个带有 data1、data2 和 data 属性的 java 对象。
我想插入上面的数据与场景1中的检查一样。 data1 + data2 不应已存在于 myTable 中。
我如何实现这一目标?我认为我们不能在插入语句中使用 SELECT 语句。
insert into myTable(key1, key2, data)
values(data1, data2, data)
where (data1 + data2 are already not present in myTable)
我怎样才能做到这一点?
【问题讨论】:
-
你在什么 DBMS 上做这个?
标签: java sql database db2 composite-key