Mysql自动插入新表

时间:2015-11-16 06:35:33

标签: php mysql

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

上有这个
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 ;

目前我正在处理一个非我开发的项目,而且我对mysql并不熟悉。

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

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

2 个答案:

答案 0 :(得分:2)

是的,你是对的。 基于对某些Trigger操作(如插入,更新或删除)执行的操作,INSERT用于某些UPDATE TABLE TABLE。 请参阅MySQL triggers

答案 1 :(得分:1)

试试这个..

CREATE TRIGGER trigger_name
BEFORE INSERT
   ON table_name FOR EACH ROW

BEGIN

   -- variable declarations

   -- trigger code

END;

参数或参数

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.

<强>限制

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

示例:

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 ;

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