MySql command error: Duplicate entry '10' for key 'PRIMARY' solution

Translation error

I often encounter this problem. Today I will record this problem. If other people encounter the same problem in the future, they will not be confused. I also use it as a record of mistakes in learning, a wrong question book, and remind myself.
When operating the database, errors such as Duplicate entry '10' for key 'PRIMARY' often appear. What does this mean?
I went to translate it, and the translation result is as follows:
insert image description here
So the appearance of this kind of problem means that the key name is repeated

Make the form normally

When defining the table header, the id is generally set as the primary key (because the id is unique and simple)
insert image description here
The following is a table made with Navicat Premium 16insert image description here

Make a mistake again (experience error reporting)

In this table, the ids already have 1, 2, 3, 4, 5, 6, 7, and 8. If the primary key of the data I insert is 8, the following result will appear: Yes,
insert image description herethis error is reported. Because the primary key of 8 exists, the primary key of the data inserted now is still 8, which causes a conflict of primary key duplication, so this error will be reported, and the data cannot be inserted at the same time.

Find the cause of the error and correct it

If we change the primary key of the data to be inserted to 9, then:
insert image description hereobviously the insertion was successful. Re-open the table to see:
insert image description here
ok, the data is inserted successfully.

Other situations that may cause errors

There is another situation that will trigger this error. Taking the above example as an example, when there is no piece of data with the id of 9, I insert this piece of data, there is no problem, no error is reported, and the insertion is successful. But if you run the sentence just now, it will not work. Because 9 has already been inserted, running it again is equivalent to inserting the data whose primary key is 9, which will cause a primary key conflict again.

There is another situation that will trigger this error, that is, the primary key increment is not set
or the visualization tool Navicat Premium 16 is used to check the automatic increment.
Either set the primary key increment on the command line.
insert image description here

insert image description here

Summarize the solution

To sum up, this error message is because an existing primary key value is inserted when inserting data, resulting in a conflict. The solution is as follows:

  1. To confirm whether duplicate data has been inserted, you can check whether there are duplicate primary key values ​​by querying the database.
  2. If the error is caused by inserting duplicate data, it can be solved by modifying the data or deleting the duplicate data.
  3. If the error is caused by other reasons, you can try to recreate the table or modify the table structure to solve it.

Guess you like

Origin blog.csdn.net/dyk11111/article/details/130544456