MySQL 1005 error when creating table using InnoDB

2019-07-19 10:48发布

When I try to create a table with the following definition,

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;

an OperationalError occurs:

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

It works if I remove the trailing ENGINE=InnoDB.

What is the reason behind this?

The MySQL version is mysql Ver 14.12 Distrib 5.0.84, for pc-linux-gnu (i686) using readline 5.2

2条回答
唯我独甜
2楼-- · 2019-07-19 10:54

Please try this:

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(3) unsigned NOT NULL,
  `created_at` timestamp NOT NULL default CURRENT_TIMESTAMP,
  PRIMARY KEY  (`id`),
  UNIQUE KEY `x_id` (`x_id`,`y_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
查看更多
兄弟一词,经得起流年.
3楼-- · 2019-07-19 11:17

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

查看更多
登录 后发表回答