填充数据库时出现外键问题

时间:2010-11-07 23:24:41

标签: java mysql jdbc

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)

我有两张桌子。 flights.data和reservations.data。我试图通过jdbc填充表。在填充它时,我收到了上述错误。

从预订到航班有一个外键引用。我必须先从辅助表中删除记录。这意味着,我应该在删除航班之前从预订中删除记录。那是对的吗?我检查了我的表,预订表有一个外键。所以我必须先从这张表中删除数据。

但我该如何执行此任务?

在预约功能中,我这样做,

Statement s = conn.createStatement();

        s.executeUpdate("DELETE FROM reservations");

在table函数中,它是这样的,相反,我使用了table中的delete。请告诉我如何修复它。

1 个答案:

答案 0 :(得分:1)

您是否知道您尝试在航班中删除的行的主键?假设它是5。

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

此时,您可以删除您的航班,因为没有任何预订指向它。

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

编辑:如果你想从两个表中删除所有日期:

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

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