简体   繁体   English

MySQL外键错误:无法创建表

[英]MySQL Foreign Key Error: Can't create table

I am trying to create two tables in MySQL database called class_members and history . 我试图在MySQL数据库中创建两个名为class_membershistory The class_members table contains information about the members and is created as: class_members表包含有关成员的信息,并创建为:

CREATE TABLE `class_members` (
  `id` int(11) NOT NULL,
  `username` varchar(65) NOT NULL DEFAULT '',
  `password` varchar(65) NOT NULL DEFAULT '',
  `email` varchar(65) NOT NULL,
  `mod_timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `username_UNIQUE` (`username`),
  UNIQUE KEY `id_UNIQUE` (`id`),
  UNIQUE KEY `email_UNIQUE` (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 

The class_members tables is successfully created. class_members表已成功创建。

I am facing the problem when creating history table which contains foreign key reference to class_members . 创建包含对class_members foreign key引用的history表时,我遇到了问题。

The command which I used is as follows: 我使用的命令如下:

CREATE TABLE `History` ( 
   `history_id` INT(11) NOT NULL AUTO_INCREMENT, 
   `username` VARCHAR(65) NOT NULL DEFAULT '', 
   `timestamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, 
   `ip` VARCHAR(20) NOT NULL,  
   PRIMARY KEY (`history_id`), 
   CONSTRAINT `fk_history_member` FOREIGN KEY (`username`) 
   REFERENCES `class_members` (`username`) 
   ON UPDATE CASCADE);

When I execute the above command I get this error: 当我执行以上命令时,出现此错误:

ERROR 1005 (HY000): Can't create table 'testDB.History' (errno: 150)

I tried to find why I cannot read the History table ( about ERROR 1005 ) but I cannot understand why 我试图找到为什么我无法读取History表( 关于错误1005 ),但我不明白为什么


Following Up on Comments, I created a new database and tried creating using the above command and I still get the error. 在评论之后,我创建了一个新数据库,并尝试使用上述命令进行创建,但仍然出现错误。 See Attached Pic 见附件图片 在此处输入图片说明


Updated (Solved) 更新(已解决)

I found why the error in the above screenshot occurs. 我发现了为什么上面的屏幕截图中出现错误。 To solve that we have to add ENGINE=InnoDB DEFAULT CHARSET=utf8 to the end of the History table creation command. 为了解决这个问题,我们必须在历史记录表创建命令的末尾添加ENGINE=InnoDB DEFAULT CHARSET=utf8

 CREATE TABLE `History` ( 
   `history_id` INT(11) NOT NULL AUTO_INCREMENT, 
   `username` VARCHAR(65) NOT NULL DEFAULT '', 
   `timestamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, 
   `ip` VARCHAR(20) NOT NULL,  
   PRIMARY KEY (`history_id`), 
   CONSTRAINT `fk_history_member` FOREIGN KEY (`username`) 
   REFERENCES `class_members` (`username`) 
   ON UPDATE CASCADE)ENGINE=InnoDB DEFAULT CHARSET=utf8;

This works perfectly. 这很完美。

Your query works on rextester . 您的查询适用于rextester

Make sure you don't already have an existing table called history . 确保您还没有名为history的现有表。 If I re-create the history table, I get: 如果我重新创建history表,则会得到:

Cannot delete or update a parent row: a foreign key constraint fails 无法删除或更新父行:外键约束失败

You can reproduce this by running the rextester query once, then remove the drop table if exists history; 您可以通过运行一次rextester查询来重现此rextester ,然后drop table if exists history; ,则删除drop table if exists history; line, and run it again. 行,然后再次运行。

If you try to create the table twice in the same batch, you get this instead: 如果您尝试在同一批次中两次创建表,则会得到以下信息:

Table 'history' already exists 表“历史”已存在

I think your problem is from the origin of the index. 我认为您的问题出在索引的起源。 Try the code this way; 以这种方式尝试代码;

CREATE TABLE `class_members` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(65) NOT NULL DEFAULT '',
  `password` varchar(65) NOT NULL DEFAULT '',
  `email` varchar(65) NOT NULL,
  `mod_timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `username_UNIQUE` (`username`),
  UNIQUE KEY `id_UNIQUE` (`id`),
  UNIQUE KEY `email_UNIQUE` (`email`),
    INDEX `index_username` (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `History` ( 
  `history_id` INT(11) NOT NULL AUTO_INCREMENT, 
  `username` VARCHAR(65) NOT NULL DEFAULT '', 
  `timestamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, 
  `ip` VARCHAR(20) NOT NULL,  
  PRIMARY KEY (`history_id`), 
  CONSTRAINT `fk_history_member` FOREIGN KEY (`username`) 
  REFERENCES `class_members` (`username`) 
  ON UPDATE CASCADE);

For SQLFiddle Sample; 对于SQLFiddle示例; Click Here 点击这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM