错误:150 / MySQL'无法创建表格'

时间:2016-08-16 18:32:59

标签: mysql database mysql-error-150

所以我通过My​​SQL Workbench中的模型创建者创建了一个数据库模型。但是,当我尝试将其转换为SQL文件时,我遇到了错误代码。错误说

  

无法创建表project2i2wcustomer(错误:150"外键约束形成错误")0.032秒。

这是我的剧本:

-- MySQL Workbench Forward Engineering

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 project2i2w
-- -----------------------------------------------------
DROP SCHEMA IF EXISTS `project2i2w` ;

-- -----------------------------------------------------
-- Schema project2i2w
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `project2i2w` DEFAULT CHARACTER SET utf8 ;
USE `project2i2w` ;

-- -----------------------------------------------------
-- Table `project2i2w`.`services`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `project2i2w`.`services` (
  `service_id` INT NOT NULL AUTO_INCREMENT,
  `service_name` VARCHAR(50) NOT NULL,
  `service_desc` VARCHAR(250) NOT NULL,
  `os_id` INT NOT NULL,
  `customer_id` INT NOT NULL,
  PRIMARY KEY (`service_id`))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `project2i2w`.`customer`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `project2i2w`.`customer` (
  `customer_id` INT NOT NULL AUTO_INCREMENT,
  `first_name` VARCHAR(45) NOT NULL,
  `last_name` VARCHAR(45) NOT NULL,
  `city` VARCHAR(45) NOT NULL,
  `street` VARCHAR(20) NOT NULL,
  `province` VARCHAR(30) NOT NULL,
  `postal` CHAR(7) NOT NULL,
  PRIMARY KEY (`customer_id`),
  CONSTRAINT `fk_customer_services`
    FOREIGN KEY (`customer_id`)
    REFERENCES `project2i2w`.`services` (`customer_id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `project2i2w`.`op_sys`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `project2i2w`.`op_sys` (
  `os_id` INT NOT NULL AUTO_INCREMENT,
  `os_name` VARCHAR(25) NOT NULL,
  `os_desc` VARCHAR(250) NULL,
  `os_ver` VARCHAR(10) NOT NULL,
  PRIMARY KEY (`os_id`),
  CONSTRAINT `fk_op_sys_services1`
    FOREIGN KEY (`os_id`)
    REFERENCES `project2i2w`.`services` (`os_id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;

-- -----------------------------------------------------
-- Data for table `project2i2w`.`services`
-- -----------------------------------------------------
START TRANSACTION;
USE `project2i2w`;
INSERT INTO `project2i2w`.`services` (`service_id`, `service_name`, `service_desc`, `os_id`, `customer_id`) VALUES (1, 'Virus Removal', 'Removing of viruses.', 1, 1);
INSERT INTO `project2i2w`.`services` (`service_id`, `service_name`, `service_desc`, `os_id`, `customer_id`) VALUES (2, 'Tune-up', 'Tuning system performance.', 2, 2);
INSERT INTO `project2i2w`.`services` (`service_id`, `service_name`, `service_desc`, `os_id`, `customer_id`) VALUES (3, 'Rooting', 'Accessing super-user status.', 3, 3);
INSERT INTO `project2i2w`.`services` (`service_id`, `service_name`, `service_desc`, `os_id`, `customer_id`) VALUES (4, 'Restore', 'Restoring to stock factory condition.', 4, 4);
INSERT INTO `project2i2w`.`services` (`service_id`, `service_name`, `service_desc`, `os_id`, `customer_id`) VALUES (5, 'Fix Permissions', 'Fixing file permissions.', 5, 5);

COMMIT;


-- -----------------------------------------------------
-- Data for table `project2i2w`.`customer`
-- -----------------------------------------------------
START TRANSACTION;
USE `project2i2w`;
INSERT INTO `project2i2w`.`customer` (`customer_id`, `first_name`, `last_name`, `city`, `street`, `province`, `postal`) VALUES (1, 'Kathryn', 'Trollinger', 'Kanata', '2337 Merivale Road', 'ON', 'K2K 1L9');
INSERT INTO `project2i2w`.`customer` (`customer_id`, `first_name`, `last_name`, `city`, `street`, `province`, `postal`) VALUES (2, 'Lori ', 'Frederick', 'Toronto', '799 Yonge Street', 'ON', 'M4W 1J7');
INSERT INTO `project2i2w`.`customer` (`customer_id`, `first_name`, `last_name`, `city`, `street`, `province`, `postal`) VALUES (3, 'Maxine ', 'Coley', 'Ingersoll', '1305 Albert Street', 'ON', 'N5C 1S2');
INSERT INTO `project2i2w`.`customer` (`customer_id`, `first_name`, `last_name`, `city`, `street`, `province`, `postal`) VALUES (4, 'June ', 'Harrison', 'Windsor', '1089 Goyeau Ave', 'ON', 'N9A 1H9');
INSERT INTO `project2i2w`.`customer` (`customer_id`, `first_name`, `last_name`, `city`, `street`, `province`, `postal`) VALUES (5, 'Joan ', 'Dunn', 'Oshawa', '615 Toy Avenue', 'ON', 'L1H 7M3');

COMMIT;


-- -----------------------------------------------------
-- Data for table `project2i2w`.`op_sys`
-- -----------------------------------------------------
START TRANSACTION;
USE `project2i2w`;
INSERT INTO `project2i2w`.`op_sys` (`os_id`, `os_name`, `os_desc`, `os_ver`) VALUES (1, 'Windows', 'OS by Microsoft', '10');
INSERT INTO `project2i2w`.`op_sys` (`os_id`, `os_name`, `os_desc`, `os_ver`) VALUES (2, 'Mac OSX', 'OS by Apple', 'El Capitan');
INSERT INTO `project2i2w`.`op_sys` (`os_id`, `os_name`, `os_desc`, `os_ver`) VALUES (3, 'Android', 'OS by Google', 'Marshmellow');
INSERT INTO `project2i2w`.`op_sys` (`os_id`, `os_name`, `os_desc`, `os_ver`) VALUES (4, 'iOS', 'OS by Apple', '10');
INSERT INTO `project2i2w`.`op_sys` (`os_id`, `os_name`, `os_desc`, `os_ver`) VALUES (5, 'Linux', 'Open-source OS', 'Ubuntu');

COMMIT;

如果有人可以为我解决这个问题,并告诉我它为什么不起作用,我会非常感激。

尼克

2 个答案:

答案 0 :(得分:0)

你需要某种关于customer_id的密钥,这样才能解决问题。

CREATE TABLE IF NOT EXISTS `project2i2w`.`services` (
`service_id` INT NOT NULL AUTO_INCREMENT,
`service_name` VARCHAR(50) NOT NULL,
`service_desc` VARCHAR(250) NOT NULL,
`os_id` INT NOT NULL,
`customer_id` INT NOT NULL,
 INDEX `customerid` (`customer_id`),
PRIMARY KEY (`service_id`))
ENGINE = InnoDB;

- 表project2i2wcustomer

CREATE TABLE IF NOT EXISTS `project2i2w`.`customer` (
`customer_id` INT NOT NULL AUTO_INCREMENT,
`first_name` VARCHAR(45) NOT NULL,
`last_name` VARCHAR(45) NOT NULL,
`city` VARCHAR(45) NOT NULL,
`street` VARCHAR(20) NOT NULL,
`province` VARCHAR(30) NOT NULL,
`postal` CHAR(7) NOT NULL,
PRIMARY KEY (`customer_id`),
CONSTRAINT `fk_customer_services`
 FOREIGN KEY (`customer_id`)
 REFERENCES `project2i2w`.`services` (`customer_id`)
 ON DELETE NO ACTION
 ON UPDATE NO ACTION)
  ENGINE = InnoDB;

试试这个

答案 1 :(得分:0)

试试这个:

    CREATE TABLE IF NOT EXISTS `project2i2w`.`services` (
  `service_id` INT NOT NULL AUTO_INCREMENT,
  `service_name` VARCHAR(50) NOT NULL,
  `service_desc` VARCHAR(250) NOT NULL,
  `os_id` INT NOT NULL,
  PRIMARY KEY (`service_id`))
ENGINE = InnoDB;


-- -----------------------------------------------------

CREATE TABLE IF NOT EXISTS `project2i2w`.`customer` (
  `customer_id` INT NOT NULL AUTO_INCREMENT,
  `services_id` INT NOT NULL,
  `first_name` VARCHAR(45) NOT NULL,
  `last_name` VARCHAR(45) NOT NULL,
  `city` VARCHAR(45) NOT NULL,
  `street` VARCHAR(20) NOT NULL,
  `province` VARCHAR(30) NOT NULL,
  `postal` CHAR(7) NOT NULL,
  PRIMARY KEY (`customer_id`),
  CONSTRAINT `fk_customer_services`
    FOREIGN KEY (`services_id`)
    REFERENCES `project2i2w`.`services` (`service_id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

外键必须是参考表的主键