Mysql TRIGGER同步2个数据库相同的服务器

时间:2018-01-30 11:16:48

标签: mysql triggers

mysql 5.6.37 phpmyadmin 4.4.15.7 同一台服务器

TRIGGER在database_2中创建 它应该从database_1

中的更新同步database_2
DELIMITER |
USE database_2;
CREATE TRIGGER trgSync 
AFTER UPDATE ON database_1.table_1
FOR EACH ROW
BEGIN
UPDATE database_2.table_1 a INNER JOIN database_1.table_1 b USING(tb_id)
SET 
a.published = b.published;
END;

我得到了

1064 - 你的SQL语法错误...在第2行'更新后'创建TRIGGER trgSync'附近

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

除了关于如何使用分隔符的问号之外,如果您尝试将触发器停放在正确的架构中,则触发器很好。希望这段代码对您来说足够令人信服

MariaDB [sandbox]> use sandbox;
Database changed
MariaDB [sandbox]> drop trigger if exists trgsync;
Query OK, 0 rows affected (0.01 sec)

MariaDB [sandbox]> select table_schema,table_name from information_schema.tables where table_name = 'users' and table_schema in('sandbox','test');
+--------------+------------+
| table_schema | table_name |
+--------------+------------+
| sandbox      | users      |
| test         | users      |
+--------------+------------+
2 rows in set (0.00 sec)

MariaDB [sandbox]> select trigger_schema,trigger_name from information_schema.triggers where trigger_name = 'trgsync';
Empty set (0.04 sec)

MariaDB [sandbox]>
MariaDB [sandbox]> DELIMITER |
MariaDB [sandbox]> CREATE TRIGGER trgSync
    -> AFTER UPDATE ON sandbox.users
    -> FOR EACH ROW
    -> BEGIN
    -> UPDATE test.users a INNER JOIN sandbox.users b USING(id)
    -> SET
    -> a.name = b.name;
    -> END |
Query OK, 0 rows affected (0.02 sec)

MariaDB [sandbox]> delimiter ;
MariaDB [sandbox]>
MariaDB [sandbox]> select trigger_schema,trigger_name from information_schema.triggers where trigger_name = 'trgsync';
+----------------+--------------+
| trigger_schema | trigger_name |
+----------------+--------------+
| sandbox        | trgSync      |
+----------------+--------------+
1 row in set (0.03 sec)

MariaDB [sandbox]> select id,name from sandbox.users union all select id,name from test.users;
+----+------+
| id | name |
+----+------+
|  1 | aaa  |
|  2 | bbbb |
|  3 | zzz  |
|  1 | aaa  |
|  2 | bbbb |
|  3 | zzz  |
+----+------+
6 rows in set (0.00 sec)

MariaDB [sandbox]> update sandbox.users set name = 'aaaa' where id = 1;
Query OK, 1 row affected (0.06 sec)
Rows matched: 1  Changed: 1  Warnings: 0

MariaDB [sandbox]> select id,name from sandbox.users union all select id,name from test.users;
+----+------+
| id | name |
+----+------+
|  1 | aaaa |
|  2 | bbbb |
|  3 | zzz  |
|  1 | aaaa |
|  2 | bbbb |
|  3 | zzz  |
+----+------+
6 rows in set (0.00 sec)

如果您尝试将其停放在错误的架构中

MariaDB [sandbox]> use sandbox;
Database changed
MariaDB [sandbox]> drop trigger if exists trgsync;
Query OK, 0 rows affected (0.01 sec)

MariaDB [sandbox]> select table_schema,table_name from information_schema.tables where table_name = 'users' and table_schema in('sandbox','test');
+--------------+------------+
| table_schema | table_name |
+--------------+------------+
| sandbox      | users      |
| test         | users      |
+--------------+------------+
2 rows in set (0.00 sec)

MariaDB [sandbox]> select trigger_schema,trigger_name from information_schema.triggers where trigger_name = 'trgsync';
Empty set (0.05 sec)

MariaDB [sandbox]>
MariaDB [sandbox]> DELIMITER |
MariaDB [sandbox]> use test;
ERROR 1049 (42000): Unknown database 'test;'
MariaDB [sandbox]> CREATE TRIGGER trgSync
    -> AFTER UPDATE ON sandbox.users
    -> FOR EACH ROW
    -> BEGIN
    -> UPDATE test.users a INNER JOIN sandbox.users b USING(id)
    -> SET
    -> a.name = b.name;
    -> END |
Query OK, 0 rows affected (0.01 sec)

命令行返回一个未知的数据库错误,我的gui(heidisql)在错误的模式错误中返回一个触发器。