(`library`.`book_loans`,CONSTRAINT`book_loans_ibfk_3`外键(`isbn`)参考`book`(`isbn`)))

时间:2019-02-23 23:01:02

标签: mysql database foreign-keys

  

java.sql.SQLIntegrityConstraintViolationException:无法添加或更新子行:外键约束失败(librarybook_loans,CONSTRAINT book_loans_ibfk_3外键(isbn)参考文献bookisbn

这是我的表格代码

| book_loans |

CREATE TABLE `book_loans` (
    `loan_id` int(11) NOT NULL,
    `isbn` varchar(10) DEFAULT NULL,
    `Card_ID` mediumint(9) NOT NULL AUTO_INCREMENT,
    `date_out` date DEFAULT NULL,
    `due_date` date DEFAULT NULL,
    `date_in` date DEFAULT NULL,
    PRIMARY KEY (`loan_id`),
    KEY `book_loans_ibfk_2` (`Card_ID`),
    KEY `isbn` (`isbn`),
    CONSTRAINT `book_loans_ibfk_2` FOREIGN KEY (`Card_ID`) 
        REFERENCES `borrower` (`Card_ID`),
    CONSTRAINT `book_loans_ibfk_3` FOREIGN KEY (`isbn`) 
        REFERENCES `book` (`isbn`)
) ENGINE=InnoDB 
    AUTO_INCREMENT=1002 
    DEFAULT CHARSET=utf8mb4 
    COLLATE=utf8mb4_0900_ai_ci

| |

CREATE TABLE `book` (
    `isbn` varchar(10) NOT NULL,
    `title` varchar(500) DEFAULT NULL,
    PRIMARY KEY (`isbn`)
) ENGINE=InnoDB 
    DEFAULT CHARSET=utf8mb4 
    COLLATE=utf8mb4_0900_ai_ci

| 借款人 |

CREATE TABLE `borrower` (
    `Card_ID` mediumint(9) NOT NULL AUTO_INCREMENT,
    `Ssn` varchar(11) DEFAULT NULL,
    `Bname` varchar(50) DEFAULT NULL,
    `Address` varchar(100) DEFAULT NULL,
    `Phone` varchar(15) DEFAULT NULL,
    PRIMARY KEY (`Card_ID`),
    UNIQUE KEY `Ssn` (`Ssn`)
) ENGINE=InnoDB 
    AUTO_INCREMENT=1002 
    DEFAULT CHARSET=utf8mb4 
    COLLATE=utf8mb4_0900_ai_ci

1 个答案:

答案 0 :(得分:-1)

当您为“ book_loans”表的“ isbn”属性插入一个不包含在主键值集中的值时,可能会发生此异常。