连接到MySQL的JDBC一直告诉我我正在打破外键约束?世界卫生大会?

时间:2011-03-30 00:58:42

标签: java mysql jdbc

好的,所以我不断收到这个错误:
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();

4 个答案:

答案 0 :(得分:2)

您正试图在patron中插入一条记录call_Number,该记录在表book的任何行中都不存在。

答案 1 :(得分:2)

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

答案 2 :(得分:1)

你在patron.call_number中输入的值是否与book.call_number中的值相同?

答案 3 :(得分:1)

执行:

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