创建表的InnoDB错误

时间:2012-04-19 22:54:41

标签: foreign-keys innodb create-table

有人知道,为什么这个SQL在创建表帐户上返回错误?

1005 - 无法创建表'mobilewaiter.accounts'(错误号:150)

我看到,问题可以在键上使用索引,但所有内容都在主键上引用。 帮助我,谢谢。

CREATE TABLE IF NOT EXISTS `tables` (
  `_id` int(10) NOT NULL AUTO_INCREMENT,
  `tag` varchar(40) COLLATE utf8_czech_ci NOT NULL,
  `name` varchar(40) COLLATE utf8_czech_ci NOT NULL,
  `occupied` tinyint(1) NOT NULL DEFAULT '0',
  PRIMARY KEY (`_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci AUTO_INCREMENT=1 ;

CREATE TABLE IF NOT EXISTS `users` (
  `_id` int(10) NOT NULL AUTO_INCREMENT,
  `nick` varchar(20) COLLATE utf8_czech_ci NOT NULL,
  `password` varchar(40) COLLATE utf8_czech_ci NOT NULL,
  `salt` varchar(20) COLLATE utf8_czech_ci NOT NULL,
  `email` varchar(50) COLLATE utf8_czech_ci NOT NULL,
  `firstname` varchar(20) COLLATE utf8_czech_ci NOT NULL,
  `lastname` varchar(20) COLLATE utf8_czech_ci NOT NULL,
  `role` varchar(20) COLLATE utf8_czech_ci NOT NULL,
  PRIMARY KEY (`_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci AUTO_INCREMENT=1 ;

CREATE TABLE IF NOT EXISTS `accounts` (
  `_id` int(10) NOT NULL AUTO_INCREMENT,
  `table_id` varchar(10) COLLATE utf8_czech_ci NOT NULL,
  `state` varchar(20) COLLATE utf8_czech_ci NOT NULL DEFAULT 'created',
  `user_id` int(10) NOT NULL,
  `start_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `end_time` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`_id`),
  FOREIGN KEY (`table_id`) REFERENCES `tables`(`_id`) ON DELETE CASCADE ON UPDATE CASCADE,
  FOREIGN KEY (`user_id`) REFERENCES `users`(`_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci AUTO_INCREMENT=1 ;

1 个答案:

答案 0 :(得分:0)

table_idvarchar(10),但您希望它引用tables._id int。为了使用外键,列的数据类型应该相同