MySQL外键 - 创建表时出错150

时间:2012-03-10 14:04:49

标签: mysql sql foreign-keys foreign-key-relationship

我编写了一些SQL代码来创建具有多个关系(外键)的五个表。

第一个外键关系工作正常,创建的表没有任何错误,但是当我尝试创建gasten_url_toegangscodes时,我收到以下错误:

  

#1005 - Can't create table 'dbname.gasten_url_toegangscodes' (errno: 150).

我已经阅读了有关外键的文档,我仍然无法找到问题。最奇怪的是,它在前三个表中有效..

有人可以帮我吗?

我的完整SQL代码是:

CREATE TABLE IF NOT EXISTS `groepen` (
  `id` tinyint(3) NOT NULL AUTO_INCREMENT,
  `naam` varchar(50) NOT NULL DEFAULT '',
  `status` tinyint(1) NOT NULL DEFAULT '1',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB;

CREATE TABLE IF NOT EXISTS `gasten` (
  `id` int(5) NOT NULL AUTO_INCREMENT,
  `bedrijf` varchar(100) NOT NULL DEFAULT '',
  `uniek` varchar(64) NOT NULL,
  `groepid` tinyint(3) NOT NULL,
  PRIMARY KEY (`id`),
  FOREIGN KEY (groepid) REFERENCES `groepen` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION
) ENGINE=InnoDB;

CREATE TABLE IF NOT EXISTS `passen` (
  `id` tinyint(3) NOT NULL AUTO_INCREMENT,
  `naam` varchar(30) NOT NULL DEFAULT '',
  `color` varchar(7) NOT NULL DEFAULT '#FFFFFF',
  `bekend` tinyint(1) NOT NULL DEFAULT '0',
  `welkom` tinyint(1) NOT NULL DEFAULT '0',
  `aantal` tinyint(1) NOT NULL DEFAULT '0',
  `nummer` int(11) NOT NULL DEFAULT '0',
  `begrens` tinyint(1) NOT NULL DEFAULT '0',
  `status` tinyint(1) NOT NULL DEFAULT '1',
  `priority` int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`),
  KEY `naam` (`naam`)
) ENGINE=InnoDB;

CREATE TABLE IF NOT EXISTS `gasten_url_toegangscodes` (
  `uniek` varchar(64) NOT NULL,
  `unieke_url_code` char(64) NOT NULL,
  `salt` char(16) NOT NULL,
  FOREIGN KEY (uniek) REFERENCES `gasten` (`uniek`) ON DELETE CASCADE ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE IF NOT EXISTS `gasten_tickets` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `pas_id` tinyint(3) NOT NULL,
  `uniek` varchar(64) NOT NULL,
  `barcode` char(16) NOT NULL,
  `secret_key` char(32) NOT NULL,
  `download_count` int(11) NOT NULL DEFAULT '0',
  `last_download_time` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `scanned` enum('N','Y') NOT NULL,
  `scanned_datetime` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  PRIMARY KEY (`id`),
  FOREIGN KEY (uniek) REFERENCES `gasten` (`uniek`) ON DELETE CASCADE ON UPDATE NO ACTION,
  FOREIGN KEY (pas_id) REFERENCES `passen` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION
) ENGINE=InnoDB;

提前致谢!

1 个答案:

答案 0 :(得分:4)

gasten.uniek

上创建索引
CREATE TABLE IF NOT EXISTS `gasten` (
  `id` int(5) NOT NULL AUTO_INCREMENT,
  `bedrijf` varchar(100) NOT NULL DEFAULT '',
  `uniek` varchar(64) NOT NULL,
  `groepid` tinyint(3) NOT NULL,
  PRIMARY KEY (`id`),
  INDEX `idx_uniek` (`uniek`),
  FOREIGN KEY (groepid) REFERENCES `groepen` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION
) ENGINE=InnoDB;

并将gasten_url_toegangscodes中FK的字符集设置为与gasten中相关列的字符集相同。

假设gasten是UTF-8:

CREATE TABLE IF NOT EXISTS `gasten_url_toegangscodes` (
  /* use same charset for this column */
  `uniek` varchar(64) NOT NULL CHARSET UTF-8,
  `unieke_url_code` char(64) NOT NULL,
  `salt` char(16) NOT NULL,
  FOREIGN KEY (uniek) REFERENCES `gasten` (`uniek`) ON DELETE CASCADE ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=latin1;