1. Difference between Before and After in Trigger
1) After we perform one action, the trigger is invoked. But there is no machanism to check if the action we perform is reasonable.
# Bootstrap data select * from goods; +----------+------------+--------------+ | goods_id | goods_name | goods_amount | +----------+------------+--------------+ | 1 | ASUS | 13 | | 2 | ThinkPad | 10 | +----------+------------+--------------+ select * from order_table; +----------+----------+-------------+ | order_id | goods_id | order_count | +----------+----------+-------------+ | 2 | 1 | 1 | | 3 | 1 | 9 | +----------+----------+-------------+ # Bootstrap trigger # 1. If we place one order item, goods amount reduce. # 2. If we delete one order item, goods amount increase. # 3. If we modify one order item, goods amount modify. # What if we place an order like below insert into order_table(goods_id, order_count) values(1, 20); select * from order_table; +----------+----------+-------------+ | order_id | goods_id | order_count | +----------+----------+-------------+ | 2 | 1 | 1 | | 3 | 1 | 9 | | 4 | 1 | 20 | +----------+----------+-------------+ select * from goods; +----------+------------+--------------+ | goods_id | goods_name | goods_amount | +----------+------------+--------------+ | 1 | ASUS | -7 | | 2 | ThinkPad | 10 | +----------+------------+--------------+ # The result is not reasonable! delete from order_table where order_id = 4; select * from goods; +----------+------------+--------------+ | goods_id | goods_name | goods_amount | +----------+------------+--------------+ | 1 | ASUS | 13 | | 2 | ThinkPad | 10 | +----------+------------+--------------+ select * from order_table; +----------+----------+-------------+ | order_id | goods_id | order_count | +----------+----------+-------------+ | 2 | 1 | 1 | | 3 | 1 | 9 | +----------+----------+-------------+ # There is lack of checking mechanism to validate whether the order item is meaningful. # So we can use Before to validate
2) After: Once the operation that triggered this trigger, then there is no way back. The operation in trigger have to be done. The trigger is triggered after CUD works.
3) Before: Once the operation that triggered this trigger, we can make some validate/check and revocate the operation. The trigger is triggered before CUD works.
# For the order item, if order_count > 5, we suppose this order item is exception and we should modify order_count to 5; # Four essential elements for creating Trigger # Scope: Table order_table # Trigger Event: Insert # When: Before # Triggered Event: Update drop trigger add_order; delimiter $ create trigger add_order before insert on order_table for each row begin if new.order_count > 5 then set new.order_count = 5; end if; update goods set goods_amount = goods_amount - new.order_count where goods_id = new.goods_id; end$ delimiter ; # Validate if the trigger works correctly insert into order_table(goods_id, order_count) values(1, 10); select * from order_table; +----------+----------+-------------+ | order_id | goods_id | order_count | +----------+----------+-------------+ | 2 | 1 | 1 | | 3 | 1 | 9 | | 5 | 1 | 5 | +----------+----------+-------------+ select * from goods; +----------+------------+--------------+ | goods_id | goods_name | goods_amount | +----------+------------+--------------+ | 1 | ASUS | 8 | | 2 | ThinkPad | 10 | +----------+------------+--------------+ # Bingo!
2. How to list all triggers?
# Show all triggers show triggers;
相关推荐
mysql安装配置教程 MySQL安装和配置的步骤,适用于Windows系统: Windows系统上安装MySQL: 下载MySQL:
《高可用MySQL:构建健壮的数据中心》是“MySQL High Availability”的中文翻译版,主要讲解真实环境下如何使用MySQL的复制、集群和监控特性,揭示MySQL可靠性和高可用性的方方面面。《高可用MySQL:构建健壮的数据...
chown -R mysql:mysql /usr/local/mysql/data/ chown -R mysql:mysql /usr/local/mysql/lib/ chown -R mysql:mysql /usr/local/mysql/sock/ 启动:mysql服务 /usr/local/mysql/share/mysql/mysql.server start 进入...
mysql数据库:mysql数据库:mysql数据库:mysql数据库:mysql数据库:mysql数据库:mysql数据库:mysql数据库:mysql数据库:mysql数据库:mysql数据库:mysql数据库:mysql数据库:mysql数据库:mysql数据库:mysql...
MySQL 版本号 高山: 3.7 MySQL的: mariaDB-10.1.28-r1 ...拉: docker pull quay.io/perriea/alpine-mysql:1.0 运行: docker run -d -p 3306:3306 --env-file .env quay.io/perriea/alpine-mysql:1.0
《涂抹MySQL:跟着三思一步一步学MySQL》是一本旨在帮助初学者和中级用户深入理解MySQL数据库系统的教程。这本书通过清晰的解释和实例,逐步引导读者掌握MySQL的核心概念和技术。MySQL是一种广泛使用的开源关系型...
MySQL Connector/J 5.1.47 是 MySQL 官方提供的用于Java应用程序连接MySQL数据库的驱动程序,它实现了Java Database Connectivity (JDBC) API。这个工具包是Java开发者与MySQL数据库进行交互的重要桥梁,允许他们...
Gain skills in MySQL’s fundamental features, including supported data types, database management syntax, triggers, views, stored routine syntax, and import/export capabilities Work with hundreds of...
java连接mysql的稳定驱动
jsp+mysql:毕业设计动漫网站。内含项目代码,数据库脚本。
String url = "jdbc:mysql://localhost:3306/mydatabase"; String user = "myuser"; String password = "mypassword"; Connection conn = DriverManager.getConnection(url, user, password); ``` 4. **执行...
String url = "jdbc:mysql://localhost:3306/mydatabase"; String user = "username"; String password = "password"; try { Connection conn = DriverManager.getConnection(url, user, password); System....
Artifact mysql-connector-java Group mysql Version 8.0.21 Last update 16. June 2020 Newest version Yes Organization Oracle Corporation URL http://dev.mysql.com/doc/connector-j/en/ License The GNU ...
Java连接MySQL数据库(优化版) 目的: 当需要把应用程序从开发环境移植到生产环境时,不用修改源代码,只需要修改mysql.ini配置文件 方法: 不直接把数据库连接信息..."jdbc:mysql://127.0.0.1:3306/dome" jdbc:mysq
DBD-mysql-3.0008 DBD-mysql-3.0008 mysqlhotcopy 需要的这个文件 问题:mysqlhotcopy备份报错 /usr/local/mysql/bin/mysqlhotcopy -u root -p --addtodest --noindices --flushlog -q qeedoo /tmp/bak install_...
在不同系统中安装MySQL:提供在Windows、Linux、MacOSX、NetWare等平台上的MySQL安装指南。 5.4. 使用源码分发版安装MySQL:解释如何通过源码安装MySQL,包括配置选项和解决编译问题。 5.5. 安装后的设置和测试:...
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "username", "password"); ``` 3. **SQL执行**:一旦建立了连接,可以创建`Statement`对象来执行SQL查询或更新。例如: ``...
java连接mysql数据库的驱动,里边包含两个版本:5.1.47、8.0.28 也可从以下两个地址免费下载: 1. https://repo1.maven.org/maven2/mysql/mysql-connector-java/ 2. ...