Home > Database > Mysql Tutorial > MySQL触发器运用于迁移和同步数据的实例教程_MySQL

MySQL触发器运用于迁移和同步数据的实例教程_MySQL

PHP中文网
Release: 2016-05-27 13:45:59
Original
1553 people have browsed it

1.迁移数据
进行数据库移植,SQL Server=>MySQL。SQL Server上有如下的Trigger

SET QUOTED_IDENTIFIER ON  
GO 
SET ANSI_NULLS ON  
GO 
ALTER TRIGGER [trg_risks] ON dbo.projectrisk 
FOR INSERT, UPDATE 
AS 
BEGIN 
UPDATE projectrisk 
  SET classification = 
  case   
  when calc>= 9 then 3 
  when calc=4 then 2 
  when calc<4 then 1 
  end  
  from (select inserted.id, inserted.possibility*inserted.severity as calc from inserted) as T1 
  where projectrisk.id = T1.id 
END 
GO 
SET QUOTED_IDENTIFIER OFF  
GO 
SET ANSI_NULLS ON  
GO
Copy after login

简单了解了下MySQL中,Trigger的语法。

# 创建 
CREATE TRIGGER  
{ BEFORE | AFTER } 
{ INSERT | UPDATE | DELETE } 
ON  
FOR EACH ROW 
 
 
# 删除 
DROP TRIGGER
Copy after login

注:创建触发器需要CREATE TRIGGER权限。(HeidiSQL中执行Trigger语句会有bug)

由于MySQL中的每个触发器只能针对一个动作,所以本次移植就需要创建两个触发器。对于发生变更的行,在触发器中可以用 NEW 来代替。
下边的触发器有什么问题吗?

delimiter && 
CREATE TRIGGER trg_risks_insert 
AFTER INSERT ON `projectrisk` 
FOR EACH ROW 
UPDATE projectrisk SET classification = CASE 
WHEN possibility*severity>=9 THEN 3 
WHEN possibility*severity=4 THEN 2 
WHEN possibility*severity=9 THEN 3 
WHEN possibility*severity=4 THEN 2 
WHEN possibility*severity<4 THEN 1 
END 
WHERE id = new.id; 
&& 
delimiter ;
Copy after login

问题就是,没有考虑到触发器中的修改也会触发触发器,进入了死循环。做了如下修改后,终于OK了。

delimiter && 
CREATE TRIGGER trg_risks_insert 
BEFORE INSERT ON `projectrisk` 
FOR EACH ROW 
BEGIN 
 SET new.classification = CASE 
 WHEN new.possibility*new.severity>=9 THEN 3 
 WHEN new.possibility*new.severity=4 THEN 2 
 WHEN new.possibility*new.severity=9 THEN 3 
 WHEN new.possibility*new.severity=4 THEN 2 
 WHEN new.possibility*new.severity<4 THEN 1 
 END; 
END 
&& 
delimiter ;
Copy after login

2.同步备份数据记录表
添加记录到新记录表

DELIMITER $$
USE `DB_Test`$$
CREATE
  /*!50017 DEFINER = &#39;root&#39;@&#39;%&#39; */
  TRIGGER `InsertOPM_Alarm_trigger` BEFORE INSERT ON `OPM_Alarm` 
  FOR EACH ROW BEGIN
INSERT INTO OPM_Alarm_copy (AlarmId,AlarmCode,AlarmTypeId,AlarmLevelId,AlarmObjectCode,AlarmStatus,
AlarmHandleUser,
AlarmHandleTime,ADDTIME,ParkUserId,BerthCode,BargainOrderCode,BerthStartTime)
VALUES(new.AlarmId,new.AlarmCode,new.AlarmTypeId,new.AlarmLevelId,new.AlarmObjectCode,new.AlarmStatus,
new.AlarmHandleUser,
new.AlarmHandleTime,new.ADDTIME,new.ParkUserId,new.BerthCode,new.BargainOrderCode,new.BerthStartTime);
  END;
