I am using MySql with JDBC...the below is my table definition
CREATE TABLE `A` (
`id` int(255) NOT NULL AUTO_INCREMENT,
`type` varchar(255) NOT NULL,
`value` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `unq` (`type`(50),`value`(50))
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
At the moment i am using statement in order to batch insert rows to that table with RETURN_GENERATED_KEYS option in order to relate the id to other table.
I want the ability to perform that operation but when / if DUPLICATE ENTRY occur ( the same combination of type & value ) to continue with the transaction as like nothing happens but still to get the generated keys, and if DUPLICATE ENTRY occur retrieve the existing key.
Thanks
use IGNORE keyword
INSET IGNORE ....
as Quoted in Mysql
If you use the IGNORE keyword, errors that occur while executing the
INSERT statement are treated as warnings instead. For example, without
IGNORE, a row that duplicates an existing UNIQUE index or PRIMARY KEY
value in the table causes a duplicate-key error and the statement is
aborted. With IGNORE, the row still is not inserted, but no error is
issued.
alternative syntax
INSERT ... ON DUPLICATE KEY UPDATE
Related
I have a table that needs a unique constraint on 3 columns, but, if the "date" column in for that insert transaction is a newer date than the current record's date, then I want to update that record (so the unique constraint is still true for the table).
Postgres has the concept of deferrable constraints, MySQL does not.
I do want to implement it with the SQL object tools available, though.
Here is my table DDL with column names obfuscated:
CREATE TABLE `apixio_results_test_sefath` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`number` varchar(20) DEFAULT NULL,
`insert_date` datetime DEFAULT NULL,
`item_id` int(5) DEFAULT NULL,
`rule` tinyint(4) DEFAULT NULL,
`another_column` varchar(20) DEFAULT NULL,
`another_column1` varchar(20) DEFAULT NULL,
PRIMARY KEY (`ID`),
KEY `insert_date_index` (`insert_date`),
KEY `number` (`number`),
) ENGINE=InnoDB AUTO_INCREMENT=627393 DEFAULT CHARSET=latin1
and here is the unique constraint statement
Alter Table dbname.table add unique constraint my_unique_constraint (number, item_id, rule);
but I can not add a condition here in this constraint (unless there is a way I'm not aware of?)
The logic I need to run before inserts are blocked by the constraint is to check if the three values: number, item_id, and rule are unique in the table, and if they aren't, then I want to compare the existing record's insert_date with the insert_date from the transaction, and only keep the record with the newest insert_date.
This could be achieved with a trigger I suppose, although I've heard triggers are only to be used if really needed. And on every insert, this trigger would be quite computationally taxing on the DB. Any advice? Any other sql tricks I can use? Or anything to help point me to how to make this trigger?
I tried the unique constraint statement
Alter Table dbname.table add unique constraint my_unique_constraint (number, item_id, rule);
But it will never update with the newer insert_date.
You can do this with an insert statement like:
insert into apixio_results_test_sefath (number, item_id, rule, insert_date, another_column, another_column1)
values (?,?,?,?,?,?)
on duplicate key update
another_column=if(insert_date>values(insert_date),another_column,values(another_column),
another_column1=if(insert_date>values(insert_date),another_column1,values(another_column1),
insert_date=greatest(insert_date,values(insert_date)
for each column besides the unique ones and insert_date, testing to see if the existing insert_date is greater than the value supplied with the insert and conditionally using the existing value or new value for the other column based on that, and ending with updating insert_date only if it is now greater.
mysql 8 has an alternate syntax it prefers to using the values function, but the values function still works.
If you want this to happen automatically for all inserts, you would need to use a trigger.
I have the following table with a unique index by field "position_in_list":
CREATE TABLE `planned_operation` (
`id` bigint(20) NOT NULL,
`position_in_list` bigint(20) NOT NULL,
`name` varchar(255) not null
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
ALTER TABLE `planned_operation`
ADD PRIMARY KEY (`id`),
ADD UNIQUE KEY `position_in_list` (`position_in_list`);
ALTER TABLE `planned_operation`
MODIFY `id` bigint(20) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=3;
INSERT INTO `planned_operation` (`id`, `position_in_list`, `name`) VALUES
(1, 1, 'first'),
(2, 2, 'second');
Then I have a trivial task, this is a change in position when updating the list. Accordingly, you need to update the list of items before which the record was inserted. In order not to perform thousands of updates, I execute one query:
update planned_operation
set position_in_list = case position_in_list
when 2 then 3
when 1 then 2
end
where position_in_list in (1, 2)
But when executing an error is issued:
#1062 - Duplicate entry '1' for key 'position_in_list'
Is there any way to avoid an error? Without disabling the unique index
You want deferrable constraints.
Unfortunately, MySQL does not implement deferrable constraint checks -- an integral part of SQL that few database engines implement.
As far as I know only PostgreSQL and Oracle (partial) do implement them.
In simple words, this means that MySQL checks the unique constraint on every single row change inside an UPDATE statement. With deferrable constraints you could defer this check to the end of the statement, or even to the end of the database transaction.
Now, you would need to switch to PostgrSQL or Oracle to defer contraints checks to the end of the statement (as you seem to want). I guess that's way out of the scope for you, but it's a theoretical option.
For a more in depth discussion you could look into Deferrable Constraints answer.
I have a composite unique key on two columns, "user_id" & "project_id".
When I try to run a DELETE query on single rows or multiple rows, I get an error.
ERROR 1062: 1062: Duplicate entry '87-1736' for key 'index_on_user_id_and_project_id'
SQL Statement:
DELETE FROM `members` WHERE `id`='39142'
The table has a single column primary key, 2 single column unique indexes (for user_id and project_id), and 1 composite unique index on user_id and project_id. No foreign keys in the database.
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL DEFAULT '0',
`project_id` int(11) NOT NULL DEFAULT '0',
`created_on` datetime DEFAULT NULL,
`mail_notification` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
UNIQUE KEY `index_on_user_id_and_project_id` (`user_id`,`project_id`),
KEY `index_members_on_user_id` (`user_id`),
KEY `index_members_on_project_id` (`project_id`)
This error only shows up for certain entries (a lot of entries) and it is consistently those entries that are problematic (e.g. 87 and 1736 pair shown above).
I have tried looking for duplicates and none were found. I was able to find some entries in there with "0"s in the fields and I removed those entries. No NULL fields were found.
I have tried:
looking for duplicates, found none.
looking for zero or NULL values in the index fields, deleted, but did not solve
removing the composite unique index, did not solve.
alter ignore table ... add unique index (user_id, project_id), it found no duplicates, threw a warning about IGNORE being deprecated, and did not solve
How do I delete these problematic entries?
It is impossible for a delete statement itself to generate a duplicate key error. At least, I cannot think of any way for that to happen in an unbroken database. After all, if you are removing a value, it can't conflict with another value.
That leaves the possibility that something else is going on. The only reasonable alternative is a trigger on the table. It is unfortunate that the error message doesn't specify the table name, but that is the only cause that I can readily think of.
I've bumped into this before when I've had the target table (t1) tied to a history table (t1_hist). t1_hist was populated by a trigger on changes to the t1 (any add/change/delete). Once I deleted the unwanted records from the t1_hist, I was able to delete from the t1. This required a second pass delete from the t1_hist table because it recorded my deletes from t1.
Simply, mine was: DELETE FROM t1 WHERE customer_number > 50000;
Same error. Did the same from t1_hist first (and last), then no problem.
The MySQL documentation states that when using INSERT ... ON DUPLICATE KEY UPDATE, the affected-rows value will be 1 when a record was inserted, 2 if an existing record was updated.
We're getting an affected-rows value of 3 when a record is updated, although I'm only seeing this when the updates are performed via MySQL calls using Connector-J from Java; when I invoke the stored procedure from the MySQL Workbench, I get the expected result of 2 rows updated.
Does anyone have any idea what this might mean? Is this perhaps a Connector-J annomaly? I'd be inclined to just run with it, but without a reasonable explanation, I'm left with concerns for my data integrity (which is, after all, one of the reasons to check the affected-rows value).
MySQL server version: 5.1.57; Connector-J version: 5.1.7 (Java 1.6)
Additional details:
This is the table being modified:
CREATE TABLE `UserContactProperty` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`UserContactId` int(11) NOT NULL,
`Property` varchar(45) NOT NULL,
`Value` tinytext,
`Date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `Contact-Property` (`UserContactId`,`Property`),
KEY `FK_UserContact` (`UserContactId`),
CONSTRAINT `FK_UserContact` FOREIGN KEY (`UserContactId`) REFERENCES `UserContact` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=28685 DEFAULT CHARSET=latin1
The stored procedure performing the insert/update is as follows:
CREATE PROCEDURE `setUserContactProperty`(
UID VARCHAR(50),
CID INT,
Prop VARCHAR(45),
Val TINYTEXT
)
BEGIN
INSERT INTO UserContactProperty ( UserContactId, Property, Value )
VALUES ( CID, Prop, Val )
ON DUPLICATE KEY UPDATE Value = Val, Date = CURRENT_TIMESTAMP;
END
It seems as a bug. please check the following link:
http://bugs.mysql.com/bug.php?id=46675
As reported there, the bug is related to another bug where the generated key list is wrong and the count is wrong too. (the second bug originated from here http://slava-technical.blogspot.co.il/2011/05/mysql-on-duplicate-key-update-breaks.html)
I think you need to find a workaround. probably by querying the database prior to inserting to see if the row with this key exists or not. or insert and catch duplicate key exception and then make the update in that case.
RESOLVED
From the developer: the problem was that a previous version of the code was still writing to the table which used manual ids instead of the auto increment. Note to self: always check for other possible locations where the table is written to.
We are getting duplicate keys in a table. They are not inserted at the same time (6 hours apart).
Table structure:
CREATE TABLE `table_1` (
`sales_id` int(10) unsigned NOT NULL auto_increment,
`sales_revisions_id` int(10) unsigned NOT NULL default '0',
`sales_name` varchar(50) default NULL,
`recycle_id` int(10) unsigned default NULL,
PRIMARY KEY (`sales_id`),
KEY `sales_revisions_id` (`sales_revisions_id`),
KEY `sales_id` (`sales_id`),
KEY `recycle_id` (`recycle_id`)
) ENGINE= MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=26759 ;
The insert:
insert into `table_1` ( `sales_name` ) VALUES ( "Blah Blah" )
We are running MySQL 5.0.20 with PHP5 and using mysql_insert_id() to retrieve the insert id immediately after the insert query.
I have had a few duplicate key error suddenly appear in MySql databases in the past even though the primary key is defined and auto_increment. Each and every time it has been because the table has become corrupted.
If it is corrupt performing a check tables should expose the problem. You can do this by running:
CHECK TABLE tbl_name
If it comes back as corrupt in anyway (Will usually say the size is bigger than it actually should be) then just run the following to repair it:
REPAIR TABLE tbl_name
Does the sales_id field have a primary (or unique) key? If not, then something else is probably making inserts or updates that is re-using existing numbers. And by "something else" I don't just mean code; it could be a human with access to the database doing it accidentally.
As the other said; with your example it's not possible.
It's unrelated to your question, but you don't have to make a separate KEY for the primary key column -- it's just adding an extra not-unique index to the table when you already have the unique (primary) key.
We are getting duplicate keys in a table.
Do you mean you are getting errors as you try to insert, or do you mean you have some values stored in the column more than once?
Auto-increment only kicks in when you omit the column from your INSERT, or try to insert NULL or zero. Otherwise, you can specify a value in an INSERT statement, over-riding the auto-increment mechanism. For example:
INSERT INTO table_1 (sales_id) VALUES (26759);
If the value you specify already exists in the table, you'll get an error.
Please post the results of this query:
SELECT `sales_id`, COUNT(*) AS `num`
FROM `table_1`
GROUP BY `sales_id`
HAVING `num` > 1
ORDER BY `num` DESC
If you have a unique key on other fields, that could be the problem.
If you have reached the highest value for your auto_increment column MySQL will keep trying to re-insert it. For example, if sales_id was a tinyint column, you would get duplicate key errors after you reached id 127.