一直在看这两天,我不知道这意味着什么。我知道这意味着“正是它所说的”,但我不知道如何解决它。有人可以用外行的话向我解释并帮助我吗?
ERROR: Error 1452: Cannot add or update a child row: a foreign key constraint fails
(`sls11n`.`dependent`, CONSTRAINT `fk_dependent_employee1` FOREIGN KEY (`emp_id`)
REFERENCES `employee` (`emp_id`) ON DELETE NO ACTION ON UPDATE NO ACTION)
INSERT INTO `sls11n`.`dependent` (`dep_id`, `emp_id`, `dep_ssn`, `dep_fname`, `dep_lname`,
`dep_street`, `dep_city`, `dep_state`, `dep_zip`, `dep_phone`, `dep_email`, `dep_notes`)
VALUES (NULL, 13, 123456789, 'Gary', 'Hart', 'West St', 'San Diego', 'CA', '23424',
'1234567890', 'garyhart@me.com', NULL)
以下是我认为相关的脚本:
-- -----------------------------------------------------
-- Table `sls11n`.`employee`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `sls11n`.`employee` ;
SHOW WARNINGS;
CREATE TABLE IF NOT EXISTS `sls11n`.`employee` (
`emp_id` TINYINT UNSIGNED NOT NULL AUTO_INCREMENT,
`emp_ssn` INT UNSIGNED NOT NULL,
`emp_fname` VARCHAR(15) NOT NULL,
`emp_lname` VARCHAR(20) NOT NULL,
`emp_street` VARCHAR(45) NOT NULL,
`emp_city` VARCHAR(45) NOT NULL,
`emp_state` CHAR(2) NOT NULL,
`emp_zip` CHAR(9) NOT NULL,
`emp_phone` CHAR(15) NOT NULL,
`emp_email` VARCHAR(100) NOT NULL,
`emp_doh` DATE NOT NULL,
`emp_is_inspect` ENUM('y', 'n') NOT NULL,
`emp_notes` VARCHAR(250) NULL,
PRIMARY KEY (`emp_id`))
ENGINE = InnoDB;
SHOW WARNINGS;
-- -----------------------------------------------------
-- Table `sls11n`.`dependent`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `sls11n`.`dependent` ;
SHOW WARNINGS;
CREATE TABLE IF NOT EXISTS `sls11n`.`dependent` (
`dep_id` TINYINT UNSIGNED NOT NULL AUTO_INCREMENT,
`emp_id` TINYINT UNSIGNED NOT NULL,
`dep_ssn` INT UNSIGNED NOT NULL,
`dep_fname` VARCHAR(45) NOT NULL,
`dep_lname` VARCHAR(45) NOT NULL,
`dep_street` VARCHAR(45) NOT NULL,
`dep_city` VARCHAR(45) NOT NULL,
`dep_state` CHAR(2) NOT NULL,
`dep_zip` CHAR(9) NOT NULL,
`dep_phone` CHAR(10) NOT NULL,
`dep_email` VARCHAR(100) NOT NULL,
`dep_notes` VARCHAR(250) NULL,
PRIMARY KEY (`dep_id`),
INDEX `fk_dependent_employee1_idx` (`emp_id` ASC),
CONSTRAINT `fk_dependent_employee1`
FOREIGN KEY (`emp_id`)
REFERENCES `sls11n`.`employee` (`emp_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
任何人都有任何想法?请使用简单的术语,当我说我不理解时,我不是在开玩笑。
答案 0 :(得分:1)
外键约束表示每个从属emp_id
必须与emp_id
表中的现有employee
匹配。因此,在将此行添加到dependent
表之前,您必须使用employee
向emp_id = 13
添加一行。