Mysql returning clause equivalent

后端 未结 2 1131
一生所求
一生所求 2020-12-07 01:21

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

相关标签:
2条回答
  • 2020-12-07 01:30

    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;
    
    0 讨论(0)
  • 2020-12-07 01:36

    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. 
    
    0 讨论(0)
提交回复
热议问题