为此,语法如下-
DELIMITER // create trigger yourTriggerName before insert on yourTableName1 for each row begin insert into yourTableName2 values (yourValue1,yourValue2,...N); end ; // DELIMITER ;
让我们首先创建一个表-
mysql> create table DemoTable1 ( StudentId int, StudentName varchar(40) );
这是创建第二个表的查询-
mysql> create table DemoTable2( Id int, Name varchar(40) );
这是插入之前的触发器查询-
mysql> DELIMITER // mysql> create trigger afterinserttrigger before insert on DemoTable1 for each row begin insert into DemoTable2 values (10,'Chris'); end ; // mysql> DELIMITER ;
让我们将记录插入第一个表-
mysql> insert into DemoTable1 values(1,'David');
现在,您可以使用select语句显示第一个表中的所有记录-
mysql> select *from DemoTable1;
这将产生以下输出-
+-----------+-------------+ | StudentId | StudentName | +-----------+-------------+ | 1 | David | +-----------+-------------+ 1 row in set (0.00 sec)
让我们检查第二张表的表记录-
mysql> select *from DemoTable2;
这将产生以下输出-
+------+-------+ | Id | Name | +------+-------+ | 10 | Chris | +------+-------+ 1 row in set (0.00 sec)