#1062 - Duplicate entry '1' for key 'PRIMARY'

妖精的绣舞 提交于 2021-02-04 10:54:15

问题


I am at a complete loss here. I have two databases. One on my localhost site that I use for development and one on my remote site that I use for my live (production) site. I manage both of them through phpMyadmin. As I have been doing for months now, when I need to update the live site, I dump the related database and import the database from my localhost site.

Now, no matter what I try, I keep getting this error:

Error SQL query:

--
-- Dumping data for table `oc_address_type`
--
INSERT INTO  `oc_address_type` (  `address_type_id` ,  `address_type_name` ) 
VALUES ( 1,  'Billing' ) , ( 2,  'Shipping' ) ;

MySQL said: Documentation

#1062 - Duplicate entry '1' for key 'PRIMARY'

I tried creating a new blank database on my localhost and importing into that but same results. I have validated all of the tables and indexes and cannot find anything wrong there.

Any suggestions please as I am completely down until this gets resolved.

By the way, I am completely dropping all tables and importing structure and data. This has always worked until today.


回答1:


you need to dump with the drop statements. The table exists and has data already and your trying to insert more which is identical. Im not 100% sure on phpmyadmin but the dumps will have an option for "add drop table" statements




回答2:


Dump your database on localhost with "mysqldump --insert-ignore ..." then try to import with phpmyadmin on your live machine.

Or try to connect to your live database with command line tools (configure your database to be able to connect from other hosts than "localhost" first!)

Then you can try following:

$ mysql -f -p < yourdump.sql

with -f "force" you can ignore errors during importing. It's the same as adding "--force" parameter to "mysqlimport".




回答3:


The problem is related with your file - you are trying to create a DB using a copy - at the top of your file you will find something like this:

CREATE DATABASE IF NOT EXISTS *THE_NAME_OF_YOUR_DB* DEFAULT CHARACTER SET latin1 COLLATE latin1_general_ci; USE *THE_NAME_OF_YOUR_DB*;

and I'm sure that you already have a DB with this name - IN THE SAME SERVER - please check, because you are trying to overwrite!! Just change the name OR (better) ERASE THIS LINE!




回答4:


For me the foreign_key_checks and truncate table options was useful.

SET foreign_key_checks = 0;
TRUNCATE `oc_address_type`;
SET foreign_key_checks = 1;

Run the above sql script, and after the import.




回答5:


I had this same issue, my problem was I had a primary key column called unique_id and when you try to add two of the same value in that primary keyed column, it comes back with the error below.

enter image description hereenter image description here

A primary key column's data is all suppose to be different, so that bottom 1 I changed to 3 and the error went away.

Your MySql is not corrupt, like previous answers and comments.




回答6:


you need to delete any previous tables that you are over-writing. if you are doing a complete restore of all tables, delete all existing tables.




回答7:


I have met the same problem, I drop the table and rebuilt the database, then the problem solved.



来源:https://stackoverflow.com/questions/18021080/1062-duplicate-entry-1-for-key-primary

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!