-- 触发器创建和使用
-- 需求:生产库的表只保存1天24小时,晚上12点必须是空表用于新的业务
-- 生产表晚上12点自动清除数据,清空之前把数据库转换到另一个临时表。
create table itpux12_history as select * from itpux12 limit 0;
select * from itpux12_history;
delimiter $$
create trigger trg_itpux12_history after
delete on itpux12
for each row
begin
insert into itpux12_history(name,age) values(old.name,old.age);
end
$$
delimiter ;
drop trigger trg_itpux12_history;
-- old /new 关键字,其中old用于after时刻,而new用于before时刻。
-- insert 操作一般使用new 关键字。
-- update 操作一般使用new和old关键字
-- delete操作一般使用old关键字
select * from itpux12;
delete from itpux12 where name = 'itpux11999';
commit;