一、MySQL触发器

  触发器是由MySQL的基本命令事件来触发某种特定操作,这些基本的命令由INSERT、UPDATE、DELETE等事件来触发某些特定操作。满足触发器的触发条件时,数据库系统就会自动执行触发器中定义的程序语句。这样可以令某些操作之间的一致性得到协调。

  1.创建MySQL触发器  

mysql> CREATE TABLE IF NOT EXISTS tb_booklog(
-> id int(11) PRIMARY KEY auto_increment NOT NULL,
-> event varchar(200) NOT NULL,
-> logtime timestamp NOT NULL DEFAULT current_timestamp
-> );
Query OK, 0 rows affected (0.03 sec) mysql> DELIMITER //
mysql> CREATE TRIGGER auto_save_log BEFORE INSERT
-> ON tb_bookinfo FOR EACH ROW
-> INSERT INTO tb_booklog(event,logtime) values('look at me',now());
-> //
Query OK, 0 rows affected (0.01 sec)
mysql> SELECT * FROM tb_bookinfo;
-> //
+----------+-----------+--------+-----------+------+-------+------+----------+------------+------+----+
| barcode | bookname | typeid | author | ISBN | price | page | bookcase | inTime | del | id |
+----------+-----------+--------+-----------+------+-------+------+----------+------------+------+----+
| 17120107 | Java King | 3 | LianJiang | 115 | 49.80 | 350 | 1 | 2018-04-17 | 0 | 1 |
| 17120108 | Lian | 1 | QiaoJiang | 116 | 50.00 | 351 | 2 | 2018-04-18 | 0 | 2 |
| 17120109 | Tian King | 2 | TianJiang | 117 | 51.10 | 352 | 3 | 2018-04-19 | 0 | 3 |
+----------+-----------+--------+-----------+------+-------+------+----------+------------+------+----+
3 rows in set (0.00 sec)
mysql> INSERT INTO tb_bookinfo
-> (barcode,bookname,typeid,author,ISBN,price,page,bookcase,inTime,del,id)
-> VALUES
-> ('17120110','ShenOba',4,'ShenJiang','118',52.15,300,4,'2018-04-20',0,4);
-> //
Query OK, 1 row affected (0.00 sec) mysql> SELECT * FROM tb_bookinfo;
-> //
+----------+-----------+--------+-----------+------+-------+------+----------+------------+------+----+
| barcode | bookname | typeid | author | ISBN | price | page | bookcase | inTime | del | id |
+----------+-----------+--------+-----------+------+-------+------+----------+------------+------+----+
| 17120107 | Java King | 3 | LianJiang | 115 | 49.80 | 350 | 1 | 2018-04-17 | 0 | 1 |
| 17120108 | Lian | 1 | QiaoJiang | 116 | 50.00 | 351 | 2 | 2018-04-18 | 0 | 2 |
| 17120109 | Tian King | 2 | TianJiang | 117 | 51.10 | 352 | 3 | 2018-04-19 | 0 | 3 |
| 17120110 | ShenOba | 4 | ShenJiang | 118 | 52.15 | 300 | 4 | 2018-04-20 | 0 | 4 |
+----------+-----------+--------+-----------+------+-------+------+----------+------------+------+----+
4 rows in set (0.00 sec) mysql> SELECT * FROM tb_booklog;
-> //
+----+------------+---------------------+
| id | event | logtime |
+----+------------+---------------------+
| 2 | look at me | 2018-04-18 19:40:02 |
+----+------------+---------------------+
1 row in set (0.00 sec)

  2.创建具有多个执行语句的触发器

mysql> CREATE DEFINER=`root`@`localhost` TRIGGER delete_book_info BEFORE DELETE
-> ON tb_bookinfo FOR EACH ROW
-> BEGIN
-> INSERT INTO tb_booklog(event,logtime) values('let me go',now());
-> INSERT INTO tb_bookinfobak SELECT * FROM tb_bookinfo where id=OLD.id;
-> END
-> //
Query OK, 0 rows affected (0.01 sec)

  血一般惨痛的教训!!!当删除表中某一条记录未能成功时,检查一下触发器什么的有没有INSERT INTO语句,如果有,检查插入值的个数和表能不能对应起来,这里的原因就是两个表不一致!!!

