Duplicate entry '1' for key 'PRIMARY' - mysql

I just did a sql dump using a python script and compressed it. When I tried to restore the same in my local machine I am getting an error "Duplicate entry '1' for key 'PRIMARY'". But I have created a separate test database, which is empty.
Whats the problem and what would be the solution?

I suppose your dump contains database name inside (something like USE your_database), so when you try to restore it you get that error.
Another condition could be a wrong dump: did you check your dump does not contain really duplicate keys?
And more: do your dump have DROP TABLE ... IF EXISTS.. ?

Related

Duplicate entry '1' for key 'PRIMARY' while table is empty

I am using bluehost's mysql phpmyadmin for my app's database. While testing, suddenly I can't insert and it's showing an error "DUPLICATE ENTRY '1' FOR KEY 'PRIMARY' " while the table is still empty. what is wrong?? what is the cause of this issue?
I already tried dropping the whole database and recreating it thru the following:
importing recently exported copy of database
creating the table one by one thru code (not thru phpmyadmin's "create table")
I changed InnoDB to MyISAM (some table worked but some had no change)

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

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.
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
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".
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!
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.
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.
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.
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.
I have met the same problem, I drop the table and rebuilt the database, then the problem solved.

Integrity error django

I was using sqlite3 for my database in django project and then I decided to change it to mysql engine. I changed everything in the settings.py correctly, and in order not to lose the db I dumped my sqlite one to text and I changed every " with `, removed Commit and all things related to sqlite3. I executed the dump file on mysql and everything was added perfectly. However, when I try to add a cateogory in a table called categories.
I get this error message
IntegrityError at /admin/red_carpet/category/
(1062, "Duplicate entry '0' for key 1")
I thought the key is duplicate wich what is the error saying. I looked at the "id" column to see it is structure I found that the id column is "NOT NULL". There was no auto increment added to that column so I added it and tried to do category from admin page I got the same error I looked again at the structure and then I added the query that the auto_increment shouldn't start from 0 but should start from last value which was 6. Then I added the category from the django admin again but I still get the same error, looked again at the structure and especially in the indexes part in phpmyadmin I found that line
Keyname Type Cardinality Action Field
PRIMARY PRIMARY 9 id
For me, everything looks good and actually when I add value through django admin I get it added though I get the same error message
IntegrityError at /admin/red_carpet/category/
(1062, "Duplicate entry '0' for key 1")
I really can't understand why is this problem !!!! Anyone have been thorough this before !! ??
PS: I get the same error message when I try to remove something. For example, if I am removing a cateogory I get the exat error message though when I look at the db I found that the category has been removed !!
One way to prevent cross-db issues would be to use fixtures, e.g. dump and load your data in (db-agnostic) json format:
see dump data command and
what is a fixture
from the django documentation.

Importing SQL file "Duplicate entry '0' for key 'PRIMARY'"

I know there is similar questions that are answered but please try to help me out.
I'm importing a backup of my MyBB database into a new host using PHPMyAdmin. When trying to import the .sql file, I get the following error:
SQL query:
INSERT INTO mybb_datacache( `title` , `cache` )
VALUES (
'internal_settings', 'a:1:{s:14:\"encryption_key\";s:32:\"rrvohvVATtOauucNTmEXAmvNvbw9ujvb\";}'
);
MySQL said:
#1062 - Duplicate entry 'internal_settings' for key 'PRIMARY'
I can't figure out the problem. I emptied all tables and clean install of the database four times already. I've been trying to figure this out all day and it's very frustrating.
(link removed)
Please help me out. How do I fix this problem?
When you export your sql from php admin
Select "custom" as export method"
then, instead of 'insert', choose "update"
This will perform update-statements and prevent duplicated inserts.
Make sure the column set as your PRIMARY KEY is set to AUTO_INCREMENT
From the current version of the question, it appears that the title column of the mybb_datacache table is the Primary Key and already has a record with the value 'internal_settings' in it. If this is indeed the case, then the problem is that all records must have unique values for their Primary Key.
I had this problem, and i found what the problem is.
The thing is that, the field cannot be empty. ie, if you are altering a table to add primary key, make sure you have some values in that field. or if u are importing a sql file, make sure that that field in the file has some values....
this solved my problem...

phpMyAdmin: MySQL Error 1062 - Duplicate entry

I connect with user "root" onto my database "test" which I host locally for development. Among others I have the table "ratingcomment". For some reason when I click on the table "ratingcomment" phpMyAdmin shows me the following error:
Fehler
SQL-Befehl:
INSERT INTO `phpmyadmin`.`pma_history` (
`username` ,
`db` ,
`table` ,
`timevalue` ,
`sqlquery`
)
VALUES (
'root', 'test', 'ratingcomment', NOW( ) , 'SELECT * FROM `ratingcomment`'
)
MySQL meldet:
#1062 - Duplicate entry '838' for key 'PRIMARY'
I used google to finde out the following
"This indicates that you have a UNIQUE or PRIMARY index on a table, and there is a duplicate value someone on one of the values in one of these indexes."
But I still dont quite understand the error! I use a primary Key, which auto-increments for all of my tables, so there actually shouldnt be a problem with the table. I had another table named "rating" which had a column "comment". Can it be, that this causes problems?
Quick fix:
REPAIR TABLE `phpmyadmin`.`pma_history`
If that fails, I'd just truncate/empty the table.
TRUNCATE TABLE `phpmyadmin`.`pma_history`
Although phpmyadmin has it's place in my toolbox, I personally don't use it's internal db.
ADDENDUM
MyISAM tables can easily become corrupted. A couple causes that usually hit me: if the MySQL is not shutdown properly, or if the table has a FULLTEXT index and the stopword file on disk had changed.
Simply stated, the REPAIR just checkes the data file for errors (and depending on your options, makes it usable again) and rewrites the index file. Fair warning: with MyISAM, repairing a table can often toast all your data in that table to make it usable. See doc for more details.
A google search pertaining to this pma table being corrupted lead me to this.
This appears to be an internal error. You've issued this query:
SELECT * FROM `ratingcomment`
phpMyAdmin tries to write such action in its internal event log and it fails. If you Google for pma_history you'll find several references to such table being corrupted.
My advice is that you find another SQL client (such as HeidiSQL) and try to repair the phpMyAdmin database.
I know this is kinda late but I had the same problem and wanted to share what I did.
In PhpMyAdmin, I went to the table's Operation tab, and just incremented the AUTO_INCREMENT value under Table options and inserted a dummy record.