想監(jiān)聽一張表里,A表內(nèi)容有更新的話就將向B表插入數(shù)據(jù),我結(jié)合了觸發(fā)器和存儲過程一起完成,下面是觸發(fā)器的示例代碼:
-- 創(chuàng)建賬戶表account
create table account(
id int primary key auto_increment,
name varchar(20),
money double
);
-- 創(chuàng)建日志表account_log
create table account_log(
id int(11) primary key auto_increment, -- 日志id
operation varchar(20), -- 操作類型(insert/update/delete)
operation_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP, -- 操作時間
operation_id int, -- 操作表的ID
operation_params varchar(500) -- 操作參數(shù)
);
下面是創(chuàng)建觸發(fā)器代碼:
-- 監(jiān)聽insert
delimiter $ -- 聲明結(jié)束符為$
create trigger account_insert
after insert -- 插入操作之后
on account -- 當account表被插入數(shù)據(jù)之后
for each row -- 行級觸發(fā)器
begin
-- 觸發(fā)器功能: 往account_log添加一條日志: 記錄插入操作的信息
-- new 關(guān)鍵字為新增的一條數(shù)據(jù)
-- new.id 表示插入到account表之后的id
-- 信息: 插入后(id=?,name=?,money=?)
insert into account_log
values(
null, -- id
'insert', -- operation
now(), -- operation_time
new.id, -- operation_id
concat('插入后(id=',new.id,',name=',new.name,',money=',new.money) -- operation_params
);
end$
delimiter ; -- 聲明結(jié)束符為;
-- 監(jiān)聽update
-- 創(chuàng)建 update 型觸發(fā)器
delimiter $ -- 聲明結(jié)束符 $
create trigger account_update -- 創(chuàng)建觸發(fā)器 account_update
after update -- 在 update 操作之后觸發(fā)
on account -- 監(jiān)聽 account 表
for each row -- 行級觸發(fā)器
begin
-- 往account_log寫入日志信息
-- old關(guān)鍵字:update之前的數(shù)據(jù);new關(guān)鍵字:update之后的數(shù)據(jù)
insert into account_log
values(
null, -- id
'update', -- operation
now(), -- operation_time
new.id, -- operation_id
concat( '修改前(id=',old.id,',name=',old.name,',money=',old.money,')',
'修改后(id=',new.id,',name=',new.name,',money=',new.money,')')); -- operation_params
end$
delimiter ; -- 聲明結(jié)束符 ;
-- 創(chuàng)建 delete 型的觸發(fā)器 , 完成刪除數(shù)據(jù)時的日志記錄
delimiter $ -- 聲明結(jié)束符 $
create trigger account_delete -- 創(chuàng)建觸發(fā)器 account_delete
after delete -- 在delete操作后觸發(fā)
on account -- 監(jiān)聽 account 表
for each row -- 行級觸發(fā)器
begin
-- 往account_log寫入日志信息
insert into account_log
values(
null, -- id
'delete', -- operation
now(), -- operation_time
old.id, -- operation_id
concat('刪除前(id=',old.id,',name=',old.name,',money=',old.money,')')); -- operation_params
end$
delimiter ; -- 聲明結(jié)束符 ;
存儲過程示例代:
-- 創(chuàng)建插入數(shù)據(jù)存儲過程
CREATE PROCEDURE insert_data()
BEGIN
INSERT INTO user_info (which_date,name,money)
SELECT CURDATE(),name,money from account;
END
-- 創(chuàng)建刪除數(shù)據(jù)存儲過程
CREATE PROCEDURE delete_curdatedata()
BEGIN
delete from user_info where which_date=CURDATE();
END
回到最開始需求,監(jiān)聽account表有更新內(nèi)容,則插入數(shù)據(jù)到user_info
-- 監(jiān)聽update
-- 創(chuàng)建 update 型觸發(fā)器
delimiter $ -- 聲明結(jié)束符 $
create trigger account_update -- 創(chuàng)建觸發(fā)器 account_update
after update -- 在 update 操作之后觸發(fā)
on account -- 監(jiān)聽 account 表
for each row -- 行級觸發(fā)器
begin
-- 往account_log寫入日志信息
-- old關(guān)鍵字:update之前的數(shù)據(jù);new關(guān)鍵字:update之后的數(shù)據(jù)
insert into account_log
values(
null, -- id
'update', -- operation
now(), -- operation_time
new.id, -- operation_id
concat( '修改前(id=',old.id,',name=',old.name,',money=',old.money,')',
'修改后(id=',new.id,',name=',new.name,',money=',new.money,')')); -- operation_params
CALL delete_curdatedata();
CALL insert_data();
end$
delimiter ; -- 聲明結(jié)束符 ;
參考文檔https://blog.csdn.net/KKAZIQA/article/details/118027103
https://blog.51cto.com/u_16213417/12064613