dongxiz5342 2015-11-16 06:35 采纳率: 100%
浏览 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.

  • 写回答

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?