I added a record in a table in MySQL database, now I am trying to delete that particular record from table, but it is not getting deleted & showing this message:
#1062 - Duplicate entry '3107' for key 'PRIMARY'
How do I delete this entry?
You can not get this error during DELETE. UPDATE or INSERT can return that
So either you are not running DELETE or you have trigger on your table, that is making some other changes, that give you that error.
If child tables have ON DELETE CASCADE,check them for triggers as well.
delete from table_name [where condition];
use this statement, replace table_name with your table name and put the condition.
If you do not specify where clause, every records will be deleted.
Related
I'm trying to insert data in a table which has the following columns :
id, date, client, device, user
I have created a primary key on id and unique key on the combination of client, device, and user.
While inserting data I getting the following Error:-
Cause: java.sql.SQLException: Duplicate entry '200-217-Xiaomi-M200' for key 'uk_user_client_device'
I checked the data in the table using the following query using:-
SELECT user, client, device, COUNT(1) rowcount FROM mytable GROUP BY user, client, device HAVING COUNT(1) > 1;
This got an empty set in response so I am certain there are no duplicate keys in the table.
I also went through the logs and I found that the data was inserted in the table at the same time I got this error. So, the data was inserted yet I got the duplicate entry error.
I also confirmed that this doesn't happen always. Sometimes, the data is inserted without any issue and sometimes I get an error and the data is inserted anyway.
I've seen a few questions regarding this with no definitive answer. I'm unable to figure out why this error is being thrown.
Of course this query returns no rows:
SELECT user, client, device, COUNT(1) as rowcount
FROM mytable
GROUP BY user, client, device
HAVING COUNT(1) > 1;
You have specified that client/device/user is a primary key (or at least unique. Hence, there are no duplicates. The database enforces this.
If you attempt to insert a row that would create a duplicate, the database returns an error. The row is not inserted. The database ensure the data integrity. Yay!
Your error is saying that the data in the insert either duplicates existing data in the table. Or, if you are inserting multiple rows, then the data has duplicates within the insert.
You can solve this in multiple ways. A typical method is to ignore the insert using on duplicate key update:
insert into mytable ( . . . )
. . .
on duplicate key update user = values(user); -- this is a no-op
The database prevents duplicates from being inserted. This construct prevents an error -- although no duplicate rows are inserted, of course.
So I am deleting records from a table by joining that table to another table.
I have disabled the foreign keys before I started running this statement.
So I have two tables A and B and I am deleting columns from table A using a join with column B to delete records that match on column id and one more criteria in where clause.
Here is the query
SET FOREIGN_KEY_CHECKS=0;
delete db.A from db.A join db.B USING(id) where name='xx';
SET FOREIGN_KEY_CHECKS=1;
Why do I still get the following 'State' in mysql process list
deleting from reference tables
Because you are "referencing a table" (db.A) in a multi table query (db.A and db.B).
If you don't specify a table on a "delete join", the query will not work, because mysql will not know which table you are trying to update.
Thereby you reference a table for delete.
I am trying to delete all rows from a table with a particular id.
my query is:
DELETE FROM table_name WHERE x_id='46';
the error returned is:
#1136 - Column count doesn't match value count at row 1
my table has a composite primary key x_id is one of the columns in the primary key.
Please Help!
That error is strange for a delete statement. It is most likely coming from badly written trigger that is being executed as a result of the delete.
This error would most likely be encountered on an insert statement such as the following:
insert into foo(bar, baz)
select bar, baz, foobar, 2
from myTable
Note how the insert statement specifies 2 columns, but provides 4 values.
You might try to provide a second value to the delete query to match the composite index for the row.
DELETE FROM CPI
WHERE (CountryID, Year) IN (('AD', 2010), ('AF', 2009), ('AG', 1992))
Cause:
You may have a trigger on this table, then you changed the table structure.
Now, you may get this error when you delete, insert, or update in this table (depending on the trigger event you specified).
Solution:
To solve this issue, you have to update the trigger as well, the number of columns of the trigger should match the number of the columns of the table.
I'm attempting to insert, update, and delete all in one MySQL query. I have a DB with about 100 records with a primary key. I'm updating the DB from a CSV file. What I would like to happen is if a record is in the csv and not in the db, then add it. If it's in the db and the csv, update it. If it's in the db and not in the csv, delete it. I have the insert and update part working, but I'm hung up on the delete part.
Here is my query so far:
INSERT INTO mydb
(tourID,agent) VALUES (:tourID,:agent)
ON DUPLICATE KEY UPDATE
tourID=:tourID
Is there anything like 'on non duplicate key delete'?
Have an extra column named "toDelete". At the beginning of your transaction, set it to true for all rows. When you update a row, set it to false. When you are done, delete every row where toDelete is still true.
Here's some pseudocode:
For each record in the CSV file, run your INSERT INTO query.
Run a DELETE FROM mydb WHERE tourID NOT IN (comma-separated list of tourIDs from CSV)
How you come up with that comma-separated list of tourIDs from CSV depends on how you're processing your CSV file.
I am using a API to fetch records from a different server and insert into my local database but when a particular field say apiserverID is duplicate i just want to update fields.
my problem is that i have table structure as
myPrimaryKey
apiserverID
....
.....
.....
updateDate
now i want simple procedure to update the row if apiserverID is duplicate.
Only solution i know is i have to check (SELECT) if the key apiserverID exist then update else insert.
but i don't want do programming for this is this possible in one query
EDIT : Main problem is that INSERT ... ON DUPLICATE KEY UPDATE don't work for a particular field, it include all the keys to check duplicity
if there is a single multiple-column unique index on the table, then the update uses (seems to use) all columns (of the unique index) in the update query.
So if there is a UNIQUE(a,b) constraint on the table in the example, then the INSERT is equivalent to this UPDATE statement:
UPDATE table SET c=c+1 WHERE a=1 AND b=2;
(and not "a=1 OR b=2")
But in your case it will work as expected because first one is your primary key and it will never be duplicate form the server so only thing which can be duplicate is your apiserverID so when ever found duplicate it will update the row else always a new insert will be executed