Delete from two tables in one statement

和自甴很熟 提交于 2019-12-24 16:22:50

问题


I want to be able to remove all data from 2 tables where the id of a user = the id given. I am using Java, Derby DB, Netbeans 7.3 Beta 2 and OS X Mountain Lion.

I have two tables (sorry about the huge image):

This is my statement so far:

String stmt2 = "DELETE FROM APP.PERSON JOIN APP.DATAVAULT WHERE PID = ?";                    
PreparedStatement ps2 = Main.getPreparedStatement(stmt2);
ps2 = conn.prepareStatement(stmt2);
ps2.setInt(1, user.getId());
ps2.executeUpdate();
System.out.println("Deleted");

I don't understand how I delete from APP.DATAVAULT as well as APP.PERSON. As you can see there is a foreign key within APP.DATAVAULT which is a users id.

I have tried many things such as:

String stmt2 = "DELETE FROM APP.PERSON, APP.DATAVAULT WHERE PID = ?";

and

String stmt2 = "DELETE FROM APP.PERSON AND APP.DATAVAULT WHERE PID = ?";

I understand that I must use the foreign key to delete from both, but I don't know how.


回答1:


Unfortunately, per the docs, you cannot delete multiple tables with a single SQL query in Derby.

You can with some other RDBMS packages, such as MySQL... just not Derby.



来源:https://stackoverflow.com/questions/15468505/delete-from-two-tables-in-one-statement

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!