来自源文件的触发器和分隔符的Mysql问题

时间:2018-03-19 01:16:24

标签: mysql triggers delimiter

如果我使用如下所示的源命令,我会收到错误:

mysql> source /home/sqlws/workspace/src.sql;
Query OK, 0 rows affected, 1 warning (0.00 sec)
Query OK, 0 rows affected (0.01 sec)
Query OK, 0 rows affected, 1 warning (0.00 sec)
Query OK, 0 rows affected, 1 warning (0.00 sec)
Query OK, 0 rows affected, 1 warning (0.00 sec)
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'END' at line 1
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'TRIGGER trigg_cust_u   INSERT INTO audit' at line 1ROW
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'TRIGGER trigg_cust_d   INSERT INTO audit' at line 1ROW

但如果我复制&将/home/sqlws/workspace/src.sql的内容粘贴到mysql-cli,就像魅力一样

mysql> DROP TABLE IF EXISTS audit_cust;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> CREATE TABLE audit_cust(audit_ts DATETIME DEFAULT CURRENT_TIMESTAMP, audit_id BIGINT(20) AUTO_INCREMENT, cust_id bigint(20),PRIMARY KEY (audit_id))ENGINE = InnoDB DEFAULT CHARSET=latin1;
Query OK, 0 rows affected (0.01 sec)

mysql> DROP TRIGGER IF EXISTS trigg_cust_i;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> DROP TRIGGER IF EXISTS trigg_cust_u;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> DROP TRIGGER IF EXISTS trigg_cust_d;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> 
mysql> DELIMITER //
mysql> 
mysql> CREATE TRIGGER trigg_cust_i AFTER INSERT ON cust FOR EACH ROW
    -> BEGIN
    ->   INSERT INTO audit_cust(audit_oprn, cust_id)  VALUES('INSERT', NEW.cust_id);
    -> END //
Query OK, 0 rows affected (0.00 sec)

mysql> 
mysql> CREATE TRIGGER trigg_cust_u AFTER UPDATE ON cust FOR EACH ROW
    -> BEGIN
    ->   INSERT INTO audit_cust(audit_oprn, cust_id) VALUES('UPDATE', NEW.cust_id);
    -> END //
Query OK, 0 rows affected (0.00 sec)

mysql> 
mysql> CREATE TRIGGER trigg_cust_d AFTER DELETE ON cust FOR EACH ROW
    -> BEGIN
    ->   INSERT INTO audit_cust(audit_oprn, cust_id) VALUES('DELETE', OLD.cust_id);
    -> END //
Query OK, 0 rows affected (0.01 sec)

mysql> 
mysql> DELIMITER ;

Mysql版本:

mysql> select version();
+-----------+
| version() |
+-----------+
| 5.7.21    |
+-----------+
1 row in set (0.00 sec)

主机版:

sqlws@tt49:$ lsb_release -a
Distributor ID: Ubuntu
Description:    Ubuntu 16.04.4 LTS
Release:    16.04
Codename:   xenial

有人可以帮我解决问题

0 个答案:

没有答案