MySQL #1062 - 键 'PRIMARY' 的重复条目 '1'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18021080/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
#1062 - Duplicate entry '1' for key 'PRIMARY'
提问by user2646528
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.
我在这里完全不知所措。我有两个数据库。一个在我用于开发的 localhost 站点上,另一个在我的远程站点上用于我的实时(生产)站点。我通过 phpMyadmin 管理它们。正如我几个月来一直在做的那样,当我需要更新实时站点时,我会转储相关数据库并从我的本地主机站点导入数据库。
Now, no matter what I try, I keep getting this error:
现在,无论我尝试什么,我都会收到此错误:
Error SQL query:
错误 SQL 查询:
--
-- 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
MySQL 说: 文档
#1062 - Duplicate entry '1' for key 'PRIMARY'
#1062 - 键 'PRIMARY' 的重复条目 '1'
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.
顺便说一句,我完全删除所有表并导入结构和数据。这一直有效,直到今天。
采纳答案by exussum
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
您需要使用 drop 语句进行转储。该表存在并且已经有数据并且您尝试插入更多相同的数据。我对 phpmyadmin 不是 100% 确定,但转储将有一个“添加删除表”语句的选项
回答by Dado
Dump your database on localhost with "mysqldump --insert-ignore ..." then try to import with phpmyadmin on your live machine.
使用“mysqldump --insert-ignore ...”将您的数据库转储到本地主机上,然后尝试在您的实时机器上使用 phpmyadmin 导入。
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!)
或者尝试使用命令行工具连接到您的实时数据库(首先将您的数据库配置为能够从“localhost”以外的其他主机连接!)
Then you can try following:
然后您可以尝试以下操作:
$ mysql -f -p < yourdump.sql
$ mysql -f -p < yourdump.sql
with -f "force" you can ignore errors during importing. It's the same as adding "--force" parameter to "mysqlimport".
使用 -f "force" 您可以忽略导入过程中的错误。这与向“mysqlimport”添加“--force”参数相同。
回答by bito_
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*;
CREATE DATABASE 如果不存在 *THE_NAME_OF_YOUR_DB* DEFAULT CHARACTER SET latin1 COLLATE latin1_general_ci; 使用 *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!
并且我确定您已经有一个具有此名称的数据库 - 在同一服务器中 - 请检查,因为您正在尝试覆盖!!只需更改名称或(更好)删除此行!
回答by Gaalvarez
For me the foreign_key_checks and truncate table options was useful.
对我来说,foreign_key_checks 和 truncate 表选项很有用。
SET foreign_key_checks = 0;
TRUNCATE `oc_address_type`;
SET foreign_key_checks = 1;
Run the above sql script, and after the import.
运行上面的sql脚本,然后导入。
回答by mattblessed
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.
我遇到了同样的问题,我的问题是我调用了一个主键列unique_id
,当您尝试在该主键列中添加两个相同的值时,它会返回以下错误。
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.
假设主键列的数据都不同,因此1
我更改为底部3
并且错误消失了。
Your MySql is not corrupt, like previous answers and comments.
您的 MySql 没有损坏,就像以前的答案和评论一样。
回答by bollos
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.
您需要删除任何以前覆盖的表。如果您要对所有表进行完全恢复,请删除所有现有表。
回答by li haowei
I have met the same problem, I drop the table and rebuilt the database, then the problem solved.
我也遇到了同样的问题,我删除了表并重建了数据库,然后问题解决了。