gpt4 book ai didi

Java 甲骨文事务

转载 作者:行者123 更新时间:2023-11-30 05:56:10 26 4
gpt4 key购买 nike

我想将数据插入到数据库中的不同表中。由于限制,我必须按特定顺序执行此操作。这意味着,首先插入表 a,然后是 b,然后是 c,.... 并且不混合表。但是我正在编写一个程序,它获取多个 csv 文件并且应该将它们导入数据库,但是程序不知道什么是正确的顺序。所以我认为事务是正确的方式,因为我听说,数据一致性必须只存在于事务结束时。但这不起作用

我的代码是这样的:

Connection connection = DriverManager.getConnection(url, user, pw);
connection.setAutoCommit(false);
Statement statement = connection.createStatement();
statement.addBatch("INSERT INTO c ....");
statement.addBatch("INSERT INTO a ....");
statement.addBatch("INSERT INTO b ....");
statement.addBatch("INSERT INTO a ....");
// ...
statement.executeBatch();
statement.close();
connection.commit();

但我会得到 ORA-02291(违反完整性约束):-(

最佳答案

您需要使约束可延迟,这样直到提交时才检查它。有一篇很好的文章here

> drop table c
table C dropped.
> drop table p
table P dropped.
> create table p (id number primary key)
table P created.
> create table c (id number primary key, p_id number)
table C created.
> alter table c add constraint pk_p foreign key (p_id) references p (id) deferrable
table C altered.
> insert into c values ( 1, 1 )
1 rows inserted.
> insert into p values ( 1 )
1 rows inserted.
> commit
commited.

关于Java 甲骨文事务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7486239/

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