问题
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