gpt4 book ai didi

java - DB2 SQL 错误 : -803 when inserting into two related tables

转载 作者:搜寻专家 更新时间:2023-10-31 08:17:03 25 4
gpt4 key购买 nike

我有两个使用以下语句创建的表:

CREATE TABLE Behandlungsvorgang (
patientId SMALLINT NOT NULL REFERENCES Patient(id),
datum DATE NOT NULL,
notizen VARCHAR(100),
PRIMARY KEY (patientId, datum)
);

CREATE TABLE behandelt (
arztLogin VARCHAR(50) NOT NULL REFERENCES Arzt(login),
behandlungsDatum DATE NOT NULL,
behandlungsPatientId SMALLINT NOT NULL,
medikamntPzn SMALLINT NOT NULL REFERENCES Medikament(pzn),
krankheitName VARCHAR(50) NOT NULL REFERENCES Krankheit(name),
PRIMARY KEY (arztLogin, behandlungsDatum, behandlungsPatientId, medikamntPzn, krankheitName),
FOREIGN KEY (behandlungsDatum, behandlungsPatientId) REFERENCES Behandlungsvorgang(datum, patientId)
);

我有一个方法可以将数据插入到这个表中。它总是插入新数据,因此在插入 behandelt 之前,我必须插入 Behandlungsvorgang 以满足外键要求。该方法如下所示:

public void add(TreatmentProcess tp) throws StoreException {
try {
PreparedStatement psBehandlungsvorgang = connection.prepareStatement("INSERT INTO Behandlungsvorgang (patientId, datum, notizen) VALUES (?, ?, ?)");
psBehandlungsvorgang.setInt(1, tp.getPatientId());
psBehandlungsvorgang.setDate(2, tp.getDate());
psBehandlungsvorgang.setString(3, tp.getNotes());

psBehandlungsvorgang.executeUpdate();

PreparedStatement psBehandelt = connection.prepareStatement("INSERT INTO behandelt (arztLogin, behandlungsDatum, behandlungsPatientId, medikamntPzn, krankheitName) VALUES (?, ?, ?, ?, ?)");

for (Drug drug : tp.getDrugs()) {
psBehandelt.setString(1, tp.getDoctor());
psBehandelt.setDate(2, tp.getDate());
psBehandelt.setInt(3, tp.getPatientId());
psBehandelt.setInt(4, drug.getPzn());
psBehandelt.setString(5, tp.getDisease());
psBehandelt.addBatch();
}

psBehandelt.executeBatch();
} catch (SQLException e) {
throw new StoreException(e);
}
}

我总是得到一个异常,说 de.unidue.inf.is.stores.StoreException: com.ibm.db2.jcc.am.go: DB2 SQL Error: SQLCODE=-803, SQLSTATE=23505, SQLERRMC =1;DBP10.BEHANDLUNGSVORGANG,驱动程序=4.7.85。如果我手动插入数据,我不会出错。例如:

insert into Behandlungsvorgang values (1, '2014-01-25', 'Test');
insert into behandelt values ('doc', '2014-01-25', 1, 1, 'Kater');
insert into behandelt values ('doc', '2014-01-25', 1, 2, 'Kater');

我在 java 代码中做错了什么?

最佳答案

-803意味着您正在尝试插入违反表的唯一约束的行。如果您使用的是 DB2 Linux/Unix/Windows,则可以从 SQLERRMC 中获取该数字并将其应用于此查询以获取您违反的索引:

SELECT INDNAME, INDSCHEMA
FROM SYSCAT.INDEXES
WHERE IID = <index-id>
AND TABSCHEMA = 'schema'
AND TABNAME = 'table'

关于java - DB2 SQL 错误 : -803 when inserting into two related tables,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21479931/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com