MySQL 150:外键约束格式错误

时间:2018-06-23 15:28:30

标签: mysql foreign-keys mysql-error-150

当我将文件导入phpmyadmin时,出现此错误:

  

#1005-无法创建表`test_db`.`part_order`(错误号:150   “外键约束的格式不正确”)

我检查了很多次语法,但是看不到有什么问题。我尝试使用INDEXCONSTRAINT,但没有成功。任何帮助将不胜感激。

CREATE TABLE IF NOT EXISTS `all_products` (   
  `product_id` int(10) NOT NULL AUTO_INCREMENT,   
  `product_name` VARCHAR(100)  NOT NULL,  
  `product_price` decimal(10,2) NOT NULL,   
  `product_description` VARCHAR(1000) NOT NULL,  
  PRIMARY KEY(`product_id`, `product_name`)   
) ENGINE=InnoDB DEFAULT CHARSET=UTF8;

CREATE TABLE IF NOT EXISTS `product_color` (   
  `color_id` int(10) NOT NULL AUTO_INCREMENT,   
  `product_id` INT(10) NOT NULL,   
  `color_name` VARCHAR(100) NOT NULL,   
  PRIMARY KEY(`color_id`, `color_name`),   
  FOREIGN KEY(`product_id`) REFERENCES all_products(`product_id`)   
) ENGINE=InnoDB DEFAULT CHARSET=UTF8;

CREATE TABLE IF NOT EXISTS `users` (   
  `user_id` INT(10) NOT NULL AUTO_INCREMENT,   
  `first_name` VARCHAR(100) NOT NULL,   
  `last_name` VARCHAR(100) NOT NULL,   
  `email` VARCHAR(100) NOT NULL,   
  `password` VARCHAR(200) NOT NULL,   
  `address` VARCHAR(200) NOT NULL,   
  `city` VARCHAR(100) NOT NULL,   
  `post_code` VARCHAR(8) NOT NULL,   
  PRIMARY KEY(`user_id`)   
) ENGINE=InnoDB DEFAULT CHARSET=UTF8;

CREATE TABLE IF NOT EXISTS `part_order` (   
  `order_number` INT(10) NOT NULL ,   
  `product_name` VARCHAR(100) NOT NULL,   
  `color_name` VARCHAR(100) NOT NULL,   
  `qty` INT(10) NOT NULL,   
  PRIMARY KEY(`order_number`),   
  INDEX(`order_number`),   
  CONSTRAINT FOREIGN KEY(`product_name`) REFERENCES
    all_products(`product_name`) ON DELETE CASCADE ON UPDATE CASCADE,   
  CONSTRAINT FOREIGN KEY(`color_name`) REFERENCES
    product_color(`color_name`) ON DELETE CASCADE ON UPDATE CASCADE   
) ENGINE=InnoDB DEFAULT CHARSET=UTF8;

CREATE TABLE IF NOT EXISTS `orders` (   
  `order_number` INT(10) NOT NULL,   
  `user_id` INT(10) NOT NULL,   
  Date DATETIME DEFAULT CURRENT_TIMESTAMP,   
  `total_cost` INT(10) NOT NULL,   
  `status` VARCHAR(50) NOT NULL,   
  PRIMARY KEY(`status`),   
  CONSTRAINT FOREIGN KEY(`order_number`) REFERENCES
    part_order(`order_number`) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT FOREIGN KEY(`user_id`) REFERENCES
    users(`user_id`) ON DELETE CASCADE ON UPDATE CASCADE
)

1 个答案:

答案 0 :(得分:0)

MySQL需要在引用的列(product_namecolor_name)上建立索引。将INDEX(product_name)添加到all_products表中,并将INDEX(color_name)添加到product_color表中

CREATE TABLE IF NOT EXISTS `all_products` (   
  `product_id` int(10) NOT NULL AUTO_INCREMENT,   
  `product_name` VARCHAR(100)  NOT NULL,  
  `product_price` decimal(10,2) NOT NULL,   
  `product_description` VARCHAR(1000) NOT NULL,  
  PRIMARY KEY(`product_id`, `product_name`),
  INDEX(`product_name`)   
) ENGINE=InnoDB DEFAULT CHARSET=UTF8;

来自docs

  

MySQL要求在外键和引用键上建立索引,以便   外键检查可以快速进行,不需要进行表扫描。在里面   引用表中,必须有一个索引,其中外键   列以相同顺序列为第一列。这样的   索引在引用表上自动创建(如果未创建)   存在。如果您创建此索引,则以后可能会默默删除该索引   另一个可用于强制执行外键约束的索引。   如果已指定,则使用index_name,如前所述。