当前位置: 代码迷 >> java >> 连接到MySQL的JDBC一直告诉我我打破了外键约束? ha?
  详细解决方案

连接到MySQL的JDBC一直告诉我我打破了外键约束? ha?

热度:93   发布时间:2023-07-17 20:13:35.0

好的,所以我不断收到此错误:
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:无法删除或更新父行:外键约束失败

而且真的很令人沮丧,我不知道为什么吗? 我所做的就是将一个外键分配给另一个表(准确地说是books表)中的主键。 任何指针将不胜感激。

        //create patron table
        s.executeUpdate ("DROP TABLE IF EXISTS patron");
        s.executeUpdate (
            "CREATE TABLE patron ("
            + "patron_ID CHAR(10),"
            + "PRIMARY KEY (patron_ID),"
            + "fName CHAR(50)NOT NULL, MI CHAR(2), lName CHAR(75)NOT NULL,"
            + "street_Name CHAR(100)NOT NULL, city CHAR(50) NOT NULL, state CHAR(50)NOT NULL,"
            + "zip_Code CHAR(10) NOT NULL, home_Phone CHAR(12)NOT NULL, call_Number CHAR(10)NOT NULL,"
            //+ "FOREIGN KEY (call_Number) references corejava.book(call_Number))");
            + "FOREIGN KEY (call_Number) references " + dbName + ".book (call_Number))");

        count2 = s.executeUpdate (
            "INSERT INTO patron"
            + " VALUES"
            + "('P222200000', 'Harry', 'P','Bradford','1234 Street Place','Silver Spring','Maryland','20906','301-555-9999','MY.111.0001'),"
            + "('P222200001', 'Sally','','Titus','2365 Huckleberry Lane','Silver Spring','Maryland','20906','301-554-9896','SF.111.002'),"
            + "('P222200002', 'Mark', 'C','Bradley','654 Finance Lane','Silver Spring','Maryland','20906','301-665-4978','AV.111.004'),"
            + "('P222200003', 'Carlos','','Iglesias','987 Potter Place','Silver Spring','Maryland','20906','240-702-5648','CO.111.006'),"
            + "('P222200004', 'Chris','','Craig','951 Einstein Way','Silver Spring','Maryland','20906','301-569-4415','IN.111.008'),"
            + "('P222200005', 'Samantha','A','Brikmon',' 8380 Colesville Road','Silver Spring','20906','301-659-5569','CO.111.007'),"
            + "('P222200006', 'Lindsey', '','Saucer','12507 Winexburg Manor Drive','Silver Spring','Maryland','20906','301-632-6635','SF.111.003'),"
            + "('P222200007', 'Judy', 'A','Freud','2518 Woodedge Road','Glenmont','Maryland','20905','240-985-9632','AV.111.005'),"
            + "('P222200008', 'Elizabeth','M','Longhorn','552 Lockwood Drive','Wheaton','Maryland','20904','202-555-6639','IN.111.009'),"
            + "('P222200009', 'Gabriella', 'S','Young','654 Glenallen Road','Silver Spring','Maryland','20906','301-555-9898','MY.111.000')");

        System.out.println (count2 + " rows were inserted");
        s.close();

你试图插入一条记录patron ,有一个call_Number不表任何行存在book

这意味着在您插入顾客表的最后一列(例如,第一条记录具有'MY.111.0001' )中, book.call_number不存在一些值。 这是外键的目的,以确保一致地引用数据! 允许插入将使patron.call_number某些值无效。

您要在patron.call_number中输入的值与book.call_number中的值相同吗?

执行:

select * from book where call_number = 'MY.111.0001';

对于每个电话号码(上述MY.111.0001 ,但依次替换),您尝试插入patron以确保该行存在。

那应该向您显示导致约束冲突的特定记录,这时您可以使用其他电话号码,也可以将该电话号码的行插入book

或者,您可以执行:

select call_number from book where call_number in (
    'MY.111.0001',
    'SF.111.002',
    'AV.111.004',
    'CO.111.006',
    'IN.111.008',
    'CO.111.007',
    'SF.111.003',
    'AV.111.005',
    'IN.111.009',
    'MY.111.000')

并查看其中哪些未显示在输出中。

当他们排列好后,看看第一个的长度,我怀疑这是一个问题-也许应该是MY.111.001吗?

  相关解决方案