scene description
In a production environment, it is sometimes necessary to update and modify the database directly. In order to prevent data loss caused by abnormal data operations, most of the time, backup is performed first and then the operation is performed. If the operation fails, the backup is rolled back.
Table to modify: qrtz_triggers
Temporary backup table: qrtz_triggers_20221031
Modification operation: Delete a row of data in qrtz_triggers.
Operation demonstration
Application backup
use demo;
create table qrtz_triggers_20221031 as select * from qrtz_triggers;
Modify operations
delete from qrtz_fired_triggers where SCHED_NAME='clusteredScheduler' and ENTRY_ID='job-scheduler-7b9f7bcd78-8b9kt16669688543581666968854330';
Rollback on failure
use refidb;
replace into qrtz_fired_triggers select * from qrtz_fired_triggers_20221031;
replace into
What is the difference between and insert into
?
replace into
It will be judged whether the record exists based on the primary key or unique index. If it exists, the row of data will be deleted first, and then new data will be inserted. If it does not exist, new data will be inserted directly. Therefore, when using replace into, it should be noted that there must be a primary key or unique index in the table, otherwise it will be inserted directly, resulting in data duplication.
insert into
Direct insertion will fail if the primary key or unique index already exists.
A long-winded sentence
The above operation is just a demo demonstration. In a real production environment, there may be a lot of data in the table, and it is unrealistic to operate the entire table. When backing up and modifying, you need to add the where condition to determine which block to modify and which block to back up.