我有两个不同的数据库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 ;
您将需要更具体地了解为什么您认为触发器不会通过示例产生预期结果。以下是您如何做到这一点的例子。如果您按照这个方式工作,您将看到触发器按编码方式工作。
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)