触发从一个数据库到另一个数据库的更新

时间:2018-09-12 10:22:58

标签: mysql triggers

我有两个不同的数据库DB1和DB2。每个表分别具有两个不同的表,表A和表B。因此,如果万一在表B中产生了新行,我想触发触发器,从而在表B中产生新行的同时为我提供表A中的日期和时间。现在,我创建了一个触发器,如下所示,但它可以正常工作。

DELIMITER $$

USE `DB2`$$

DROP TRIGGER /*!50032 IF EXISTS */ `trigger_update_date_time_table_A`$$

CREATE
    /*!50017 DEFINER = 'root'@'%' */
    TRIGGER `trigger_update_date_time_tableA` AFTER INSERT ON table_B 
    FOR EACH ROW 
    BEGIN
    UPDATE DB1.table_A SET Date = CURRENT_TIMESTAMP WHERE id = NEW.id 
    END;
$$

DELIMITER ;

1 个答案:

答案 0 :(得分:1)

您将不得不更具体地说明为什么您认为触发器没有产生预期结果的原因。这是如何执行此操作的示例。顺便说一句,如果您按此方式进行操作,您将看到触发器按编码方式工作。

MariaDB [SANDBOX]> USE SANDBOX;
Database changed
MariaDB [SANDBOX]> SHOW CREATE TABLE T;
+-------+---------------------------------------------------------------------------------------+
| Table | Create Table                                                                          |
+-------+---------------------------------------------------------------------------------------+
| T     | CREATE TABLE `t` (
  `ID` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+---------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

MariaDB [SANDBOX]> USE TEST;
Database changed
MariaDB [TEST]> SHOW CREATE TABLE TEST.T;
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                                                                                                                                                                   |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| T     | CREATE TABLE `t` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `price` int(11) DEFAULT NULL,
  `tax` int(11) DEFAULT NULL,
  `pricetax` int(11) AS (price + price *tax/100) VIRTUAL,
  `DATE` date DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1000 DEFAULT CHARSET=latin1 |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

MariaDB [TEST]> USE SANDBOX;
Database changed
MariaDB [SANDBOX]> SHOW CREATE TRIGGER T;
+---------+--------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+--------------------+
| Trigger | sql_mode                                   | SQL Original Statement                                                                                                                                         | character_set_client | collation_connection | Database Collation |
+---------+--------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+--------------------+
| T       | STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION | CREATE DEFINER=`root`@`localhost` TRIGGER T AFTER INSERT ON T
FOR EACH ROW
BEGIN
     UPDATE TEST.T SET Date = CURRENT_TIMESTAMP WHERE id = NEW.id ;
END | utf8mb4              | utf8mb4_general_ci   | latin1_swedish_ci  |
+---------+--------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+--------------------+
1 row in set (0.00 sec)

MariaDB [SANDBOX]> TRUNCATE TABLE T;
Query OK, 0 rows affected (0.22 sec)

MariaDB [SANDBOX]> SELECT * FROM T;
Empty set (0.00 sec)

MariaDB [SANDBOX]> UPDATE TEST.T SET DATE = NULL WHERE ID = 999;
Query OK, 1 row affected (0.02 sec)
Rows matched: 1  Changed: 1  Warnings: 0

MariaDB [SANDBOX]> SELECT ID,DATE FROM TEST.T;
+-----+------+
| ID  | DATE |
+-----+------+
|   1 | NULL |
| 999 | NULL |
+-----+------+
2 rows in set (0.00 sec)

MariaDB [SANDBOX]> INSERT INTO T (ID) VALUES (999);
Query OK, 1 row affected (0.02 sec)

MariaDB [SANDBOX]> SELECT ID,DATE FROM TEST.T;
+-----+------------+
| ID  | DATE       |
+-----+------------+
|   1 | NULL       |
| 999 | 2018-09-13 |
+-----+------------+
2 rows in set (0.00 sec)