gpt4 book ai didi

java - 使用 Java 和 SQL 删除两个表中的行

转载 作者:行者123 更新时间:2023-12-01 23:01:42 25 4
gpt4 key购买 nike

我已将 Java 连接到 SQL,并希望从不同的表中删除两行,但我的 Java 方法出现问题。我试图从两个表中删除,因为我无法从 Table1 中删除 ID,因为它被 table2 引用。关于解决这个问题有什么建议吗?

public void delete() throws SQLException
{
DataConnection connection = new DataConnection();

String query = " DELETE FROM " + Table1 + " WHERE ID = " + id + " AND " + " DELETE FROM " + Table2 + " WHERE ID = " + id;
connection.updateData(query);
connection.closeConnection();
}

最佳答案

See Always child references should be deleted first.

if there are any references to parent table then parent table can not be deleted.

but if the parent table has cascade delete then it can delete all child tables while deleting table.

in your case table1 is parent as it is referencing from one of the child table table2.

so delete table2 first then any other tables if they are also referencing table1's id.

then try to delete table1.

it should work.

let me know for any issues.

关于java - 使用 Java 和 SQL 删除两个表中的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23313653/

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