使用 InnoDB 引擎创建表时出现 MySQL 1005 错误

标签 mysql innodb mysql-error-1005

当我尝试创建具有以下定义的表时,

CREATE TABLE `demo` (
    `id` INT(11) NOT NULL auto_increment,
    `x_id` INT(11) NOT NULL,
    `y_id` INT(11) NOT NULL,
    `z_id` INT(11) NOT NULL,
    `status` TINYINT unsigned NOT NULL,
    `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY  (`id`),
    CONSTRAINT UNIQUE INDEX(x_id, y_id)
) ENGINE=InnoDB;

OperationalError 发生:

_mysql_exceptions.OperationalError:
(1005, "Can't create table 'xxx.frm' (errno: -1)")

如果我删除尾随的 ENGINE=InnoDB,它会起作用。

这背后的原因是什么?

MySQL版本是 mysql Ver 14.12 Distrib 5.0.84,适用于使用 readline 5.2 的 pc-linux-gnu (i686)

最佳答案

If you re-create a table that was dropped, it must have a definition that conforms to the foreign key constraints referencing it. It must have the right column names and types, and it must have indexes on the referenced keys, as stated earlier. If these are not satisfied, MySQL returns error number 1005 and refers to error 150 in the error message.

If MySQL reports an error number 1005 from a CREATE TABLE statement, and the error message refers to error 150, table creation failed because a foreign key constraint was not correctly formed. Similarly, if an ALTER TABLE fails and it refers to error 150, that means a foreign key definition would be incorrectly formed for the altered table. You can use SHOW ENGINE INNODB STATUS to display a detailed explanation of the most recent InnoDB foreign key error in the server.

Foreign Key Constraints - Error 1005

关于使用 InnoDB 引擎创建表时出现 MySQL 1005 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4213013/

相关文章:

mysql - 每 1 个月将 mysql 列重置为基值

mysql - 查询在生产中很快,在过去一周的开发中变得很慢

java - 当服务器时区不是 UTC 时,从 Java 中的 MySQL 检索 UTC DATETIME 字段

php - MySQL 创建过多的重复行

mysql - MVCC 行锁定与教科书事务行为

mysql - 无法在 MySQL 错误 : 150 中创建外键

mysql - 复合主键和另一个属性的外键

java - 如何使用mybatis在java中映射mysql点类型

mysql - 创建数据库时出错 - Errno 150

php - 无法让 UPSERT 在具有自动增量列和 FK 的 MYSQL 上工作