phpMyAdmin: MySQL Error 1062 - Duplicate entry - mysql

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.

Related

MySQL Database Upload Error

My query relates to a WordPress website, which is hosted on a VPS.
Earlier today, I needed to upload my backed up Database. Upon completion, I was returned with the message:
SQL query:
ALTER TABLE 'wp_options'
ADD PRIMARY KEY ('option_id)
ADD UNIQUE KEY 'option_name' ('option_name')
MySQL said:
#1062 - Duplicate entry '0' for key 'PRIMARY'
The website seems to have fully uploaded, with all of its content, however (and I am not sure if this is connected) when I edit a page and select 'Update', it does not seem to save the modifications.
I am hoping that by resolving the database issue, the other issue will look after itself.
If anyone could provide me with a straight forward fix, it would be much appreciated.
Thanks.
Craig
Maybe you need auto incrément on primary key
MySQL doc say :
To start with an AUTO_INCREMENT value other than 1, set that value with CREATE TABLE or ALTER TABLE, like this:
mysql> ALTER TABLE tbl AUTO_INCREMENT = 100;

MySQL error 1215: Can not add foreign key constraint

I am trying to forward engineer my new schema onto my db server, but I can't figure out why I am getting this error. I've tried to search for the answer here, but nothing I've tried worked.
This is the error I get when I am trying to forward engineer my new schema onto my DB server:
Few things might cause an issue:
1) Check what data types are studentnummer column in student table and vakID in vakklas - Are they INT and VARCHAR(45)?
2) Check for typing errors - are tables and columns you're referencing named same way as in this CREATE statement?
3) Check if student and vakklas already have some rows that violate NOT NULL condition - is there a record with NULL value in referenced columns?
4) Are you using InnoDB for all tables?
5) Are you sure charset is the same on all tables? This is important especially because you're using VARCHAR as foreign key. Try adding DEFAULT CHARSET=utf8 after ENGINE = InnoDB on all queries for table creating - it might solve the problem

mySQL corruption "Cannot add foreign key constraint" trying to CREATE table

EDIT This is a corrupt mySQL DB problem. Pls see "EDIT:" below
Trying to add a table called client to a new mySQL DB:
CREATE TABLE `client` (
`id` INT NOT NULL AUTO_INCREMENT,
`person_id` INT NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
)
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB;
Every time I get: #1215 - Cannot add foreign key constraint.
I have looked at all the reserved word lists I can find. None of them seem to have "client".
I can use "clients" (but do not like having table as plurals -0 all the others are singular), "clientxxx" etc. I REALLY want to use "client". Is there a way round this? It is important that they are clients not patients.
PS I have deleted all other tables from the DB this still will not fly.
PPS Although not listed CLIENT is a ****** reserved word in SQL in reality (or at least HeidiSQL lists it. Is there any way around it? (I think I know the answer to that one). And why the heck is it not in the mySQL reserved words list?
EDIT: OK dropped old DB. Tried with a DB called testxxx and worked fine. Tried to rename DB to old name = betadb01 and got:
Database "betadb01" exists. but it does not contain objects with the same names as in "testxxx", so it's uncritical to move everything. Move all objects to "betadb01"?
... said yes and then got SQL error (1025) ... errno 150 - foreign key constraint is incorrectly formed
I have run all the repair options in HeidiSQL. Looking through the repair and analysis logs the DB in question is nowhere to be seen. I can pretty easily rename the DB and get past the problem but this is a bit nervous making. Any ideas how I can clean out this problem? Complete reinstall of mySQL after exporting my other databases???

#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.

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...