Duplication Detected: MySQL Table Creation Failure (Error 1022)
While attempting to create a table using MySQL Workbench, users may encounter the dreaded error 1022: "Can't write; duplicate key in table." This issue arises despite the seemingly straightforward SQL statement used for table creation. Upon closer examination, the problem becomes apparent:
The Culprit: Identical Foreign Key Names
The error message stems from the presence of multiple foreign keys with identical names within the same database model. To understand this concept, consider the following analogy:
The issue arises when two different foreign keys in different tables reference the same column in the parent table. Just as an employee cannot work in two departments at once, a child table row cannot have multiple foreign keys pointing to different parent table rows.
A Physical Example
Consider the following database model:
If both tables contain foreign keys named "supplier_id" that reference the "Supplier" table, MySQL will return the error 1022. To resolve this issue, it is essential to assign unique names to foreign keys.
Resolving the Naming Conflict
To rectify the error, follow these steps:
By addressing the naming conflict within foreign keys, you can successfully create the desired table and avoid the "Can't write; duplicate key in table" error.
The above is the detailed content of Why am I Getting \'Can\'t write; duplicate key in table\' Error (Error 1022) When Creating a MySQL Table?. For more information, please follow other related articles on the PHP Chinese website!