我正在创建一个数据库并在其中添加表格。我正在构建一个关联表,但我无法在其中添加外键。我不明白为什么不让我这样做。
ERROR: Error 1215: Cannot add foreign key constraint
CREATE TABLE IF NOT EXISTS `Hospital_Management_System`.`Bill_Item` (
`billId` INT NOT NULL COMMENT '',
`itemNumber` INT NOT NULL COMMENT '',
INDEX `billId_idx` (`billId` ASC) COMMENT '',
INDEX `itemNumber_idx` (`itemNumber` ASC) COMMENT '',
CONSTRAINT `billId`
FOREIGN KEY (`billId`)
REFERENCES `Hospital_Management_System`.`Patient_Bill` (`billId`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `itemNumber`
FOREIGN KEY (`itemNumber`)
REFERENCES `Hospital_Management_System`.`Patient_Bill_Item` (`itemNumber`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
基表是
CREATE TABLE IF NOT EXISTS `Hospital_Management_System`.`Patient_Bill` (
`patientId` INT NOT NULL COMMENT '',
`billId` INT NOT NULL COMMENT '',
`datebillPaid` DATE NOT NULL COMMENT '',
`amountDue` INT NOT NULL COMMENT '',
PRIMARY KEY (`billId`) COMMENT '',
INDEX `patientIdNumber_idx` (`patientId` ASC) COMMENT '',
CONSTRAINT `patientId`
FOREIGN KEY (`patientId`)
REFERENCES `Hospital_Management_System`.`Patient` (`patientId`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
和第二个表是
CREATE TABLE IF NOT EXISTS `Hospital_Management_System`.`Patient_Bill_Item` (
`itemNumber` INT NOT NULL COMMENT '',
`quantity` INT NOT NULL COMMENT '',
`totalAmount` INT NOT NULL COMMENT '',
PRIMARY KEY (`itemNumber`) COMMENT '')
ENGINE = InnoDB;