当前位置: 代码迷 >> 其他数据库 >> 触发器的有关问题
  详细解决方案

触发器的有关问题

热度:7655   发布时间:2013-02-26 00:00:00.0
触发器的问题
如何建立一个触发器,让它操作另一个数据库中的表啊 欢迎大家给提意见 谢谢

------解决方案--------------------------------------------------------
一般来说帮助手册中都有这种例子。

SQL code
CREATE TABLE test1(a1 INT);CREATE TABLE test2(a2 INT);CREATE TABLE test3(a3 INT NOT NULL AUTO_INCREMENT PRIMARY KEY);CREATE TABLE test4(  a4 INT NOT NULL AUTO_INCREMENT PRIMARY KEY,   b4 INT DEFAULT 0);delimiter |CREATE TRIGGER testref BEFORE INSERT ON test1  FOR EACH ROW BEGIN    INSERT INTO test2 SET a2 = NEW.a1;    DELETE FROM test3 WHERE a3 = NEW.a1;      UPDATE test4 SET b4 = b4 + 1 WHERE a4 = NEW.a1;  END;|delimiter ;INSERT INTO test3 (a3) VALUES   (NULL), (NULL), (NULL), (NULL), (NULL),   (NULL), (NULL), (NULL), (NULL), (NULL);INSERT INTO test4 (a4) VALUES   (0), (0), (0), (0), (0), (0), (0), (0), (0), (0);Suppose that you insert the following values into table test1 as shown here: mysql> INSERT INTO test1 VALUES     -> (1), (3), (1), (7), (1), (8), (4), (4);Query OK, 8 rows affected (0.01 sec)Records: 8  Duplicates: 0  Warnings: 0As a result, the data in the four tables will be as follows: mysql> SELECT * FROM test1;+------+| a1   |+------+|    1 ||    3 ||    1 ||    7 ||    1 ||    8 ||    4 ||    4 |+------+8 rows in set (0.00 sec)mysql> SELECT * FROM test2;+------+| a2   |+------+|    1 ||    3 ||    1 ||    7 ||    1 ||    8 ||    4 ||    4 |+------+8 rows in set (0.00 sec)mysql> SELECT * FROM test3;+----+| a3 |+----+|  2 ||  5 ||  6 ||  9 || 10 |+----+5 rows in set (0.00 sec)mysql> SELECT * FROM test4;+----+------+| a4 | b4   |+----+------+|  1 |    3 ||  2 |    0 ||  3 |    1 ||  4 |    2 ||  5 |    0 ||  6 |    0 ||  7 |    1 ||  8 |    1 ||  9 |    0 || 10 |    0 |+----+------+10 rows in set (0.00 sec)
  相关解决方案