Microsoft Windows [版本 10.0.16299.371]
(c) 2017 Microsoft Corporation。保留所有权利。 C:\Users\BigJun>mysql -uroot -p
Enter password: *********
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 23
Server version: 5.7.20 MySQL Community Server (GPL) Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> DELIMITER //
mysql> CREATE DEFINER=`root`@`localhost` TRIGGER BEFORE DELETE
-> ON tb_bookinfo FOR EACH ROW
-> BEGIN
-> INSERT INTO tb_booklog(event,logtime) values('let me go'.now());
-> INSERT INTO tb_bookinfobak SELECT * FROM tb_bookinfo where id=OLD.id;
-> END
-> //
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 'BEFORE DELETE
ON tb_bookinfo FOR EACH ROW
BEGIN
INSERT INTO tb_booklog(event,log' at line 1
mysql> CREATE DEFINER=`root`@`localhost` TRIGGER delete_book_info BEFORE DELETE
-> ON tb_bookinfo FOR EACH ROW
-> BEGIN
-> INSERT INTO tb_booklog(event,logtime) values('let me go'.now());
-> INSERT INTO tb_bookinfobak SELECT * FROM tb_bookinfo where id=OLD.id;
-> END
-> //
ERROR 1046 (3D000): No database selected
mysql> use db_library;
ERROR 1049 (42000): Unknown database 'db_library;'
mysql> USE db_library;
ERROR 1049 (42000): Unknown database 'db_library;'
mysql> exit
Bye C:\Users\BigJun>mysql -uroot -p
Enter password: *********
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 24
Server version: 5.7.20 MySQL Community Server (GPL) Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| db_database13 |
| db_library |
| db_library1 |
| db_library_gbk |
| hotel |
| mybatis |
| mysql |
| mysqldb |
| performance_schema |
| se |
| spring |
| springapp |
| sys |
| test |
+--------------------+
15 rows in set (0.00 sec) mysql> use db_library;
Database changed
mysql> DELIMITER //
mysql> CREATE DEFINER=`root`@`localhost` TRIGGER delete_book_info BEFORE DELETE
-> ON tb_bookinfo FOR EACH ROW
-> BEGIN
-> INSERT INTO tb_booklog(event,logtime) values('let me go'.now());
->
-> INSERT INTO tb_bookinfobak SELECT * FROM tb_bookinfo where id=OLD.id;
-> END
-> //
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 '.now()); INSERT INTO tb_bookinfobak SELECT * FROM tb_bookinfo where id=OLD.id;
' at line 4
mysql> CREATE DEFINER=`root`@`localhost` TRIGGER delete_book_info BEFORE DELETE
-> ON tb_bookinfo FOR EACH ROW
-> BEGIN
-> INSERT INTO tb_booklog(event,logtime) values('let me go'.now());
-> INSERT INTO tb_bookinfobak SELECT * FROM tb_bookinfo where id=OLD.id;
-> END
-> //
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 '.now());
INSERT INTO tb_bookinfobak SELECT * FROM tb_bookinfo where id=OLD.id;
E' at line 4
mysql> CREATE DEFINER=`root`@`localhost` TRIGGER delete_book_info BEFORE DELETE
-> ON tb_bookinfo FOR EACH ROW
-> BEGIN
-> INSERT INTO tb_booklog(event,logtime) values('let me go',now());
-> INSERT INTO tb_bookinfobak SELECT * FROM tb_bookinfo where id=OLD.id;
-> END
-> //
Query OK, 0 rows affected (0.01 sec) mysql> select * from tb_bookinfo;
-> //
+----------+-----------+--------+-----------+------+-------+------+----------+------------+------+----+
| barcode | bookname | typeid | author | ISBN | price | page | bookcase | inTime | del | id |
+----------+-----------+--------+-----------+------+-------+------+----------+------------+------+----+
| 17120107 | Java King | 3 | LianJiang | 115 | 49.80 | 350 | 1 | 2018-04-17 | 0 | 1 |
| 17120108 | Lian | 1 | QiaoJiang | 116 | 50.00 | 351 | 2 | 2018-04-18 | 0 | 2 |
| 17120109 | Tian King | 2 | TianJiang | 117 | 51.10 | 352 | 3 | 2018-04-19 | 0 | 3 |
| 17120110 | ShenOba | 4 | ShenJiang | 118 | 52.15 | 300 | 4 | 2018-04-20 | 0 | 4 |
+----------+-----------+--------+-----------+------+-------+------+----------+------------+------+----+
4 rows in set (0.01 sec) mysql> select * from tb_booklog;
-> //
+----+------------+---------------------+
| id | event | logtime |
+----+------------+---------------------+
| 2 | look at me | 2018-04-18 19:40:02 |
+----+------------+---------------------+
1 row in set (0.00 sec) mysql> select * from tb_bookinfobak;
-> //
Empty set (0.00 sec) mysql> DELETE FROM tb_bookinfo where id=4;
-> //
ERROR 1136 (21S01): Column count doesn't match value count at row 1
mysql> DELETE FROM tb_bookinfo WHERE id=4;
-> //
ERROR 1136 (21S01): Column count doesn't match value count at row 1
mysql> DELETE FROM tb_bookinfo WHERE id=3;
-> //
ERROR 1136 (21S01): Column count doesn't match value count at row 1
mysql> DELETE FROM tb_bookinfo WHERE barcode='17120107';
-> //
ERROR 1136 (21S01): Column count doesn't match value count at row 1
mysql> show triggers;
-> //
+------------------+--------+-------------+--------------------------------------------------------------------------------------------------------------------------------------------------+--------+------------------------+-------------------------------------------------------------------------------------------------------------------------------------------+----------------+----------------------+----------------------+--------------------+
| Trigger | Event | Table | Statement | Timing | Created | sql_mode | Definer | character_set_client | collation_connection | Database Collation |
+------------------+--------+-------------+--------------------------------------------------------------------------------------------------------------------------------------------------+--------+------------------------+-------------------------------------------------------------------------------------------------------------------------------------------+----------------+----------------------+----------------------+--------------------+
| delete_book_info | DELETE | tb_bookinfo | BEGIN
INSERT INTO tb_booklog(event,logtime) values('let me go',now());
INSERT INTO tb_bookinfobak SELECT * FROM tb_bookinfo where id=OLD.id;
END | BEFORE | 2018-04-18 20:35:07.19 | ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION | root@localhost | gbk | gbk_chinese_ci | utf8_general_ci |
+------------------+--------+-------------+--------------------------------------------------------------------------------------------------------------------------------------------------+--------+------------------------+-------------------------------------------------------------------------------------------------------------------------------------------+----------------+----------------------+----------------------+--------------------+
1 row in set (0.00 sec) mysql> select * from tb_bookinfo;
-> //
+----------+-----------+--------+-----------+------+-------+------+----------+------------+------+----+
| barcode | bookname | typeid | author | ISBN | price | page | bookcase | inTime | del | id |
+----------+-----------+--------+-----------+------+-------+------+----------+------------+------+----+
| 17120107 | Java King | 3 | LianJiang | 115 | 49.80 | 350 | 1 | 2018-04-17 | 0 | 1 |
| 17120108 | Lian | 1 | QiaoJiang | 116 | 50.00 | 351 | 2 | 2018-04-18 | 0 | 2 |
| 17120109 | Tian King | 2 | TianJiang | 117 | 51.10 | 352 | 3 | 2018-04-19 | 0 | 3 |
| 17120110 | ShenOba | 4 | ShenJiang | 118 | 52.15 | 300 | 4 | 2018-04-20 | 0 | 4 |
+----------+-----------+--------+-----------+------+-------+------+----------+------------+------+----+
4 rows in set (0.00 sec) mysql> delete from tb_bookinfo where id=1;
-> //
ERROR 1136 (21S01): Column count doesn't match value count at row 1
mysql> desc tb_bookinfobak;
-> //
+------------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------------+------+-----+---------+-------+
| barcode | varchar(30) | YES | | NULL | |
| bookname | varchar(70) | YES | | NULL | |
| typeid | int(10) unsigned | YES | | NULL | |
| author | varchar(30) | YES | | NULL | |
| ISBN | varchar(20) | YES | | NULL | |
| price | float(8,2) | YES | | NULL | |
| page | int(10) unsigned | YES | | NULL | |
| bookcase | int(10) unsigned | YES | | NULL | |
| inTime | datetime(6) | YES | | NULL | |
| del | tinyint(1) | YES | | 0 | |
| id | int(11) | NO | | NULL | |
| translator | varchar(30) | NO | | NULL | |
+------------+------------------+------+-----+---------+-------+
12 rows in set (0.00 sec) mysql> alter table tb_bookinfobak drop transtor;
-> //
ERROR 1091 (42000): Can't DROP 'transtor'; check that column/key exists
mysql> alter table tb_bookinfobak drop translator;
-> //
Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0 mysql> desc tb_bookinfobak;
-> //
+----------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+------------------+------+-----+---------+-------+
| barcode | varchar(30) | YES | | NULL | |
| bookname | varchar(70) | YES | | NULL | |
| typeid | int(10) unsigned | YES | | NULL | |
| author | varchar(30) | YES | | NULL | |
| ISBN | varchar(20) | YES | | NULL | |
| price | float(8,2) | YES | | NULL | |
| page | int(10) unsigned | YES | | NULL | |
| bookcase | int(10) unsigned | YES | | NULL | |
| inTime | datetime(6) | YES | | NULL | |
| del | tinyint(1) | YES | | 0 | |
| id | int(11) | NO | | NULL | |
+----------+------------------+------+-----+---------+-------+
11 rows in set (0.00 sec) mysql> desc tb_bookinfo;
-> //
+----------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+------------------+------+-----+---------+-------+
| barcode | varchar(30) | YES | | NULL | |
| bookname | varchar(70) | YES | | NULL | |
| typeid | int(10) unsigned | YES | | NULL | |
| author | varchar(30) | YES | | NULL | |
| ISBN | varchar(20) | YES | | NULL | |
| price | float(8,2) | YES | | NULL | |
| page | int(10) unsigned | YES | | NULL | |
| bookcase | int(10) unsigned | YES | | NULL | |
| inTime | date | YES | | NULL | |
| del | tinyint(1) | YES | | 0 | |
| id | int(11) | NO | PRI | NULL | |
+----------+------------------+------+-----+---------+-------+
11 rows in set (0.00 sec) mysql> alter table tb_bookinfobak modify inTime date;
-> //
Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0 mysql> desc tb_bookinfobak;
-> //
+----------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+------------------+------+-----+---------+-------+
| barcode | varchar(30) | YES | | NULL | |
| bookname | varchar(70) | YES | | NULL | |
| typeid | int(10) unsigned | YES | | NULL | |
| author | varchar(30) | YES | | NULL | |
| ISBN | varchar(20) | YES | | NULL | |
| price | float(8,2) | YES | | NULL | |
| page | int(10) unsigned | YES | | NULL | |
| bookcase | int(10) unsigned | YES | | NULL | |
| inTime | date | YES | | NULL | |
| del | tinyint(1) | YES | | 0 | |
| id | int(11) | NO | | NULL | |
+----------+------------------+------+-----+---------+-------+
11 rows in set (0.00 sec) mysql> select * from tb_bookinfo;
-> //
+----------+-----------+--------+-----------+------+-------+------+----------+------------+------+----+
| barcode | bookname | typeid | author | ISBN | price | page | bookcase | inTime | del | id |
+----------+-----------+--------+-----------+------+-------+------+----------+------------+------+----+
| 17120107 | Java King | 3 | LianJiang | 115 | 49.80 | 350 | 1 | 2018-04-17 | 0 | 1 |
| 17120108 | Lian | 1 | QiaoJiang | 116 | 50.00 | 351 | 2 | 2018-04-18 | 0 | 2 |
| 17120109 | Tian King | 2 | TianJiang | 117 | 51.10 | 352 | 3 | 2018-04-19 | 0 | 3 |
| 17120110 | ShenOba | 4 | ShenJiang | 118 | 52.15 | 300 | 4 | 2018-04-20 | 0 | 4 |
+----------+-----------+--------+-----------+------+-------+------+----------+------------+------+----+
4 rows in set (0.00 sec) mysql> select * from tb_bookbak;
-> //
ERROR 1146 (42S02): Table 'db_library.tb_bookbak' doesn't exist
mysql> select * from tb_bookinfobak;
-> //
Empty set (0.00 sec) mysql> select * from tb_booklog;
-> //
+----+------------+---------------------+
| id | event | logtime |
+----+------------+---------------------+
| 2 | look at me | 2018-04-18 19:40:02 |
+----+------------+---------------------+
1 row in set (0.00 sec) mysql> delete from tb_bookinfo where id=1;
-> //
Query OK, 1 row affected (0.01 sec) mysql> select * from tb_bookinfobak;
-> //
+----------+-----------+--------+-----------+------+-------+------+----------+------------+------+----+
| barcode | bookname | typeid | author | ISBN | price | page | bookcase | inTime | del | id |
+----------+-----------+--------+-----------+------+-------+------+----------+------------+------+----+
| 17120107 | Java King | 3 | LianJiang | 115 | 49.80 | 350 | 1 | 2018-04-17 | 0 | 1 |
+----------+-----------+--------+-----------+------+-------+------+----------+------------+------+----+
1 row in set (0.00 sec) mysql> select * from tb_booklog;
-> //
+----+------------+---------------------+
| id | event | logtime |
+----+------------+---------------------+
| 2 | look at me | 2018-04-18 19:40:02 |
| 8 | let me go | 2018-04-18 20:54:19 |
+----+------------+---------------------+
2 rows in set (0.00 sec)

  二、查看触发器

  1.SHOW TRIGGER语句

mysql> SHOW TRIGGERs\G
*************************** 1. row ***************************
Trigger: delete_book_info
Event: DELETE
Table: tb_bookinfo
Statement: BEGIN
INSERT INTO tb_booklog(event,logtime) values('let me go',now());
INSERT INTO tb_bookinfobak SELECT * FROM tb_bookinfo where id=OLD.id;
END
Timing: BEFORE
Created: 2018-04-18 20:35:07.19
sql_mode: ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
Definer: root@localhost
character_set_client: gbk
collation_connection: gbk_chinese_ci
Database Collation: utf8_general_ci
1 row in set (0.00 sec)

  2.查看triggers表中的触发信息

  (1)查看所有触发器的定义

mysql> SELECT * FROM information_schema.triggers\G
*************************** 1. row ***************************
TRIGGER_CATALOG: def
TRIGGER_SCHEMA: db_library
TRIGGER_NAME: delete_book_info
EVENT_MANIPULATION: DELETE
EVENT_OBJECT_CATALOG: def
EVENT_OBJECT_SCHEMA: db_library
EVENT_OBJECT_TABLE: tb_bookinfo
ACTION_ORDER: 1
ACTION_CONDITION: NULL
ACTION_STATEMENT: BEGIN
INSERT INTO tb_booklog(event,logtime) values('let me go',now());
INSERT INTO tb_bookinfobak SELECT * FROM tb_bookinfo where id=OLD.id;
END
ACTION_ORIENTATION: ROW
ACTION_TIMING: BEFORE
ACTION_REFERENCE_OLD_TABLE: NULL
ACTION_REFERENCE_NEW_TABLE: NULL
ACTION_REFERENCE_OLD_ROW: OLD
ACTION_REFERENCE_NEW_ROW: NEW
CREATED: 2018-04-18 20:35:07.19
SQL_MODE: ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
DEFINER: root@localhost
CHARACTER_SET_CLIENT: gbk
COLLATION_CONNECTION: gbk_chinese_ci
DATABASE_COLLATION: utf8_general_ci
*************************** 2. row ***************************
TRIGGER_CATALOG: def
TRIGGER_SCHEMA: sys
TRIGGER_NAME: sys_config_insert_set_user
EVENT_MANIPULATION: INSERT
EVENT_OBJECT_CATALOG: def
EVENT_OBJECT_SCHEMA: sys
EVENT_OBJECT_TABLE: sys_config
ACTION_ORDER: 1
ACTION_CONDITION: NULL
ACTION_STATEMENT: BEGIN IF @sys.ignore_sys_config_triggers != true AND NEW.set_by IS NULL THEN SET NEW.set_by = USER(); END IF; END
ACTION_ORIENTATION: ROW
ACTION_TIMING: BEFORE
ACTION_REFERENCE_OLD_TABLE: NULL
ACTION_REFERENCE_NEW_TABLE: NULL
ACTION_REFERENCE_OLD_ROW: OLD
ACTION_REFERENCE_NEW_ROW: NEW
CREATED: 2018-01-08 16:14:42.70
SQL_MODE:
DEFINER: mysql.sys@localhost
CHARACTER_SET_CLIENT: utf8
COLLATION_CONNECTION: utf8_general_ci
DATABASE_COLLATION: utf8_general_ci
*************************** 3. row ***************************
TRIGGER_CATALOG: def
TRIGGER_SCHEMA: sys
TRIGGER_NAME: sys_config_update_set_user
EVENT_MANIPULATION: UPDATE
EVENT_OBJECT_CATALOG: def
EVENT_OBJECT_SCHEMA: sys
EVENT_OBJECT_TABLE: sys_config
ACTION_ORDER: 1
ACTION_CONDITION: NULL
ACTION_STATEMENT: BEGIN IF @sys.ignore_sys_config_triggers != true AND NEW.set_by IS NULL THEN SET NEW.set_by = USER(); END IF; END
ACTION_ORIENTATION: ROW
ACTION_TIMING: BEFORE
ACTION_REFERENCE_OLD_TABLE: NULL
ACTION_REFERENCE_NEW_TABLE: NULL
ACTION_REFERENCE_OLD_ROW: OLD
ACTION_REFERENCE_NEW_ROW: NEW
CREATED: 2018-01-08 16:14:42.70
SQL_MODE:
DEFINER: mysql.sys@localhost
CHARACTER_SET_CLIENT: utf8
COLLATION_CONNECTION: utf8_general_ci
DATABASE_COLLATION: utf8_general_ci
3 rows in set (0.03 sec)

  (2)查看指定触发器名称的触发器的定义(指定数据库时将NAME改成SCHEMA,等号后面改成数据库名)

mysql> SELECT * FROM information_schema.triggers WHERE TRIGGER_NAME='delete_book_info'\G;
*************************** 1. row ***************************
TRIGGER_CATALOG: def
TRIGGER_SCHEMA: db_library
TRIGGER_NAME: delete_book_info
EVENT_MANIPULATION: DELETE
EVENT_OBJECT_CATALOG: def
EVENT_OBJECT_SCHEMA: db_library
EVENT_OBJECT_TABLE: tb_bookinfo
ACTION_ORDER: 1
ACTION_CONDITION: NULL
ACTION_STATEMENT: BEGIN
INSERT INTO tb_booklog(event,logtime) values('let me go',now());
INSERT INTO tb_bookinfobak SELECT * FROM tb_bookinfo where id=OLD.id;
END
ACTION_ORIENTATION: ROW
ACTION_TIMING: BEFORE
ACTION_REFERENCE_OLD_TABLE: NULL
ACTION_REFERENCE_NEW_TABLE: NULL
ACTION_REFERENCE_OLD_ROW: OLD
ACTION_REFERENCE_NEW_ROW: NEW
CREATED: 2018-04-18 20:35:07.19
SQL_MODE: ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
DEFINER: root@localhost
CHARACTER_SET_CLIENT: gbk
COLLATION_CONNECTION: gbk_chinese_ci
DATABASE_COLLATION: utf8_general_ci
1 row in set (0.01 sec)

  三、使用触发器

  1.触发器的执行顺序

mysql> create table if not exists tb_temp(
-> id int(11) PRIMARY KEY auto_increment NOT NULL,
-> event varchar(200) NOT NULL,
-> time timestamp NOT NULL DEFAULT current_timestamp
-> );
Query OK, 0 rows affected (0.03 sec)
mysql> create table tb_bookcase(
-> id int(11) PRIMARY KEY auto_increment NOT NULL,
-> name varchar(200)
-> );
Query OK, 0 rows affected (0.02 sec) mysql> create trigger before_in BEFORE INSERT ON
-> tb_bookcase FOR EACH ROW
-> INSERT INTO tb_temp(event) values('BEFORE INSERT');
Query OK, 0 rows affected (0.01 sec) mysql> create trigger after_in AFTER INSERT ON
-> tb_bookcase FOR EACH ROW
-> INSERT INTO tb_temp(event) values('AFTER INSERT');
Query OK, 0 rows affected (0.01 sec) mysql> select * from tb_bookcase;
Empty set (0.00 sec) mysql> select * from tb_temp;
Empty set (0.00 sec) mysql> INSERT INTO tb_bookcase(name) VALUES('左边第二个');
Query OK, 1 row affected (0.01 sec) mysql> select * from tb_bookcase;
+----+-----------------+
| id | name |
+----+-----------------+
| 1 | 左边第二个 |
+----+-----------------+
1 row in set (0.00 sec) mysql> select * from tb_temp;
+----+---------------+---------------------+
| id | event | time |
+----+---------------+---------------------+
| 1 | BEFORE INSERT | 2018-04-19 08:17:28 |
| 2 | AFTER INSERT | 2018-04-19 08:17:28 |
+----+---------------+---------------------+
2 rows in set (0.00 sec)

  2.使用触发器维护冗余数据(为了避免数据不一致问题的发生,尽量不要人工维护数据,建议通过编程自动维护)

mysql> create table tb_stock(
-> id int(11) PRIMARY KEY auto_increment NOT NULL.
-> id int(11) PRIMARY KEY auto_increment NOT NULL.^C
mysql> create table tb_stock(
-> id int(11) PRIMARY KEY auto_increment NOT NULL,
-> goodsname varchar(200) NOT NULL,
-> number int(11)
-> );
Query OK, 0 rows affected (0.02 sec)
mysql> select * from tb_stock;
+----+-------------+--------+
| id | goodsname | number |
+----+-------------+--------+
| 1 | 小猪佩qi | 100 |
+----+-------------+--------+
1 row in set (0.00 sec)
mysql> create table tb_sell(
-> id int(11) PRIMARY KEY auto_increment NOT NULL,
-> goodsname varchar(200),
-> number int(11)
-> );
-> //
Query OK, 0 rows affected (0.02 sec) mysql> select * from tb_sell;
-> //
Empty set (0.00 sec)
mysql> CREATE TRIGGER auto_number AFTER INSERT
-> ON tb_sell FOR EACH ROW
-> BEGIN
-> DECLARE sellnum int(10);
-> SELECT number FROM tb_sell where id=NEW.id INTO @sellnum;
-> UPDATE tb_stock SET number=number-@sellnum WHERE goodsname='小猪佩qi';
-> END
-> //
Query OK, 0 rows affected (0.01 sec)
mysql> INSERT INTO tb_sell(goodsname,number) VALUES
-> ('小猪佩qi',2);
-> //
Query OK, 1 row affected (0.01 sec) mysql> select * from tb_sell;
-> //
+----+-------------+--------+
| id | goodsname | number |
+----+-------------+--------+
| 1 | 小猪佩qi | 2 |
+----+-------------+--------+
1 row in set (0.00 sec) mysql> select * from tb_stock;
-> //
+----+-------------+--------+
| id | goodsname | number |
+----+-------------+--------+
| 1 | 小猪佩qi | 98 |
+----+-------------+--------+
1 row in set (0.00 sec)

  四、删除触发器

Mysql数据库(七)触发器的更多相关文章

  1. MySQL数据库之触发器

    1 引言 本文是对MySQL中触发器的总结,从触发器概念出发,结合实例对创建触发器.使用触发器.删除触发器进行介绍. 2 触发器简介 MySQL触发器和存储过程一样,都是嵌入到MySQL的一段程序.触 ...

  2. Mysql数据库的触发器、存储引擎和存储过程

    数据库的触发器 1.触发器 触发器是MySQL响应以下任意语句而自动执行的一条MySQL语句(或位于BEGIN和END语句之间的一组语句): DELETE,INSERT,UPDATE 我们可以监视某表 ...

  3. 40、mysql数据库(触发器)

    1.触发器说明: 使用触发器可以定制用户对表进行[增.删.改]操作时前后的行为,注意:没有查询. 2.创建触发器语法: (1)插入前: CREATE TRIGGER tri_before_insert ...

  4. mysql数据库之 存储引擎、事务、视图、触发器、存储过程、函数、流程控制、数据库备份

    目录 一.存储引擎 1.什么是存储引擎? 2.mysql支持的存储引擎 3. 使用存储引擎 二.事务 三.视图 1.什么是视图 2.为什么要用视图 3.如何用视图 四.触发器 为何要用触发器 创建触发 ...

  5. python面试题七: mysql数据库

    ---------------------------------------------------------------------------------------------------- ...

  6. PHP操作MySQL数据库之天龙八部 -- 七贱下天山 -- 六脉神剑

    天龙八部            八步操作数据库 七贱下天山        七步操作数据库  (将判断错误省略) 六脉神剑            六步操作数据库(将判断错误省略,将选择数据库添加到第一步 ...

  7. 第二百七十六节,MySQL数据库,【显示、创建、选定、删除数据库】,【用户管理、对用户增删改查以及授权】

    MySQL数据库,[显示.创建.选定.删除数据库],[用户管理.对用户增删改查以及授权] 1.显示数据库 SHOW DATABASES;显示数据库 SHOW DATABASES; mysql - 用户 ...

  8. 第二百八十四节,MySQL数据库-MySQL触发器

    MySQL数据库-MySQL触发器 对某个表进行[增/删/改]操作的前后如果希望触发某个特定的行为时,可以使用触发器,触发器用于定制用户对表的行进行[增/删/改]前后的行为. 1.创建触发器基本语法 ...

  9. mysql数据库从删库到跑路之mysql:视图、触发器、事务、存储过程、函数

    mysql:视图.触发器.事务.存储过程.函数 一.视图 视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名],用户使用时只需使用[名称]即可获取结果集,可以将该结果 ...

  10. MySQL数据库(6)_用户操作与权限管理、视图、存储过程、触发器、基本函数

    用户操作与权限管理 MySQL用户操作 创建用户 方法一: CREATE USER语句创建 CREATE USER "用户名"@"IP地址" IDENTIFIE ...

随机推荐

  1. 构建之法——homework1:问题思考

    1.我看了第一章概论,1.2.4 软件工程的目标——创造“足够好”的软件,其中提到了什么是好的软件?  软件工程的一个要素就是把软件的Bug都消灭掉的过程. 提问:我们知道Bug是不可能完全消灭掉的, ...

  2. css3练习

    读条的实现1 .div{position: relative;border: 1px solid #111;width: 80px;height: 60px} .div div{width: 20px ...

  3. 快学Scala 第一课 (变量,类型,操作符)

    Scala 用val定义常量,用var定义变量. 常量重新赋值就会报错. 变量没有问题. 注意:我们不需要给出值或者变量的类型,scala初始化表达式会自己推断出来.当然我们也可以指定类型. 多个值和 ...

  4. 《Java语言程序设计》编程练习6.31(财务应用程序:信用卡号的合法性)

    6.31(财务应用程序:信用卡号的合法性)信用卡号遵循下面的模式.一个信用卡号必须是13到16位的整数.它的开头必须是: 4,指Visa卡 5,指Master卡 37,指American Expres ...

  5. springboot结合jpa

    idea中新建springboot项目,引入spring-boot-starter-data-jpa依赖 application.yml中配置数据库连接,示例如下: spring: datasourc ...

  6. 简单cookie入侵

    在当前网站,按下F12键进入开发者模式,在console控制台输入:document.cookie获取cookie值如: 复制你得到cookie值,你或通过每种方式获取Cookie,例如:当别人点击你 ...

  7. e课表项目第二次冲刺周期第十天

    昨天完成了什么? 昨天还有一天第一次冲刺周期就结束了,我们的工作也接近尾声了,所以今天我利用之前的方法,完成了对监听的设置,以及对修改界面的编写,可以实现相应的删除和修改的功能,然后我和我们组的成员商 ...

  8. ini 文件操作指南

    今天总结一篇工具箱文章. ini 类型文件通常作为程序的初始化文件.不同于我们常见的配置文件通篇 key-value 的键值对形式,ini 文件在键值对的基础之上还有分类节点,比如我们常见的 Mysq ...

  9. Spring Boot 2.2 正式发布,大幅性能提升 + Java 13 支持

    之前 Spring Boot 2.2没能按时发布,是由于 Spring Framework 5.2 的发布受阻而推迟.这次随着 Spring Framework 5.2.0 成功发布之后,Spring ...

  10. 【源码解析】凭什么?spring boot 一个 jar 就能开发 web 项目

    问题 为什么开发web项目,spring-boot-starter-web 一个jar就搞定了?这个jar做了什么? 通过 spring-boot 工程可以看到所有开箱即用的的引导模块 spring- ...