query to Update another table if first update fails in mysql - mysql

I have a .sql file with some update queries.
What i am looking for is when an update query fails, i should be able to execute another update query
e.g. Update table1 set col1="zbc" where id=1;
suppose above query fails as "id=1" was not found.
Then i want to execute below query to do update on another table:
Update table2 set col1="zbc" where id=1;
Since i want this to go in .sql file, I am looking for a single query to achieve this using if/case statement or something similar.

What if just run the second query and check if these rows exist in the first table for example:
Update table1 set col1="zbc" where id=1;
Update table2 set col1="zbc" where id=1
and not exists (select id from table1 where id=1);
or use ROW_COUNT() MySql function and IF. It returns the number of rows changed, deleted, or inserted by the last statement.

Can you use a stored procedure and call that procedure from the .sql file contents? The MySQL manual states that if/then clauses are supported only in "stored programs":
http://dev.mysql.com/doc/refman/5.5/en/if.html

Related

mysql insert ... returning [duplicate]

I am a complete newbie to MySql so please be gentle.
Is there an equivalent of the RETURNING clause in Oracle or the Inserted'/'Deleted tables in SQL Server in MySQL? What I want to do is the following:
Delete a set of rows from table A
Insert the deleted set of rows into table B.
Please help!
Thanks
Unfortunately, you can't do both insertion and deletion in one query, but you can do it all in one transaction if you are using a transactional store engine (like InnoDB). Moreover, RETURNING is supported by Oracle and PostgreSQL but not by MySQL and therefore you need to write separate delete and insert statements.
Using a transaction however, will guarantee that only the successfully copied data will be deleted from tableA. Consider the following:
begin transaction;
insert into tableB select * from tableA where 'your_condition_here';
delete from tableA where 'your_condition_here';
commit;
Why not insert the rows to be deleted from table A in table B and then delete the rows from table A? you can achieve that like this:
insert into tableB select * from tableA where condition;
and then
delete from tableA where condition.

Update and select using single query in mysql

I want to update the columns and select the values using single query in MySQL
For example
update table set address='bangalore',updated_count =updated_count+1 where id=1
select * from table where id=1
This is not possible. As #D-Shih mentioned, you cannot do both update and select in the same query. The SQL update convention doesn't allow for return data and the select statement can't write to a table.
Each has its own purpose and can't be written in one statement. They must be executed separately.

Check if record exists delete it using mysql

i'm using MySQL and i want to check if a record exists and if it exists delete this record.
i try this but it 's not working for me:
SELECT 'Barcelone' AS City, EXISTS(SELECT 1 FROM mytable WHERE City = 'Barcelone') AS 'exists';
THEN
DELETE FROM mytable
WHERE City = 'Barcelone';
Thank you for your help.
The if statement is only allowed in stored procedures, stored functions, and triggers (in MySQL).
If I understand what you want, just do:
DELETE FROM mytable
WHERE City = 'Barcelone';
There is no reason to check for the existence beforehand. Just delete the row. If none exist, no problem. No errors.
I would recommend an index on mytable(city) for performance reasons. If you want to check if the row exists first, that is fine, but it is unnecessary for the delete.
If you mean MySQL is returning an error message (if that's what you mean by "not working for me"), then that's exactly the behavior we would expect.
That SQL syntax is not valid for MySQL.
If you want to delete rows from a table, issue a DELETE statement, e.g.
DELETE FROM mytable WHERE City = 'Barcelone'
If you want to know how many rows were deleted (if the statement doesn't throw an error), immediately follow the DELETE statement (in the same session) with a query:
SELECT ROW_COUNT()
Or the appropriate function in whatever client library you are using.
If the ROW_COUNT() function returns 0, then there were no rows deleted.
There's really no point (in terms of MySQL) in issuing a SELECT to find out if there are rows to be deleted; the DELETE statement itself will figure it out.
If for some reason your use case requires you to check whether there are rows be be deleted, then just run a separate SELECT:
SELECT COUNT(1) FROM mytable WHERE City = 'Barcelone'

MySQL multiple statement execution order

I have created a routine which inserts a record in one table, and after that it searches for that id (with a select statement) and updates another table's field with that id.Is this possible? It's one routine so my question is if the statements are executed in a sequential order?
Thanks in advance
You do not need to search for the id. You can use LAST_INSERT_ID() to get the id of the last inserted row.
INSERT INTO tablename
(<columns>)
VALUES
(<columnvalues>);
SELECT LAST_INSERT_ID() INTO somevariable;
Then you can write your UPDATE statement.
UPDATE sometable
SET sometable.col = somevariable
WHERE sometable.something = #something
Statements in a routine are executed in the order they are written.

Does mysql UPDATE inserts the values if it doesn't exist?

UPDATE items SET name = 'haha' WHERE id = '12'
I'm curious if update also inserts the values if the where condition fails. I've read on w3schools that update only updates existing data on the database but on my script it's automatically inserting rows with the data. I am wondering if it might be a bug in the script or that's just how UPDATE works on mysql.
No. If, in your example, there's no entry with id = 12 in the database, the query will return "no rows affected". An update will never create a new entry in MySQL.
EDIT: although update won't create a new entry, it may include default/automatic values set up in your database schema (current timestamp, for instance).
NO. Update does not insert a value if the value doesn't exist in table. Please check if the script checks if the status of the update and makes another call to DB to insert the data.
Your SQL should do the following -
Update all records in the items table that have an id of 12 by setting their name to 'haha'
Update won't insert records if they don't exist, it will only update existing records in the table.
Short answer: No.
Long Answer: If your column doesn't exist you will get an error. If your where condition column doesn't exist you get error too. If your where condition value doesn't exist, it do nothing.
I use a temp table to review the data update conditions, you can refer
UPDATE table1
SET
column1 = 'things'
WHERE
IDcolumn = 'id' AND
(NOT EXISTS (SELECT * FROM (SELECT * FROM table1) AS temp WHERE temp.column1 = N'things'))