mysql ERROR 1022(23000):不能写;表中的重复键

时间:2017-03-22 12:13:35

标签: mysql

我生气了。

如果我用外键执行create语句:

mysql> CREATE TABLE `asset` (
    ->   `ID` bigint(20) NOT NULL,
    ->   `CODE` varchar(255) NOT NULL,
    ->   `DESCRIPTION` longtext,
    ->   `NAME` varchar(255) NOT NULL,
    ->   `SYSTEM_NODE_ID` bigint(20) DEFAULT NULL,
    ->   `FUNCTIONAL_NODE_ID` bigint(20) DEFAULT NULL,
    ->   `ZONAL_NODE_ID` bigint(20) DEFAULT NULL,
    ->   `RESPONSIBLE_ID` bigint(20) DEFAULT NULL,
    ->   `ORIGINATOR_ID` bigint(20) DEFAULT NULL,
    ->   `INTERVENTION_PROGRESSIVE` int(11) NOT NULL DEFAULT '0',
    ->   `TICKET_PROGRESSIVE` int(11) NOT NULL DEFAULT '0',
    ->   `WORK_ORDER_PROGRESSIVE` int(11) NOT NULL DEFAULT '0',
    ->   `DEPOT_PROGRESSIVE` int(11) NOT NULL DEFAULT '0',
    ->   PRIMARY KEY (`ID`),
    ->   UNIQUE KEY `UK_ASSET_CODE` (`CODE`),
    ->   FULLTEXT KEY `FT_ASSET` (`CODE`,`NAME`,`DESCRIPTION`),
    ->   CONSTRAINT `FK_ASSET_FUNCTIONAL_NODE_ID` FOREIGN KEY (`FUNCTIONAL_NODE_ID`) REFERENCES `node` (`ID`),
    ->   CONSTRAINT `FK_ASSET_OWNER_ID` FOREIGN KEY (`ORIGINATOR_ID`) REFERENCES `subject` (`ID`),
    ->   CONSTRAINT `FK_ASSET_RESPONSIBLE_ID` FOREIGN KEY (`RESPONSIBLE_ID`) REFERENCES `subject` (`ID`),
    ->   CONSTRAINT `FK_ASSET_SYSTEM_NODE_ID` FOREIGN KEY (`SYSTEM_NODE_ID`) REFERENCES `node` (`ID`),
    ->   CONSTRAINT `FK_ASSET_ZONAL_NODE_ID` FOREIGN KEY (`ZONAL_NODE_ID`) REFERENCES `node` (`ID`)
    -> ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

ERROR 1215 (HY000): Cannot add foreign key constraint

但如果我在创建表后移动外键,一切正常:

mysql> CREATE TABLE `asset` (
    ->   `ID` bigint(20) NOT NULL,
    ->   `CODE` varchar(255) NOT NULL,
    ->   `DESCRIPTION` longtext,
    ->   `NAME` varchar(255) NOT NULL,
    ->   `SYSTEM_NODE_ID` bigint(20) DEFAULT NULL,
    ->   `FUNCTIONAL_NODE_ID` bigint(20) DEFAULT NULL,
    ->   `ZONAL_NODE_ID` bigint(20) DEFAULT NULL,
    ->   `RESPONSIBLE_ID` bigint(20) DEFAULT NULL,
    ->   `ORIGINATOR_ID` bigint(20) DEFAULT NULL,
    ->   `INTERVENTION_PROGRESSIVE` int(11) NOT NULL DEFAULT '0',
    ->   `TICKET_PROGRESSIVE` int(11) NOT NULL DEFAULT '0',
    ->   `WORK_ORDER_PROGRESSIVE` int(11) NOT NULL DEFAULT '0',
    ->   `DEPOT_PROGRESSIVE` int(11) NOT NULL DEFAULT '0',
    ->   PRIMARY KEY (`ID`),
    ->   UNIQUE KEY `UK_ASSET_CODE` (`CODE`),
    ->   FULLTEXT KEY `FT_ASSET` (`CODE`,`NAME`,`DESCRIPTION`)
    -> ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Query OK, 0 rows affected (0.13 sec)

mysql> ALTER TABLE `asset` ADD CONSTRAINT `FK_ASSET_FUNCTIONAL_NODE_ID` FOREIGN KEY (`FUNCTIONAL_NODE_ID`) REFERENCES `node` (`ID`);
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> ALTER TABLE `asset` ADD CONSTRAINT `FK_ASSET_OWNER_ID` FOREIGN KEY (`ORIGINATOR_ID`) REFERENCES `subject` (`ID`);
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> ALTER TABLE `asset` ADD CONSTRAINT `FK_ASSET_RESPONSIBLE_ID` FOREIGN KEY (`RESPONSIBLE_ID`) REFERENCES `subject` (`ID`);
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> ALTER TABLE `asset` ADD CONSTRAINT `FK_ASSET_SYSTEM_NODE_ID` FOREIGN KEY (`SYSTEM_NODE_ID`) REFERENCES `node` (`ID`);
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> ALTER TABLE `asset` ADD CONSTRAINT `FK_ASSET_ZONAL_NODE_ID` FOREIGN KEY (`ZONAL_NODE_ID`) REFERENCES `node` (`ID`);
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

那么,有什么区别?

我在Windows 10下5.7.10-log MySQL Community Server (GPL)

由于

显然:

mysql> show variables like 'fo%';
+--------------------+-------+
| Variable_name      | Value |
+--------------------+-------+
| foreign_key_checks | OFF   |
+--------------------+-------+
1 row in set (0.00 sec)

这是show engine innodb status

mysql> show engine innodb status;

...

------------------------
LATEST FOREIGN KEY ERROR
------------------------
2017-03-22 13:14:17 0x26e4 Error in foreign key constraint of table edea2_atc/asset:
 FOREIGN KEY (`FUNCTIONAL_NODE_ID`) REFERENCES `node` (`ID`),
  CONSTRAINT `FK_ASSET_OWNER_ID` FOREIGN KEY (`ORIGINATOR_ID`) REFERENCES `subject` (`ID`),
  CONSTRAINT `FK_ASSET_RESPONSIBLE_ID` FOREIGN KEY (`RESPONSIBLE_ID`) REFERENCES `subject` (`ID`),
  CONSTRAINT `FK_ASSET_SYSTEM_NODE_ID` FOREIGN KEY (`SYSTEM_NODE_ID`) REFERENCES `node` (`ID`),
  CONSTRAINT `FK_ASSET_ZONAL_NODE_ID` FOREIGN KEY (`ZONAL_NODE_ID`) REFERENCES `node` (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8:
Cannot resolve table name close to:
 (`ID`),
  CONSTRAINT `FK_ASSET_OWNER_ID` FOREIGN KEY (`ORIGINATOR_ID`) REFERENCES `subject` (`ID`),
  CONSTRAINT `FK_ASSET_RESPONSIBLE_ID` FOREIGN KEY (`RESPONSIBLE_ID`) REFERENCES `subject` (`ID`),
  CONSTRAINT `FK_ASSET_SYSTEM_NODE_ID` FOREIGN KEY (`SYSTEM_NODE_ID`) REFERENCES `node` (`ID`),
  CONSTRAINT `FK_ASSET_ZONAL_NODE_ID` FOREIGN KEY (`ZONAL_NODE_ID`) REFERENCES `node` (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

1 个答案:

答案 0 :(得分:1)

我无法重现错误。

测试:

mysql> SELECT VERSION();
+-----------+
| VERSION() |
+-----------+
| 5.7.17    |
+-----------+
1 row in set (0.00 sec)

mysql> SHOW VARIABLES LIKE 'foreign_key_checks';
+--------------------+-------+
| Variable_name      | Value |
+--------------------+-------+
| foreign_key_checks | ON    |
+--------------------+-------+
1 row in set (0.01 sec)

mysql> SET @@SESSION.foreign_key_checks := 0;
Query OK, 0 rows affected (0.00 sec)

mysql> SHOW VARIABLES LIKE 'foreign_key_checks';
+--------------------+-------+
| Variable_name      | Value |
+--------------------+-------+
| foreign_key_checks | OFF   |
+--------------------+-------+
1 row in set (0.00 sec)

mysql> DROP TABLE IF EXISTS `asset`;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> CREATE TABLE `asset` (
    -> `ID` bigint(20) NOT NULL,
    -> `CODE` varchar(255) NOT NULL,
    -> `DESCRIPTION` longtext,
    -> `NAME` varchar(255) NOT NULL,
    -> `SYSTEM_NODE_ID` bigint(20) DEFAULT NULL,
    -> `FUNCTIONAL_NODE_ID` bigint(20) DEFAULT NULL,
    -> `ZONAL_NODE_ID` bigint(20) DEFAULT NULL,
    -> `RESPONSIBLE_ID` bigint(20) DEFAULT NULL,
    -> `ORIGINATOR_ID` bigint(20) DEFAULT NULL,
    -> `INTERVENTION_PROGRESSIVE` int(11) NOT NULL DEFAULT '0',
    -> `TICKET_PROGRESSIVE` int(11) NOT NULL DEFAULT '0',
    -> `WORK_ORDER_PROGRESSIVE` int(11) NOT NULL DEFAULT '0',
    -> `DEPOT_PROGRESSIVE` int(11) NOT NULL DEFAULT '0',
    -> PRIMARY KEY (`ID`),
    -> UNIQUE KEY `UK_ASSET_CODE` (`CODE`),
    -> FULLTEXT KEY `FT_ASSET` (`CODE`,`NAME`,`DESCRIPTION`),
    -> CONSTRAINT `FK_ASSET_FUNCTIONAL_NODE_ID`
    ->     FOREIGN KEY (`FUNCTIONAL_NODE_ID`) REFERENCES `node` (`ID`),
    -> CONSTRAINT `FK_ASSET_OWNER_ID`
    ->     FOREIGN KEY (`ORIGINATOR_ID`) REFERENCES `subject` (`ID`),
    -> CONSTRAINT `FK_ASSET_RESPONSIBLE_ID`
    ->     FOREIGN KEY (`RESPONSIBLE_ID`) REFERENCES `subject` (`ID`),
    -> CONSTRAINT `FK_ASSET_SYSTEM_NODE_ID`
    ->     FOREIGN KEY (`SYSTEM_NODE_ID`) REFERENCES `node` (`ID`),
    -> CONSTRAINT `FK_ASSET_ZONAL_NODE_ID`
    ->     FOREIGN KEY (`ZONAL_NODE_ID`) REFERENCES `node` (`ID`)
    -> ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Query OK, 0 rows affected (0.00 sec)

mysql> SET @@SESSION.foreign_key_checks := 1;
Query OK, 0 rows affected (0.00 sec)

<强>更新

在版本5.7.10中生成错误。

更新2

  

Changes in MySQL 5.7.11 (2016-02-05)

     

InnoDB:禁用foreign_key_checks时,创建具有全文索引和外键约束的表失败。 (Bug #22094601Bug #78955)。

     

参考文献:此错误是Bug #16845421的回归。