problem with foreign key in populating database

2019-08-02 00:31发布

问题:

Deleting previous tuples from flights
 Error: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationExcepti
on: Duplicate entry 'AAH196' for key 'PRIMARY'
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Dupl
icate entry 'AAH196' for key 'PRIMARY'

        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)

        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Sou
rce)
        at java.lang.reflect.Constructor.newInstance(Unknown Source)
        at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
        at com.mysql.jdbc.Util.getInstance(Util.java:384)
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1041)

        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3566)
        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3498)
        at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1959)
        at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2113)
        at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2562)
        at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1664)
        at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1583)
        at Populate.Populate_flights(Populate.java:94)
        at Populate.<init>(Populate.java:36)
        at Populate.main(Populate.java:28)

Deleting previous tuples from reservationsInserting Data into table building Err
or 2: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException
: Cannot add or update a child row: a foreign key constraint fails (`cs585hw3`.`
reservations`, CONSTRAINT `reservations_ibfk_1` FOREIGN KEY (`flight_no`) REFERE
NCES `flights` (`flight_no`))
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cann
ot add or update a child row: a foreign key constraint fails (`cs585hw3`.`reserv
ations`, CONSTRAINT `reservations_ibfk_1` FOREIGN KEY (`flight_no`) REFERENCES `
flights` (`flight_no`))

        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)

        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Sou
rce)
        at java.lang.reflect.Constructor.newInstance(Unknown Source)
        at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
        at com.mysql.jdbc.Util.getInstance(Util.java:384)
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1041)

        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3566)
        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3498)
        at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1959)
        at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2113)
        at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2562)
        at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1664)
        at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1583)
        at Populate.Populate_reservations(Populate.java:126)
        at Populate.<init>(Populate.java:37)
        at Populate.main(Populate.java:28)

I have two tables. flights.data and reservations.data. I am trying to populate the tables via jdbc. While populating it, I am getting the above error.

There is a foreign key reference from reservations to flights.I have to delete records from the secondary table first. This would mean, I should delete records from reservations before I delete from flights. Is that Correct? I checked my table, reservations table has a foreign key. SO I have to delete the data first from this table.

But How do I perform this task?

In the reservation function, I doing like this,

Statement s = conn.createStatement();

        s.executeUpdate("DELETE FROM reservations");

In the table function, it is same like this , instead , I used delete from table.Please tell how would I fix it.

回答1:

Do you know the primary key of the row you're trying to delete in flights? Let's say it is 5.

long flightId = 5;
Statement s = c.prepareStatement("DELETE FROM reservations WHERE flight_id = ?");
s.setLong(1,flightId);
s.executeUpdate();

At that point, you can delete your flight because there will be no reservations pointing back to it.

s.prepareStatement("DELETE FROM flights WHERE id = ?");
s.setLong(1, flightId);
s.executeUpdate();

EDIT: if you want to remove all date from both tables:

Statement s = c.prepareStatement("DELETE FROM reservations");
s.executeUpdate();

s = c.prepareStatement("DELETE FROM flights");
s.executeUpdate();


标签: java mysql jdbc