我尝试转发设计我的ERD并且不断收到以下错误消息。我真的不明白这意味着什么,有人可以解释我需要解决的问题吗?
在服务器
中执行SQL脚本错误:错误1452:无法添加或更新子行:外键 约束失败(
database
。employee
,CONSTRAINTfk_employee_department1
FOREIGN KEY(dpt_id
)参考department
(dpt_id
)在更新时不执行任何操作的行动)
SQL代码:
INSERT INTO `database`.`employee` (`emp_id`, `dpt_id`, `emp_fname`, `emp_lname`, `emp_street`, `emp_city`, `emp_state`, `emp_zip`, `emp_phone`, `emp_email`, `emp_notes`) VALUES (NULL, 1, 'Harry', 'Potter', '4 Privet Drive', 'Tallahassee', 'Fl', '32304', '9544564567', 'hpotter@abc.com', NULL)
SQL脚本执行完成:语句:32成功,1失败
以最终形式获取视图定义。 没什么可取的
这是所有的脚本,就像我说我很困惑,不知道在哪里解决它
SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES';
-- -----------------------------------------------------
-- Schema database
-- -----------------------------------------------------
DROP SCHEMA IF EXISTS `database` ;
CREATE SCHEMA IF NOT EXISTS `database` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
SHOW WARNINGS;
USE `database` ;
-- -----------------------------------------------------
-- Table `database`.`employee`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `database`.`employee` ;
SHOW WARNINGS;
CREATE TABLE IF NOT EXISTS `database`.`employee` (
`emp_id` TINYINT UNSIGNED NOT NULL AUTO_INCREMENT,
`dpt_id` TINYINT UNSIGNED NULL,
`emp_fname` VARCHAR(20) NOT NULL,
`emp_lname` VARCHAR(20) NOT NULL,
`emp_street` VARCHAR(20) NOT NULL,
`emp_city` VARCHAR(20) NOT NULL,
`emp_state` CHAR(2) NOT NULL,
`emp_zip` CHAR(9) NOT NULL,
`emp_phone` CHAR(10) NOT NULL,
`emp_email` VARCHAR(100) NOT NULL,
`emp_notes` VARCHAR(255) NULL,
PRIMARY KEY (`emp_id`),
INDEX `fk_employee_department1_idx` (`dpt_id` ASC),
CONSTRAINT `fk_employee_department1`
FOREIGN KEY (`dpt_id`)
REFERENCES `database`.`department` (`dpt_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
SHOW WARNINGS;
-- -----------------------------------------------------
-- Table `database`.`division`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `database`.`division` ;
SHOW WARNINGS;
CREATE TABLE IF NOT EXISTS `database`.`division` (
`div_id` TINYINT UNSIGNED NOT NULL AUTO_INCREMENT,
`emp_id` TINYINT UNSIGNED NOT NULL,
`div_name` VARCHAR(45) NOT NULL,
`div_street` VARCHAR(30) NOT NULL,
`div_city` VARCHAR(20) NOT NULL,
`div_state` CHAR(2) NOT NULL,
`div_zip` CHAR(9) NOT NULL,
`div_phone` CHAR(10) NOT NULL,
`div_email` VARCHAR(100) NOT NULL,
`div_url` VARCHAR(100) NOT NULL,
`div_notes` VARCHAR(255) NULL,
PRIMARY KEY (`div_id`),
INDEX `fk_division_employee1_idx` (`emp_id` ASC),
CONSTRAINT `fk_division_employee1`
FOREIGN KEY (`emp_id`)
REFERENCES `database`.`employee` (`emp_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
SHOW WARNINGS;
-- -----------------------------------------------------
-- Table `database`.`department`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `database`.`department` ;
SHOW WARNINGS;
CREATE TABLE IF NOT EXISTS `database`.`department` (
`dpt_id` TINYINT UNSIGNED NOT NULL AUTO_INCREMENT,
`div_id` TINYINT UNSIGNED NOT NULL,
`emp_id` TINYINT UNSIGNED NOT NULL,
`dpt_name` VARCHAR(45) NOT NULL,
`dpt_street` VARCHAR(30) NOT NULL,
`dpt_city` VARCHAR(20) NOT NULL,
`dpt_state` CHAR(2) NOT NULL,
`dpt_zip` CHAR(9) NOT NULL,
`dpt_phone` CHAR(10) NOT NULL,
`dpt_email` VARCHAR(100) NOT NULL,
`dpt_notes` VARCHAR(255) NULL,
PRIMARY KEY (`dpt_id`),
INDEX `fk_department_employee1_idx` (`emp_id` ASC),
INDEX `fk_department_division1_idx` (`div_id` ASC),
CONSTRAINT `fk_department_employee1`
FOREIGN KEY (`emp_id`)
REFERENCES `database`.`employee` (`emp_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_department_division1`
FOREIGN KEY (`div_id`)
REFERENCES `database
`.`division` (`div_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
SHOW WARNINGS;
-- -----------------------------------------------------
-- Table `database`.`project`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `database`.`project` ;
SHOW WARNINGS;
CREATE TABLE IF NOT EXISTS `database`.`project` (
`pro_id` TINYINT UNSIGNED NOT NULL AUTO_INCREMENT,
`pro_name` VARCHAR(45) NOT NULL,
`pro_notes` VARCHAR(255) NULL,
PRIMARY KEY (`pro_id`))
ENGINE = InnoDB;
SHOW WARNINGS;
-- -----------------------------------------------------
-- Table `database`.`assignment`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `database`.`assignment` ;
SHOW WARNINGS;
CREATE TABLE IF NOT EXISTS `database`.`assignment` (
`asn_id` TINYINT UNSIGNED NOT NULL AUTO_INCREMENT,
`emp_id` TINYINT UNSIGNED NOT NULL,
`pro_id` TINYINT UNSIGNED NOT NULL,
`asn_name` VARCHAR(45) NOT NULL,
`asn_start_date` DATE NOT NULL,
`asn_end_date` DATE NOT NULL,
`asn_duration` TINYINT UNSIGNED NOT NULL,
`asn_eval` VARCHAR(200) NOT NULL,
`asn_notes` VARCHAR(255) NULL,
PRIMARY KEY (`asn_id`),
INDEX `fk_assignment_employee1_idx` (`emp_id` ASC),
INDEX `fk_assignment_project1_idx` (`pro_id` ASC),
CONSTRAINT `fk_assignment_employee1`
FOREIGN KEY (`emp_id`)
REFERENCES `database`.`employee` (`emp_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_assignment_project1`
FOREIGN KEY (`pro_id`)
REFERENCES `database`.`project` (`pro_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
SHOW WARNINGS;
SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
-- -----------------------------------------------------
-- Data for table `database`.`employee`
-- -----------------------------------------------------
START TRANSACTION;
USE `database`;
INSERT INTO `database`.`employee` (`emp_id`, `dpt_id`, `emp_fname`, `emp_lname`, `emp_street`, `emp_city`, `emp_state`, `emp_zip`, `emp_phone`, `emp_email`, `emp_notes`) VALUES (NULL, 1, 'Harry', 'Potter', '4 Privet Drive', 'Tallahassee', 'Fl', '32304', '9544564567', 'hpotter@abc.com', NULL);
INSERT INTO `database`.`employee` (`emp_id`, `dpt_id`, `emp_fname`, `emp_lname`, `emp_street`, `emp_city`, `emp_state`, `emp_zip`, `emp_phone`, `emp_email`, `emp_notes`) VALUES (NULL, 2, 'Sherlock ', 'Holmes', '221 Baker Street', 'Tallahassee', 'Fl', '32304', '9045674576', 'sholmes@abc.com', NULL);
INSERT INTO `database`.`employee` (`emp_id`, `dpt_id`, `emp_fname`, `emp_lname`, `emp_street`, `emp_city`, `emp_state`, `emp_zip`, `emp_phone`, `emp_email`, `emp_notes`) VALUES (NULL, 3, 'James', 'Kirk', '22 Space Drive', 'Tallahassee', 'Fl', '32304', '8768765645', 'jkirk@abc.com', NULL);
INSERT INTO `database`.`employee` (`emp_id`, `dpt_id`, `emp_fname`, `emp_lname`, `emp_street`, `emp_city`, `emp_state`, `emp_zip`, `emp_phone`, `emp_email`, `emp_notes`) VALUES (NULL, 4, 'Garrus', 'Vakarian', '1313 Zakera Street', 'Tallahassee', 'Fl', '32304', '7897656787', 'gvakarian@abc.com', NULL);
INSERT INTO `database`.`employee` (`emp_id`, `dpt_id`, `emp_fname`, `emp_lname`, `emp_street`, `emp_city`, `emp_state`, `emp_zip`, `emp_phone`, `emp_email`, `emp_notes`) VALUES (NULL, 5, 'Bruce ', 'Wayne', '45 Gotham Boulevard', 'Tallahassee', 'Fl', '32304', '6754563412', 'bwayne@abc.com', NULL);
COMMIT;
-- -----------------------------------------------------
-- Data for table `database`.`division`
-- -----------------------------------------------------
START TRANSACTION;
USE `database`;
INSERT INTO `database`.`division` (`div_id`, `emp_id`, `div_name`, `div_street`, `div_city`, `div_state`, `div_zip`, `div_phone`, `div_email`, `div_url`, `div_notes`) VALUES (NULL, 1, 'Repairs', '123 Main Street', 'Tallahassee', 'Fl', '32304', '8506445677', 'repairs@abc.com', 'abcrepairs.com', NULL);
INSERT INTO `database`.`division` (`div_id`, `emp_id`, `div_name`, `div_street`, `div_city`, `div_state`, `div_zip`, `div_phone`, `div_email`, `div_url`, `div_notes`) VALUES (NULL, 2, 'Management', '124 Side Street', 'Tallahassee', 'Fl', '32304', '8506445332', 'management@abc.com', 'abcmanagement.com', NULL);
INSERT INTO `database`.`division` (`div_id`, `emp_id`, `div_name`, `div_street`, `div_city`, `div_state`, `div_zip`, `div_phone`, `div_email`, `div_url`, `div_notes`) VALUES (NULL, 3, 'Delivery', '125 Adjacent Avenue', 'Tallahassee', 'Fl', '32304', '8506445666', 'delivery@abc.com', 'abcdelivery.com', NULL);
INSERT INTO `database`.`division` (`div_id`, `emp_id`, `div_name`, `div_street`, `div_city`, `div_state`, `div_zip`, `div_phone`, `div_email`, `div_url`, `div_notes`) VALUES (NULL, 4, 'Stock', '126 Cross Boulevard', 'Tallahassee', 'Fl', '32304', '8506444444', 'stock@abc.com', 'abcstock.com', NULL);
INSERT INTO `database`.`division` (`div_id`, `emp_id`, `div_name`, `div_street`, `div_city`, `div_state`, `div_zip`, `div_phone`, `div_email`, `div_url`, `div_notes`) VALUES (NULL, 5, 'Technology', '127 Different Avenue', 'Tallahassee', 'Fl', '32304', '8506445656', 'tech@abc.com', 'abctech.com', NULL);
COMMIT;
-- -----------------------------------------------------
-- Data for table `database`.`department`
-- -----------------------------------------------------
START TRANSACTION;
USE `database`;
INSERT INTO `database`.`department` (`dpt_id`, `div_id`, `emp_id`, `dpt_name`, `dpt_street`, `dpt_city`, `dpt_state`, `dpt_zip`, `dpt_phone`, `dpt_email`, `dpt_notes`) VALUES (NULL, 1, 1, 'Computer Repair', '123 Main Street', 'Tallahassee', 'Fl', '32304', '8506443567', 'comprepair@abc.com', NULL);
INSERT INTO `database`.`department` (`dpt_id`, `div_id`, `emp_id`, `dpt_name`, `dpt_street`, `dpt_city`, `dpt_state`, `dpt_zip`, `dpt_phone`, `dpt_email`, `dpt_notes`) VALUES (NULL, 2, 2, 'Customer Service', '124 Side Street', 'Tallahassee', 'Fl', '32304', '8506445678', 'customers@abc.com', NULL);
INSERT INTO `database`.`department` (`dpt_id`, `div_id`, `emp_id`, `dpt_name`, `dpt_street`, `dpt_city`, `dpt_state`, `dpt_zip`, `dpt_phone`, `dpt_email`, `dpt_notes`) VALUES (NULL, 3, 3, 'Warehouse', '125 Adjacent Avenue', 'Tallahassee', 'Fl', '32304', '8506443471', 'warehouse@abc.com', NULL);
INSERT INTO `database`.`department` (`dpt_id`, `div_id`, `emp_id`, `dpt_name`, `dpt_street`, `dpt_city`, `dpt_state`, `dpt_zip`, `dpt_phone`, `dpt_email`, `dpt_notes`) VALUES (NULL, 4, 4, 'Stockroom', '126 Cross Boulevard', 'Tallahassee', 'Fl', '32304', '8506447889', 'stockroom@abc.com', NULL);
INSERT INTO `database`.`department` (`dpt_id`, `div_id`, `emp_id`, `dpt_name`, `dpt_street`, `dpt_city`, `dpt_state`, `dpt_zip`, `dpt_phone`, `dpt_email`, `dpt_notes`) VALUES (NULL, 5, 5, 'IT', '127 Different Avenue', 'Tallahassee', 'Fl', '32304', '8506445633', 'it@abc.com', NULL);
COMMIT;
-- -----------------------------------------------------
-- Data for table `database`.`project`
-- -----------------------------------------------------
START TRANSACTION;
USE `database`;
INSERT INTO `database`.`project` (`pro_id`, `pro_name`, `pro_notes`) VALUES (NULL, '1', NULL);
INSERT INTO `database`.`project` (`pro_id`, `pro_name`, `pro_notes`) VALUES (NULL, '2', NULL);
INSERT INTO `database`.`project` (`pro_id`, `pro_name`, `pro_notes`) VALUES (NULL, '3', NULL);
COMMIT;
-- -----------------------------------------------------
-- Data for table `database`.`assignment`
-- -----------------------------------------------------
START TRANSACTION;
USE `database`;
INSERT INTO `database`.`assignment` (`asn_id`, `emp_id`, `pro_id`, `asn_name`, `asn_start_date`, `asn_end_date`, `asn_duration`, `asn_eval`, `asn_notes`) VALUES (NULL, 1, 1, 'Assignment 1', '2013-12-14', '2013-12-19', 5, '5', NULL);
INSERT INTO `database`.`assignment` (`asn_id`, `emp_id`, `pro_id`, `asn_name`, `asn_start_date`, `asn_end_date`, `asn_duration`, `asn_eval`, `asn_notes`) VALUES (NULL, 2, 2, 'Assignment 2', '2014-01-24', '2014-01-25', 1, '4', NULL);
INSERT INTO `database`.`assignment` (`asn_id`, `emp_id`, `pro_id`, `asn_name`, `asn_start_date`, `asn_end_date`, `asn_duration`, `asn_eval`, `asn_notes`) VALUES (NULL, 3, 3, 'Assignment 3', '2014-01-01', '2014-02-01', 30, '4', NULL);
INSERT INTO `database`.`assignment` (`asn_id`, `emp_id`, `pro_id`, `asn_name`, `asn_start_date`, `asn_end_date`, `asn_duration`, `asn_eval`, `asn_notes`) VALUES (NULL, 4, 1, 'Assignment 4', '2014-03-03', '2014-03-04', 1, '4', NULL);
INSERT INTO `database`.`assignment` (`asn_id`, `emp_id`, `pro_id`, `asn_name`, `asn_start_date`, `asn_end_date`, `asn_duration`, `asn_eval`, `asn_notes`) VALUES (NULL, 5, 2, 'Assignment 5', '2014-03-04', '2014-03-06', 3, '2', NULL);
COMMIT;
答案 0 :(得分:0)
您需要先将条目插入deparment
表
employee
表有deparment
表的外键,因为{1}}表中没有id为1的表项,插入失败。
您的department
表格department
也有外键,这将是递归的。
您需要更改表格结构。
答案 1 :(得分:0)
下面的错误行清楚地告诉你问题是什么。 dpt_id
表上的Employee
与Department
表dpt_id
列具有外键关系。
因此,在将相同的密钥插入Department
表之前,必须先向employee
表插入一行。换句话说,在您将该键插入任何引用表之前,dpt_id
表中必须存在相同的department
。
CONSTRAINT fk_employee_department1 FOREIGN KEY(dpt_id)参考 部门(dpt_id)