MySQL ON DUPLICATE KEY UPDATE does not update but inserts - mysql

INSERT INTO TABLE(APIKEY, CLIENTID, rID) VALUES('33333meow', '2', '3')
ON DUPLICATE KEY UPDATE APIKEY='meo11111111w';
The above code will insert but if run a second time, the APIKEY field does not update it just inserts another row.
I've looked at documentation and it looks correct. What else am I doing wrong?

Create a unique key on your table. Probably the columns clientid and rid. That will trigger the duplicate key trigger

Related

ON DUPLICATE KEY unrecognized in phpmyadmin

I have a mysql database hosted on a remote server, I'm using InnoDB. Everything else works fine but on phpmyadmin whenever I try to execute the following query
INSERT INTO User (user_id, location) VALUES (1, 'London') ON DUPLICATE KEY UPDATE location=VALUES('London')
It highlights ON, DUPLICATE, KEY because they're unrecognized keywords
Help please!
Please remove the VALUES(...) in your ON DUPLICATE KEY part.
INSERT INTO User (user_id, location) VALUES (1, 'London') ON DUPLICATE KEY UPDATE location = 'London'
If you want to update more than one column, you should use normal UPDATE syntax:
INSERT INTO User (firstColumn, secondColumn) VALUES (1, 'London') ON DUPLICATE KEY UPDATE firstColumn = 'ABC', secondColumn = 'DEF';
Before using ON DUPLICATE KEY you should add index for one or multiple column. You can use following command for adding new index.
ALTER TABLE `table_name` ADD UNIQUE `unique_index`(`column1`);
For multiple column index use following command.
ALTER TABLE `table_name` ADD UNIQUE `unique_index`(`column1`, `column2`);
Now you can INSERT/UPDATE one or more row together.
For inserting single row use following command.
INSERT INTO `table_name` (`column1`,`column2`,`column3`,`column4`) VALUES('value1','value2','value3','value4') ON DUPLICATE KEY UPDATE `column3`='value3', `column4`='value4';
OR
INSERT INTO `table_name` (`column1`,`column2`,`column3`,`column4`) VALUES('value1','value2','value3','value4') ON DUPLICATE KEY UPDATE `column3`=VALUES(`column3`), `column4`=VALUES(`column4`);
Using this command you can also INSERT/UPDATE multiple rows. Use following command for this purpose.
INSERT INTO `table_name` (`column1`,`column2`,`column3`,`column4`) VALUES('value11','value12','value13','value14'),('value21','value22','value23','value24') ON DUPLICATE KEY UPDATE `column3`=VALUES(`column3`), `column4`=VALUES(`column4`);

Insert into mysql if postid is new, else update

I am facing this situation. I want to insert into a database like this :
INSERT INTO
`mytable` (`id`, `post_id`, `user_id`, `dtime`)
VALUES ('','$pid','$uid',NOW())"
The ID column is primary and with auto-increment. And I need to check if the post_id is not getting inserted twice.
Any idea on how to do that, so that if the post_id exists, it does an update, without needing two queries ?
Use the ON DUPLICATE KEY UPDATE syntax.
If you specify ON DUPLICATE KEY UPDATE, and a row is inserted that would cause a duplicate value in a UNIQUE index or PRIMARY KEY, MySQL performs an UPDATE of the old row.
http://dev.mysql.com/doc/refman/5.7/en/insert-on-duplicate.html
This link was the second result when I searched for "mysql insert or update" on Google.
Use a REPLACE query. It does exactly what you need, and looks like an INSERT except it starts with "REPLACE INTO"
http://dev.mysql.com/doc/refman/5.0/en/replace.html

MySQL "Insert ... On Duplicate Key" with more than one unique key

