Home > Database > Mysql Tutorial > body text

Why am I Getting \'Can\'t write; duplicate key in table\' Error (Error 1022) When Creating a MySQL Table?

DDD
Release: 2024-10-28 04:07:20
Original
290 people have browsed it

Why am I Getting

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:


  • In the business world, each employee is associated with a single department.

  • Similarly, in a database, each row in a child table should reference a single row in a parent table.

  • This referencing mechanism is established through foreign keys, which act as bridges between tables.

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:


  • Table "Catalog" contains a list of products.

  • Table "Supplier" contains a list of suppliers.

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:

  1. Open the SQL statement and locate the foreign key definitions.
  2. Identify any duplicate foreign key names.
  3. Rename the duplicate foreign key to a unique name.
  4. Implement the updated SQL statement to create the table.

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!