2

I am trying to insert multiple rows at the same time and check if they are exist. I want to update if they are exist, otherwise insert.

My primary key is "ID". I don't want to use unique index for "query" column.

What I want to insert/update :

INSERT INTO my_table (query, keyword) VALUES ('home', 'home-link');
INSERT INTO my_table (query, keyword) VALUES ('contact', 'contact-link');

my table:

--------------------------------
ID |  query   |  keyword       |
--------------------------------
1  |  home    |  home-link     |
--------------------------------
2  |  contact |  contact-link  |
--------------------------------

I have tried that but it gives me SQL syntax error #1064:

IF EXISTS(SELECT query FROM my_table WHERE query='home')
THEN
UPDATE my_table SET query='home' AND keyword='home-link' WHERE query='home'
ELSE
INSERT INTO my_table (query, keyword) VALUES ('home', 'home-link')
END IF


IF EXISTS(SELECT query FROM my_table WHERE query='contact')
THEN
UPDATE my_table SET query='contact' AND keyword='contact-link' WHERE query='contact'
ELSE
INSERT INTO my_table (query, keyword) VALUES ('contact', 'contact-link')
END IF
1
  • Why is it not possible to add a primary key or at least a unique key, so that you can use insert ... on duplicate key update ...? Commented Jan 29, 2016 at 14:00

4 Answers 4

2

I have decided to delete the record and then insert the new one

DELETE FROM my_table WHERE query = 'home';
INSERT INTO my_table (query, keyword) VALUES ('home', 'home-link');
Sign up to request clarification or add additional context in comments.

Comments

0

Assuming you have a unique index on the query column in your table, you can do an upsert like this:

insert into my_table (query, keyword) values ('home', 'home-link')
on duplicate key update 
keyword = values(keyword)

More info: http://mechanics.flite.com/blog/2013/09/30/how-to-do-an-upsert-in-mysql/

7 Comments

Unfortunately I have primary key index for ID (auto increment)
You can still add a unique index on the query column, assuming you want to enforce uniqueness on that column.
Certainly I want to make the query unique and update if it exists but since I am going to use it for opencart, I don't want to mess up the table. Is it possible to do it without adding unique index?
@Dejavu no, on duplicate key update works with primary key or unique index only.
What about with IF ELSE statement ?
|
0

If you don't add a unique index to the query column, then you can't do an upsert using only SQL. The conditional syntax you tried is not supported.

You could write a stored procedure to do this, or use a scripting language (bash, python, etc).

Another option is to install common_schema and use the conditional logic in QueryScript. Here's an example using common_schema QueryScript:

call common_schema.run("
  if (
  select count(*)=0 
  from test.my_table 
  where query='home'
  )
  {
    insert into test.my_table (query, keyword) values ('home', 'home-link');
  }
  else
  {
    update test.my_table set keyword='home-link' where query='home';
  }
");

1 Comment

Ok now I see how it works. I think first I'll delete the row then insert the new one :)
0

Conceptually there is no problem with the approach of using an if-else statement for this. The error is caused by the if-else syntax used in the provided SQL.

The correct if-else syntax would be (Note the different use of keywords):

IF (Expression )
BEGIN
  -- If the condition is TRUE then execute the following statement
  True Statements;
END
 
ELSE
BEGIN
  -- If the condition is False then execute the following statement
  False Statements;
END

Code from: https://www.sqlshack.com/sql-if-statement-introduction-and-overview/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.