创建表时SQLSTATE [HY000]错误?

时间:2014-07-08 14:10:38

标签: mysql

这是我的SQL:

CREATE TABLE mytable (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `customer_id` int(10) NOT NULL,
  `token` varchar(100),
  `created_at` datetime,
  PRIMARY KEY (`id`),
  CONSTRAINT `FK_PASSWORD_RECOVERY_CUSTOMER` FOREIGN KEY (`customer_id`) REFERENCES `customer_entity` (`entity_id`) ON UPDATE CASCADE ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1

它总是给我:无法创建表'mytable'(错误号:150)SQL.sql

1 个答案:

答案 0 :(得分:0)

正如我对您的问题的评论中所述,您获得的错误与约束定义有关。然后,您应该检查proper syntax for the constraint

我认为问题在于您没有为customer_id列编制索引。尝试索引它:

CREATE TABLE mytable (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `customer_id` int(10) NOT NULL,
  `token` varchar(100),
  `created_at` datetime,
  PRIMARY KEY (`id`),
  INDEX cust_id(customer_id), -- ADD THIS
  FOREIGN KEY (`customer_id`) 
      REFERENCES `customer_entity` (`entity_id`) 
      ON UPDATE CASCADE 
      ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;

(作为预防措施和哑巴检查,确认customer_entity也是InnoDB表)