我有表用户:
CREATE TABLE IF NOT EXISTS `ePrzychodnia`.`users` (
`id` INT NOT NULL AUTO_INCREMENT ,
`firstName` VARCHAR(45) NOT NULL ,
`lastName` VARCHAR(45) NOT NULL ,
`personalId` VARCHAR(11) NOT NULL ,
`password` VARCHAR(45) NOT NULL ,
`city` VARCHAR(45) NULL ,
`address` VARCHAR(45) NULL ,
`email` VARCHAR(45) NULL ,
`phone` VARCHAR(45) NULL ,
`specialization` VARCHAR(45) NULL ,
`role` VARCHAR(45) NOT NULL ,
`active` TINYINT(1) NOT NULL ,
PRIMARY KEY (`id`) ,
UNIQUE INDEX `personalId_UNIQUE` (`personalId` ASC) )
和餐桌访问。表访问对用户表有两个外键
CREATE TABLE IF NOT EXISTS `ePrzychodnia`.`visits` (
`id` INT NULL AUTO_INCREMENT ,
`doctorId` INT NOT NULL ,
`patientId` INT NULL,
`dateVisit` DATE NOT NULL ,
`hourVisit` DATE NOT NULL ,
PRIMARY KEY (`id`, `doctorId`, `patientId`) ,
INDEX `fk_visits_Users1_idx` (`doctorId` ASC) ,
INDEX `fk_visits_Users2_idx` (`patientId` ASC) ,
CONSTRAINT `fk_visits_Users1`
FOREIGN KEY (`doctorId` )
REFERENCES `ePrzychodnia`.`users` (`id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_visits_Users2`
FOREIGN KEY (`patientId` )
REFERENCES `ePrzychodnia`.`users` (`id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
我设置的第二个外键可以为空,因为我关心在表访问中添加一个条目,该条目将为空。但是,当您尝试添加时出现错误
Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails (eprzychodnia.visits, CONSTRAINT fk_visits_Users2 FOREIGN KEY (patientId) REFERENCES users (id) ON DELETE NO ACTION ON UPDATE NO ACTION)
我该怎么做才能使密钥为空?