我有两张桌子,都带有几个外键。这些是表创建脚本:
-- -----------------------------------------------------
-- Table `mydb`.`Order`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Order` (
`order_id` INT NOT NULL AUTO_INCREMENT,
`verznr` INT NOT NULL,
`datum` DATE NOT NULL,
`status` VARCHAR(45) NOT NULL,
`huisarts` VARCHAR(45) NOT NULL,
`apotheker` VARCHAR(45) NOT NULL,
PRIMARY KEY (`order_id`, `verznr`, `huisarts`, `apotheker`),
INDEX `order_huisarts_idx` (`huisarts` ASC),
INDEX `order_apotheker_idx` (`apotheker` ASC)
)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Patient`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Patient` (
`verznr` INT NOT NULL AUTO_INCREMENT,
`achternaam` VARCHAR(45) NOT NULL,
`geboorteplaats` VARCHAR(45) NOT NULL,
`adres` VARCHAR(45) NOT NULL,
`tel` INT(4) NOT NULL,
`postcode` VARCHAR(7) NOT NULL,
`apotheker` VARCHAR(45) NOT NULL,
`huisarts` VARCHAR(45) NOT NULL,
`post` VARCHAR(45) NOT NULL,
`idenid` INT(10) NOT NULL,
PRIMARY KEY (`verznr`, `huisarts`, `apotheker`),
CONSTRAINT `pat_order`
FOREIGN KEY (`verznr`)
REFERENCES `mydb`.`Order` (`verznr`)
ON DELETE NO ACTION
ON UPDATE NO ACTION
)
ENGINE = InnoDB;
这是在mysql workbench中作为模型创建的,然后导出到sql create脚本中。出于某种原因,它给出了从患者到订单的外键错误。我把它缩小到了那个范围。在两个表中verznr
都是INT not null和主键。当创建外键时,它不起作用。