I have a table defined with a single primary/auto-increment key.
When I call the following query I receive an error.
INSERT INTO device_sensor_reading (`we_id`, `UNIX_time`, `temp_we_id`, `reading_format_id`,`log_id`, `msg_type`, `device_position`) VALUES
(79995, 1550896918, 0, 3, 1, 0,1);
Error: 08:43:39 call sits_db.Insert Simulated Data() Error Code: 1062. Duplicate entry '79995-1550896918' for key 'we_id_UNIX_time' 0.063 sec
Neither we_id or UNIX_time are specified as UNIQUE in my table so why does MySQL give me this error?
There is a combined UNIQUE KEY. MySQL allows you to combine two columns, which results that only the combination of both values is unique.
In your table you specified w_id, temp_w_id , in your query are we_id and temp_we_id, now, check if you have any value with this entry in two fields. Now is the same with log_id... Maybe unique field is repeating
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.
I am trying to do an insert statement. I have an unique index setup that combines phone_number and niche.
INSERT INTO CV_YP.table (phone_number,website,niche) VALUES
(1231231234, 'website.com', 'fitness')
The return value says I have a duplicate entry but the phone number is different then the one I just tried to insert.
ERROR 1062 (23000) at line 1: Duplicate entry '2147483647-fitness' for
key 'idx_table_phone_number_niche'
I can't figure out why MySQL thinks my insert is related to the index duplication it returns.
i think if you run this query
SELECT * FROM CV_YP.table WHERE phone_number = '2147483647' AND niche = 'fitness';
you will find more than 1 row, so I think its a violation of the unique index, so when the engine tries to check for uniqueness on the new data this error fire
update
I think your phone number isn't 1231231234 right?
and it's a number greater than 2147483647 and you use phone_number data type as int
If that true I think you need to change the data type of the phone_number column because the max value of int is 2147483647 so when you insert the new number the engine sees it as the max value which is 2147483647 and try to insert it.
I have table MySQL with Primary Key Composed of 2 fields, as below
Already existing records in the table are:
The INSERT query I am issuing is:
When I run the query:
INSERT INTO `case_data`
VALUES ('WCD/2016/1000017', 2, '2016-09-29', 'WCD',***********************
The error message displayed is:
[Err] 1062 - Duplicate entry 'WCD/2016/1000017' for key 'PRIMARY'
Am I violating the Primary Key constraint?
Thanks in advance.
You could check if the primary key values of a row you are trying to insert already exist in a table:
SELECT COUNT(*)
FROM case_data
WHERE caseno = 'WCD/2016/1000017' AND iteration = 2;
If it returns 0 then you will not violate the PK constraint and are safe to insert the row you wish (assuming there are no additional checks, triggers, constraints). Otherwise it will return 1 which means that you already have a row with values in those columns, thus you would violate uniqueness of the row which is not allowed.
When it returns 0 just issue an INSERT command. Also, remember to specify your column tables within the statement to make sure every value from your VALUES is being put within the right column of your destination table:
INSERT INTO case_data (caseno, iteration, casedate, casetype)
VALUES ('WCD/2016/1000017', 2, '2016-09-29', 'WCD');
Avoid using backticks around your column and table names if they don't contain alternative characters like commas or spaces. This will make your code more readable and definitely speed up your writing time.
As per a prior question, I created a Unique Index on a name field. I've simplified my import so I don't need to merge fields any longer, just import from SourceTable into DestinationTable, the latter having Unique Index on Name.
I got an error immediately about a duplicate value existing when I tried to do the insert, so I guess that is good news, is there a way to specify an insert that will just skip the duplicate values and go to the next vs throwing out the "[Err] 1062 - Duplicate entry 'Actor' for key 2" and quitting?
In MySQL you can use insert ignore to ignore all errors, including duplicate key.
To just ignore this one error, you can use on duplicate key update:
insert into t(cols)
select values
from wherever
on duplicate key update col1 = values(col1);
The update part doesn't do anything important. It just does something so no error is reported.
I have two tables, the first table has 400 rows. The second table holds the same records with the same count. Now the first table row count increases to 450. I want to insert only those 50 new rows into the second table. I don't need to update the first 400 records.
I am setting the unique index for the particular field (like empid). Now when I insert the first table data it returns the following error:
Duplicate entry 'xxxx' for key 'idx_confirm'
Please help me to fix this error.
Am using the below code to insert the record. But it allows duplicate entry..
insert ignore into tbl_emp_confirmation (fldemp_id,fldempname,fldjoindatefldstatus)
select fldempid, fldempname,DATE_FORMAT (fldjoindate,'%Y-%m-%d') as fldjoindate,fldstatus from tblempgeneral as n;
Modify your INSERT ... statement to INSERT IGNORE ....
See for example this post for an explanation.
You need to make sure that you have a unique index that prevents any duplicates, such as on the primary key.