I've been reading up on how to use MySQL insert on duplicate key to see if it will allow me to avoid Selecting a row, checking if it exists, and then either inserting or updating. As I've read the documentation however, there is one area that confuses me. This is what the documentation says:
If you specify ON DUPLICATE KEY UPDATE, and a row is inserted that would cause a duplicate value in a UNIQUE index or PRIMARY KEY, an UPDATE of the old row is performed
The thing is, I don't want to know if this will work for my problem, because the 'condition' I have for not inserting a new one is the existence of a row that has two columns equal to a certain value, not necessarily that the primary key is the same. Right now the syntax I'm imagining is this, but I don't know if it will always insert instead of replace:
INSERT INTO attendance (event_id, user_id, status) VALUES(some_event_number, some_user_id, some_status) ON DUPLICATE KEY UPDATE status=1
The thing is, event_id and user_id aren't primary keys, but if a row in the table 'attendance' already has those columns with those values, I just want to update it. Otherwise I would like to insert it. Is this even possible with ON DUPLICATE? If not, what other method might I use?
The quote includes "a duplicate value in a UNIQUE index". So, your values do not need to be the primary key:
create unique index attendance_eventid_userid on attendance(event_id, user_id);
Presumably, you want to update the existing record because you don't want duplicates. If you want duplicates sometimes, but not for this particular insert, then you will need another method.
If I were you, I would make a primary key out of event_id and user_id. That will make this extremely easy with ON DUPLICATE.
SQLFiddle
create table attendance (
event_id int,
user_id int,
status varchar(100),
primary key(event_id, user_id)
);
Then with ease:
insert into attendance (event_id, user_id, status) values(some_event_number, some_user_id, some_status)
on duplicate key
update status = values(status);
Maybe you can try to write a trigger that checks if the pair (event_id, user_id) exists in the table before inserting, and if it exists just update it.
To the broader question of "Will INSERT ... ON DUPLICATE respect a UK even if the PK changes", the answer is yes: SQLFiddle
In this SQLFiddle I insert a new record, with a new PK id, but its values would violate the UK. It performs the ON DUPLICATE and the original PK id is preserved, but the non-UK ON DUPLICATE KEY UPDATE value changes.

Can I use INSERT INTO ... On DUPLICATE KEY without by using auto_increment value?

I am trying to write a query to check if a record exists (based on couple of clause and not unique identifier) if such a search return records then I need to update all the found records if nothing found then I need to INSERT a record. Note that I can't use IF EXISTS because I am trying to make a query for a client side script and not a server side. So I came a cross the idea of INSERT INTO .... ON DUPLICATE KEY
Can I do this without knowing the row key identifier? So if I find a record where accountid = 17 and name = 'Mike' then update it to make the name 'Mike A' if there is no record with these 2 clause then insert a record.
This is an attempt that is giving me a syntax error
INSERT INTO test (name, accountid) VALUES ('Mike', 17)
ON DUPLICATE KEY
UPDATE test SET name='Mike A' WHERE name ='Mike' AND accountid = 17
Can this method handle what I am trying to do? If yes then can you please correct my syntax?
Thank you
The only way you can get this to work is if you have a primary key or unique constraint on the fields. From the documentation:
If you specify ON DUPLICATE KEY UPDATE, and a row is inserted that
would cause a duplicate value in a UNIQUE index or PRIMARY KEY, an
UPDATE of the old row is performed. For example, if column a is
declared as UNIQUE and contains the value 1, the following two
statements have identical effect:
create table test (name varchar(100), accountid int);
insert into test values ('Mike', 17);
alter table test add unique (name, accountid);
insert int test (name, accountid) values ('Mike', 17)
on duplicate key update name='Mike A';
SQL Fiddle Demo
Without the unique key, it will insert a duplicate record.

mysql duplicate key update issue

I have my index set to the field: testsuite_id and I'm executing the following query:
INSERT INTO testsuite_dates (testsuite_id,start_date,end_date)
VALUES ('27798','2012:02:27 00:00:00','2012:02:28 00:00:00')
ON DUPLICATE KEY UPDATE testsuite_id='27798';
I would expect this to update the entry if an entry for testsuite_id exists or insert it if it doesn't but if I execute this query a second time it inserts a second entry with the same testsuite_id.
What am I missing?
Then you don't have a PRIMARY or UNIQUE key on testsuite_id.
The "on duplicate key update" only changes the fields YOU specify in the 'update' portion. You're updating only the key field, which triggered the constraint violation in the first place. The other fields are silently dropped, so you're effective doing update yourtable set id=id where id=xxx.
You have to list each field you want to be updated:
INSERT INTO testsuite_dates (testsuite_id,start_date,end_date)
VALUES ('27798','2012:02:27 00:00:00','2012:02:28 00:00:00')
ON DUPLICATE KEY UPDATE
start_date=values(start_date), end_date=values(end_date)
Note the use of the values() function in the update portion. This allows you to refer to the NEW value that would've been inserted, and use it for the update portion. This saves you having to embed the same data twice in the same query. Not a big deal for a few dates or numbers, but if you're inserting large blobs, it makes for a HUGE query string size savings.
did you set your testsuite_id to be a primary key, or have unique constraint
INSERT INTO testsuite_dates (testsuite_id,start_date,end_date)
VALUES ('27798','2012:02:27 00:00:00','2012:02:28 00:00:00')
ON DUPLICATE KEY UPDATE testsuite_id=testsuite_id+1;