0

有人知道,为什么这个 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 ;
4

1 回答 1

0

table_idisvarchar(10)但您希望它参考tables._idwhich is int。为了使用外键,列的数据类型应该相同

于 2012-04-21T18:11:18.137 回答