Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to move a record from one table to another using a single SQL statement?

Tags:

sql

mysql

I need a query to move a record from one table to another without using multiple statements?

like image 837
Palani Avatar asked Nov 27 '22 05:11

Palani


1 Answers

No, you cannot move records in one SQL statement. You have to use an INSERT followed by a DELETE statement. You should wrap these statements into a transaction, to make sure that the copy operation remains atomic.

START TRANSACTION;

INSERT INTO 
    new_table 
SELECT 
    *
FROM
    old_table
WHERE
    some_field = 'your_criteria';

DELETE FROM old_table WHERE some_field = 'your_criteria';

COMMIT;
like image 129
Daniel Vassallo Avatar answered Nov 29 '22 17:11

Daniel Vassallo