mysql insert ... returning [duplicate] - mysql

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.

Related

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.

MySQL Deadlock Issue

I am working on online trading project.
I insert one row on table t1 (innoDB engine) and there is one trigger on after insert event.
In trigger, i fetch data using select statement in same table like "SELECT * FROM t1 WHERE ID = NEW.ID".
It works perfectly 2-3 months. but now it gives Deadlock issue.
what is reason behind it ?
is it possible while multiple thread access same operation on same table at a time ?
do you want the id that you have inserted before ?
you can try something like this
INSERT INTO table1 (title,userid) VALUES ('test', 1);
SET #last_id_in_table1 = LAST_INSERT_ID();

query to Update another table if first update fails in 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

SQL query- Update if exists, insert otherwise

I need to write an SQL query for MySQL so that a row is updated if it exists, but inserted if it does not.
i.e.
If row exists...
UPDATE Table1 SET (...) WHERE Column1='SomeValue'
If it does not exist...
INSERT INTO Table1 VALUES (...)
Can this be done in one query?
i believe you need to reverse your logic in order for it to work:
insert into a table - if it exists (same key) then update it.
this can be achieved by the ON DUPLICATE statement like so:
INSERT INTO Table1 VALUES(...)
ON DUPLICATE KEY UPDATE column=column+1
check the manual here
Use the INSERT... ON DUPLICATE KEY UPDATE syntax.
See the manual
(For searching purposes, btw, this is usually referred to as an "upsert")

How to insert data only if data doesn't already exist in MYSQL?

This is what I'm doing right now:
Execute a query and check if the date
to be inserted already exists in a
table.
If date doesn't exist:
Another query will insert the date into the table.
How can these two query be combined?
IF NOT EXISTS (SELECT * FROM X WHERE A=B)
INSERT INTO ...
Taken from here:
INSERT IGNORE INTO Table (EmailAddr) VALUES ('test#test.com')
you can put the two statements together into a stored procedure