MYSQL ERROR 1823当SET FOREIGN_KEY_CHECKS = 1时

时间:2017-02-15 18:11:28

标签: mysql

当FOREIGN_KEY_CHECKS = 0且执行了2个查询时,我发生了一个奇怪的错误(除了FOREIGN_CHECK_CHECKS之外,两种情况都相同。

错误导致错误ERROR 1823(HY000):无法添加外键约束...(下面的完整错误)

如果我使用SET FOREIGN_KEY_CHECKS = 1运行查询,它将按预期工作。 (这几乎似乎是关键词,因为关闭外键检查应该让事情发生,但不应该这样做)

这是mysql中的错误还是我不理解?我找不到关于这个mysql错误代码的很多信息。

MYSQL VERISON: 33年6月5日

错误(最后查询):

mysql> SET FOREIGN_KEY_CHECKS=0;
Query OK, 0 rows affected (0.00 sec)

mysql> ALTER TABLE phppos_people ADD INDEX phppos_people_ibfk_1 (image_id);
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> ALTER TABLE phppos_customers ADD CONSTRAINT phppos_customers_ibfk_1 FOREIGN KEY person_id (person_id) REFERENCES phppos_people (person_id) ON UPDATE NO ACTION ON DELETE NO ACTION;
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> ALTER TABLE phppos_employees ADD CONSTRAINT phppos_employees_ibfk_1 FOREIGN KEY person_id (person_id) REFERENCES phppos_people (person_id) ON UPDATE NO ACTION ON DELETE NO ACTION;
ERROR 1823 (HY000): Failed to add the foreign key constraint 'migrate/person_id' to system tables

SUCCESS:

mysql> SET FOREIGN_KEY_CHECKS=1;
Query OK, 0 rows affected (0.00 sec)

mysql> ALTER TABLE phppos_people ADD INDEX phppos_people_ibfk_1 (image_id);
Query OK, 0 rows affected (0.04 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> ALTER TABLE phppos_customers ADD CONSTRAINT phppos_customers_ibfk_1 FOREIGN KEY person_id (person_id) REFERENCES phppos_people (person_id) ON UPDATE NO ACTION ON DELETE NO ACTION;
Query OK, 1 row affected (0.03 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> ALTER TABLE phppos_employees ADD CONSTRAINT phppos_employees_ibfk_1 FOREIGN KEY person_id (person_id) REFERENCES phppos_people (person_id) ON UPDATE NO ACTION ON DELETE NO ACTION;
Query OK, 1 row affected (0.04 sec)
Records: 1  Duplicates: 0  Warnings: 0

SCHEMA:

mysql> show create table phppos_people;
+---------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table         | Create Table                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       |
+---------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| phppos_people | CREATE TABLE `phppos_people` (
  `first_name` varchar(255) CHARACTER SET ucs2 NOT NULL,
  `last_name` varchar(255) CHARACTER SET ucs2 NOT NULL,
  `phone_number` varchar(255) CHARACTER SET ucs2 NOT NULL,
  `email` varchar(255) CHARACTER SET ucs2 NOT NULL,
  `address_1` varchar(255) CHARACTER SET ucs2 NOT NULL,
  `address_2` varchar(255) CHARACTER SET ucs2 NOT NULL,
  `city` varchar(255) CHARACTER SET ucs2 NOT NULL,
  `state` varchar(255) CHARACTER SET ucs2 NOT NULL,
  `zip` varchar(255) CHARACTER SET ucs2 NOT NULL,
  `country` varchar(255) CHARACTER SET ucs2 NOT NULL,
  `comments` text CHARACTER SET ucs2 NOT NULL,
  `image_id` int(11) DEFAULT NULL,
  `person_id` int(11) NOT NULL,
  PRIMARY KEY (`person_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+---------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> show create table phppos_customers;
+------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table            | Create Table                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
+------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| phppos_customers | CREATE TABLE `phppos_customers` (
  `id` int(11) NOT NULL,
  `person_id` int(11) NOT NULL,
  `account_number` varchar(255) CHARACTER SET ucs2 DEFAULT NULL,
  `override_default_tax` int(11) NOT NULL,
  `company_name` varchar(255) CHARACTER SET ucs2 NOT NULL,
  `balance` decimal(23,10) NOT NULL,
  `credit_limit` decimal(23,10) DEFAULT NULL,
  `points` decimal(23,10) NOT NULL,
  `current_spend_for_points` decimal(23,10) NOT NULL,
  `current_sales_for_discount` int(11) NOT NULL,
  `taxable` int(11) NOT NULL,
  `tax_certificate` varchar(255) CHARACTER SET ucs2 NOT NULL,
  `cc_token` varchar(255) CHARACTER SET ucs2 DEFAULT NULL,
  `cc_preview` varchar(255) CHARACTER SET ucs2 DEFAULT NULL,
  `card_issuer` varchar(255) CHARACTER SET ucs2 DEFAULT NULL,
  `tier_id` int(11) DEFAULT NULL,
  `deleted` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

2 个答案:

答案 0 :(得分:1)

是的,这似乎是一个错误。我可以在MySQL 5.6+中验证它(在MySQL 8中类似),而5.5不受影响。这里的问题是MySQL使用的约束名称。

syntax

[CONSTRAINT [symbol]] FOREIGN KEY
[index_name] (index_col_name, ...)
REFERENCES tbl_name (index_col_name,...)

约束名称应为 symbol ,如果没有给出,则自动创建:

  

如果给出了CONSTRAINT符号子句,则符号值(如果使用)在数据库中必须是唯一的。重复的符号将导致类似于以下的错误:ERROR 1022(2300):无法写入;表格中的重复键#sql- 464_1'。如果未给出该子句,或者CONSTRAINT关键字后面不包含符号,则会自动创建约束的名称。

这通常会这样:

  • 使用符号
  • 否则自动创建名称(格式为 tablename_ibfk_id

如果您为FOREIGN_KEY_CHECKS禁用ALTER TABLE,但CREATE TABLE禁用person_id,则会另外使用 index_name

  • 使用 index_name
  • 如果未指定 index_name ,请使用符号
  • 否则自动创建名称(格式为 tablename_ibfk_id

因此,虽然 index_name 通常只需要对每个表都是唯一的,但是这里,两个ALTER语句中的公共 index_name person_id将是导致麻烦,因为MySQL试图用相同(错误)的名称创建两个约束。

作为一种解决方法,在您的情况下,您可以从alter语句中删除 index_name ALTER TABLE phppos_customers ADD INDEX person_id (person_id); ALTER TABLE phppos_customers ADD CONSTRAINT FOREIGN KEY (person_id) REFERENCES ... ,因为如果一切正常,索引应该得到< em> symbol 无论如何:

  

如果子表上已经显式定义了可以支持外键的索引,则忽略index_name值。否则,MySQL会隐式创建一个根据以下规则命名的外键索引:

     
      
  • 如果已定义,则使用CONSTRAINT符号值。否则,使用FOREIGN KEY index_name值。

  •   
  • 如果既未定义CONSTRAINT符号也未定义FOREIGN KEY index_name,则使用引用外键列的名称生成外键索引名称。

  •   

如果要显式命名索引,但使用默认约束名称,则可以在添加外键之前添加索引,例如:使用

FOREIGN_KEY_CHECKS

MySQL 8显示略有不同的行为(当ALTER TABLE被禁用且仅 PolylineOptions lineOptions = null; lineOptions = new PolylineOptions(); lineOptions.addAll(points); lineOptions.width(5); lineOptions.color(Color.RED); Polyline polyline = mGoogleMap.addPolyline(lineOptions); polyline.remove(); 时):它也错误地使用 index_name ,但仅当没有时给出符号 - 在您的情况下可以使用,因为您设置了符号

答案 1 :(得分:0)

得到相同的错误编号:1823;符号:ER_FK_FAIL_ADD_SYSTEM; SQLSTATE: HY00063 消息:无法将外键约束“%s”添加到系统表

在我的情况下,当我减少查询工作的约束名称的长度时,FK 名称太长。