Deep Analysis of MySQL Error 1022: Duplicate Key Constraints and Solutions

Nov 24, 2025 · Programming · 11 views · 7.8

Keywords: MySQL Error 1022 | Foreign Key Constraints | Duplicate Keys

Abstract: This article provides an in-depth analysis of MySQL Error 1022 'Can't write; duplicate key in table', exploring its causes and solutions. Through practical case studies, it demonstrates how to handle foreign key constraint naming conflicts in CREATE TABLE statements, offers information schema queries to locate duplicate constraints, and discusses special error scenarios in InnoDB full-text indexing contexts. Combining Q&A data with reference materials, the article systematically explains error mechanisms and best practices.

Core Mechanism of Error 1022

MySQL Error 1022 typically occurs during Data Definition Language (DDL) operations, particularly when creating or modifying table structures involving foreign key constraints. The fundamental cause lies in the database-level requirement for unique constraint names.

Practical Case Analysis

Consider the following CREATE TABLE statement:

CREATE TABLE IF NOT EXISTS `apptwo`.`usercircle` (
  `idUserCircle` MEDIUMINT NOT NULL,
  `userId` MEDIUMINT NULL,
  `circleId` MEDIUMINT NULL,
  `authUser` BINARY NULL,
  `authOwner` BINARY NULL,
  `startDate` DATETIME NULL,
  `endDate` DATETIME NULL,
  PRIMARY KEY (`idUserCircle`),
  INDEX `iduser_idx` (`userId` ASC),
  INDEX `idcategory_idx` (`circleId` ASC),
  CONSTRAINT `iduser` FOREIGN KEY (`userId`) REFERENCES `apptwo`.`user` (`idUser`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT `idcategory` FOREIGN KEY (`circleId`) REFERENCES `apptwo`.`circle` (`idCircle`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE = INNODB;

When this statement returns Error 1022, the most likely cause is that constraint names iduser or idcategory already exist in other tables within the current database. MySQL requires all constraint names to be unique across the entire database, not just within individual tables.

Constraint Conflict Detection Methods

To confirm whether constraint names are duplicated, use the information schema query:

SELECT `TABLE_SCHEMA`, `TABLE_NAME`
FROM `information_schema`.`KEY_COLUMN_USAGE`
WHERE `CONSTRAINT_NAME` IN ('iduser', 'idcategory');

This query returns all tables and databases using the specified constraint names, helping developers accurately identify conflict sources.

Solutions and Best Practices

The direct solution to constraint name conflicts is renaming the conflicting constraints. It's recommended to adopt more descriptive naming conventions, for example:

CONSTRAINT `fk_usercircle_user` FOREIGN KEY (`userId`) REFERENCES `apptwo`.`user` (`idUser`),
CONSTRAINT `fk_usercircle_circle` FOREIGN KEY (`circleId`) REFERENCES `apptwo`.`circle` (`idCircle`)

This naming approach not only avoids conflicts but also enhances code readability and maintainability.

Special Scenarios with InnoDB Full-Text Indexes

Reference articles reveal abnormal manifestations of Error 1022 in specific contexts. When executing Data Manipulation Language (DML) queries on InnoDB tables containing full-text indexes, this error may occur. This is typically related to synchronization issues with internal full-text search document IDs.

Error logs may display information such as:

2019-08-14 13:23:19 13329 [ERROR] InnoDB: Duplicate FTS_DOC_ID value on table <databasename>/<tablename>
2019-08-14 13:23:19 13329 [ERROR] Cannot find index FTS_DOC_ID_INDEX in InnoDB index translation table.

This phenomenon exhibits non-deterministic characteristics and may relate to hardware configuration and concurrent access patterns. In most cases, immediately retrying the failed operation succeeds, but this should not be considered a long-term solution.

Preventive Measures and Architectural Recommendations

To prevent Error 1022, it's recommended to:

Conclusion

Although MySQL Error 1022 appears straightforward, it involves core concepts of database constraint management. By understanding the global uniqueness requirement for constraint naming and mastering corresponding detection and resolution methods, developers can effectively avoid such errors. Additionally, awareness of special scenarios like InnoDB full-text indexing helps comprehensively address potential exceptional situations.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.