$$
DELIMITER ;

CREATE TRIGGER InsertOPM_Alarm_trigger 
 BEFORE INSERT ON OPM_Alarm 
 FOR EACH ROW
BEGIN 
INSERT INTO OPM_Alarm_copy (AlarmId,AlarmCode,AlarmTypeId,AlarmLevelId,AlarmObjectCode,AlarmStatus,
AlarmHandleUser,
AlarmHandleTime,ADDTIME,ParkUserId,BerthCode,BargainOrderCode,BerthStartTime)
VALUES(new.AlarmId,new.AlarmCode,new.AlarmTypeId,new.AlarmLevelId,new.AlarmObjectCode,new.AlarmStatus,
new.AlarmHandleUser,
new.AlarmHandleTime,new.ADDTIME,new.ParkUserId,new.BerthCode,new.BargainOrderCode,new.BerthStartTime);
END ;
Copy after login

mysql触发器监控mysql数据表记录删除操作 DELIMITER $$

USE `DB_Test`$$

DROP TRIGGER /*!50032 IF EXISTS */ `SYS_OPM_trigger`$$

CREATE
  /*!50017 DEFINER = &#39;root&#39;@&#39;%&#39; */
  TRIGGER `SYS_OPM_trigger` AFTER DELETE ON `OPM_Alarm` 
  FOR EACH ROW BEGIN
  DECLARE str VARCHAR(40000);
   SET str=CONCAT(old.AlarmId,&#39;@&#39;,old.AlarmCode,&#39;@&#39;,old.AlarmTypeId,&#39;@&#39;,old.AlarmLevelId,&#39;@&#39;,
   old.AlarmObjectCode,&#39;@&#39;,old.AlarmStatus,&#39;@&#39;,old.AlarmHandleUser,&#39;@&#39;,old.AlarmHandleTime,&#39;@&#39;,
   old.AddTime,&#39;@&#39;,old.ParkUserId,&#39;@&#39;,old.BerthCode,&#39;@&#39;,old.BargainOrderCode,&#39;@&#39;,old.BerthStartTime);
   INSERT INTO OPM_AlarmAction_log(UserName,Client_IP,Delete_before_key,Delete_Date) 
  VALUES(SUBSTRING_INDEX(USER(),&#39;@&#39;,1),SUBSTRING_INDEX(USER(),&#39;@&#39;,-1), str, NOW());
  END;
$$


DELIMITER ;
Copy after login

删除前 添加原记录备份到另一记录表

DELIMITER $$

USE `DB_Test`$$

DROP TRIGGER /*!50032 IF EXISTS */ `InsertOPM_Alarm_trigger`$$

CREATE
  /*!50017 DEFINER = &#39;root&#39;@&#39;%&#39; */
  TRIGGER `InsertOPM_Alarm_trigger` BEFORE 

DELETE ON `OPM_Alarm` 
  FOR EACH ROW BEGIN
   INSERT INTO OPM_Alarm_copy 

(AlarmId,AlarmCode,AlarmTypeId,AlarmLevelId,AlarmObjectCode,AlarmStatus,AlarmHandleUser,
    AlarmHandleTime,ADDTIME,ParkUserId,BerthCode,BargainOrderCode,BerthStartTime)
     VALUES

(old.AlarmId,old.AlarmCode,old.AlarmTypeId,old.AlarmLevelId,old.AlarmObjectCode,old.AlarmS

tatus,old.AlarmHandleUser,
         

old.AlarmHandleTime,old.ADDTIME,old.ParkUserId,old.BerthCode,old.BargainOrderCode,old.Bert

hStartTime);
     

  END;
$$

DELIMITER ;
Copy after login


以上就是MySQL触发器运用于迁移和同步数据的实例教程_MySQL的内容,更多相关内容请关注PHP中文网(www.php.cn)!


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template