ON DUPLICATE KEY unrecognized in phpmyadmin - mysql

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`);

Related

Update or Insert mysql column

I have simple mySql question similar to SO "Update mysql column"
I have a table table1 with at least two columns columns and a bunch of rows:
[key_col|col_a|col_other]
I want to update one column col_a if key_col=number exist or insert if not exists. I first must do select or update do insert automatically?
See this answer: Insert into a MySQL table or update if exists
What you said: "I want to update if key exists or insert if not exists" is equivalent to the opposite order: "insert if not exists or update if exists", because the 2 cases are mutually exclusive.
First make sure that key_col has a unique index (like: primary key). Then this would work:
insert into
`table1`
(`key_col`, `col_a`, ...)
values
(123, 234, ...)
on duplicate key update
`col_a` = 234, ...;
At "..." place the other fields.
if key_col is a primary key then do a replace into instead of doing insert into.....replace into will insert if the key does not exist else update if the key exists.

MySQL ON DUPLICATE KEY UPDATE does not update but inserts

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

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

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 'UPDATE ON DUPLICATE KEY' without a unique column?

What are peoples' thoughts on the most performance efficient way to do the following query:
3 column table
if the combination of col_1 and col_2 values already exist UPDATE col_3
else INSERT new row
I assume i need some kind if UPDATE ON DUPLICATE KEY (which i've never used before), however I do not have a 'KEY' but instead a pair of two values (columns) to make a key...
You can create a PRIMARY or UNIQUE key out of multiple columns (called a composite key) in MySQL, which'll allow ON DUPLICATE KEY to work just fine.
// create a composite index
CREATE INDEX my_composite_index ON my_table (column1, column2);
// insert or update
INSERT INTO my_table (column1, column2) VALUES ('value1', 'value2') ON DUPLICATE KEY UPDATE column3=column3+1;
Most efficient way is to create UNIQUE KEY and use ON DUPLICATE KEY UPDATE.
Slower way is to:
LOCK TABLE
SELECT TABLE (you need an index anyway for the best performance)
if exists, UPDATE
else INSERT
UNLOCK TABLES
Edit: Ignore my suggestions
You can use a composite key as ceejayoz said, however I think you need REPLACE INTO instead of UPDATE ON DUPLICATE KEY because REPLACE also inserts if no duplicate is found.
Note: I don't know the workings of UPDATE ON DUPLICATE KEY but it sounds like it doesn't perform inserts.