dongxiz5342
2015-11-16 06:35
浏览 8
已采纳

Mysql自动插入新表

Can anyone explain me what below code actually do, I have this on my mysqlDB.sql exported file

DROP TRIGGER IF EXISTS `alertupdateevent`;
DELIMITER $$
CREATE TRIGGER `alertupdateevent` AFTER INSERT ON `RECOG`
 FOR EACH ROW BEGIN
INSERT INTO `mydb`.`ALERTUPDATES` (`FIRSTNAME`, `LASTNAME`, `CAMNAME`, `TIMESTAMP`, `RESERVE1`, `ALERTTYPE`, `IMGURL`) VALUES (NEW.FIRSTNAME, NEW.LASTNAME, NEW.DOB, NEW.TIMESTAMP, NEW.FACEID, 'RECOG' , NEW.FACEURL);
END
$$
DELIMITER ;

Currently I dealing a project which is not developed by me, and I am not well familiarized with mysql.

The problem I am facing is I have two table in DB like RECOG and ALERTUPDATES and I need to insert data to both of these table(same data), and I can see only one php which insert data to the table `RECOG'.

So my question does the above piece of code insert data automatically to table ALERTUPDATES when data insert on RECOG table by php.

图片转代码服务由CSDN问答提供 功能建议

任何人都可以解释下面的代码实际做了什么,我的 mysqlDB.sql 导出文件

  DROP TRIGGER如果存在`alertupdateevent`; 
DELIMITER $$ 
创建TRIGGER`warkupdateevent`插入`RECOG` 
为每行开始
INSERT INTO  `mydb` .ALERTUPDATES`(`FIRSTNAME`,`LASTNAME`,`CAMNAME`,`TIMESTAMP`,`RESERVE1`,`ALERTTYPE`,`IMGURL`)VALUES(NEW.FIRSTNAME,NEW.LASTNAME,NEW.DOB,  NEW.TIMESTAMP,NEW.FACEID,'RECOG',NEW.FACEURL); 
END 
 $$ 
DELIMITER; 
   
 
 

目前我处理的项目是 不是我开发的,我对mysql不是很熟悉。

我面临的问题是我在DB中有两个表,如 RECOG ALERTUPDATES 我需要在这两个表中插入数据(相同的数据),我只能看到一个php将数据插入表`RECOG'。

所以 我的问题是上面的代码自动插入数据 当数据通过php插入 RECOG 表时,表 ALERTUPDATES

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douman9420 2015-11-16 06:48
    已采纳

    Try this..

    CREATE TRIGGER trigger_name
    BEFORE INSERT
       ON table_name FOR EACH ROW
    
    BEGIN
    
       -- variable declarations
    
       -- trigger code
    
    END;
    

    Parameters or Arguments

    trigger_name
    
    The name of the trigger to create.
    
    BEFORE INSERT
    
    It indicates that the trigger will fire before the INSERT operation is executed.
    
    table_name
    
    The name of the table that the trigger is created on.
    

    RESTRICTIONS

    You can not create a BEFORE trigger on a view.
    You can update the NEW values.
    You can not update the OLD values.
    

    Example:

    DELIMITER //
    
    CREATE TRIGGER contacts_before_insert
    BEFORE INSERT
       ON contacts FOR EACH ROW
    
    BEGIN
    
       DECLARE vUser varchar(50);
    
       -- Find username of person performing INSERT into table
       SELECT USER() INTO vUser;
    
       -- Update create_date field to current system date
       SET NEW.created_date = SYSDATE();
    
       -- Update created_by field to the username of the person performing the INSERT
       SET NEW.created_by = vUser;
    
    END; //
    
    DELIMITER ;
    

    Ref:http://www.techonthenet.com/mysql/triggers/before_insert.php

    点赞 评论
  • doukan5332 2015-11-16 06:42

    Yes, you are correct. Trigger are used to INSERT UPDATE on some TABLE based on action perform on some TABLE actions like insert, update or delete. Refer MySQL triggers

    点赞 评论

相关推荐 更多相似问题