`
DavyJones2010
  • 浏览: 153770 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

MySQL: Trigger (Part II)

阅读更多

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安装和配置的步骤,适用于Windows系统: Windows系统上安装MySQL: 下载MySQL:

    mysql安装配置教程 MySQL安装和配置的步骤,适用于Windows系统: Windows系统上安装MySQL: 下载MySQL:

    [高可用MySQL:构建健壮的数据中心.].查理斯·贝尔.扫描版.part1

    《高可用MySQL:构建健壮的数据中心》是“MySQL High Availability”的中文翻译版,主要讲解真实环境下如何使用MySQL的复制、集群和监控特性,揭示MySQL可靠性和高可用性的方方面面。《高可用MySQL:构建健壮的数据...

    [高可用MySQL:构建健壮的数据中心.].查理斯·贝尔.扫描版.part2

    《高可用MySQL:构建健壮的数据中心》是“MySQL High Availability”的中文翻译版,主要讲解真实环境下如何使用MySQL的复制、集群和监控特性,揭示MySQL可靠性和高可用性的方方面面。《高可用MySQL:构建健壮的数据...

    mysql for liunx 免安装版

    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 进入...

    基于JavaSpring推荐算法新生宿舍管理系统技术框架:前端:html css vue后端:java springboo

    mysql数据库:mysql数据库:mysql数据库:mysql数据库:mysql数据库:mysql数据库:mysql数据库:mysql数据库:mysql数据库:mysql数据库:mysql数据库:mysql数据库:mysql数据库:mysql数据库:mysql数据库:mysql...

    alpine-mysql:Docker镜像MySQL(Alpine)

    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的核心概念和技术。MySQL是一种广泛使用的开源关系型...

    mysql-connector-java-5.1.47

    MySQL Connector/J 5.1.47 是 MySQL 官方提供的用于Java应用程序连接MySQL数据库的驱动程序,它实现了Java Database Connectivity (JDBC) API。这个工具包是Java开发者与MySQL数据库进行交互的重要桥梁,允许他们...

    mysql-connector-java-5.1.32

    String url = "jdbc:mysql://localhost:3306/mydatabase"; String user = "myuser"; String password = "mypassword"; Connection conn = DriverManager.getConnection(url, user, password); ``` 4. **执行...

    linux下mysql的rpm安装包

    一、卸载原来的mysql: 卸载一: 输入: #rpm -qa | grep -i mysql 显示: mysql-libs-5.1.52-1.el6_0.1.i686 卸载方法: yum -y remove mysql-libs-5.1.52-1.el6_0.1.i686 卸载二: 输入: #rpm -qa | grep -i ...

    mysql-connector-java-8.0.21.jar

    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 ...

    mysql-8.0.37 docker离线镜像安装包

    mysql-8.0.37 docker离线镜像安装包(开源资源 )

    C++访问MySQL

    在给定的代码示例中,我们看到了一个C++程序,该程序用于连接到MySQL数据库并执行SQL查询。以下是对这段代码中涉及的关键知识点的详细解释: 1. **C++与MySQL连接**:C++通过MySQL C API来访问MySQL数据库。`#...

    MYSQL文档完美中文版

    在不同系统中安装MySQL:提供在Windows、Linux、MacOSX、NetWare等平台上的MySQL安装指南。 5.4. 使用源码分发版安装MySQL:解释如何通过源码安装MySQL,包括配置选项和解决编译问题。 5.5. 安装后的设置和测试:...

    mysql连接包mysql-connector-java-5.1.27.jar

    Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "username", "password"); ``` 3. **SQL执行**:一旦建立了连接,可以创建`Statement`对象来执行SQL查询或更新。例如: ``...

    mysql金典教材——9本经典合集4

    1.[MySQL技术内幕:SQL编程].姜承尧.扫描版 2.Effective+MySQL之SQL语句最优化 3.Expert MySQL 4.MySQL 5权威指南中文版(第3版) 5.MySQL高性能书籍_第3版(中文) 6.MySQL技术内幕(第4版) ...mysql.part6.rar

    mysql 安装程序 part3

    本教程将深入探讨MySQL的安装过程,特别关注“part3”,这通常涉及到安装过程的高级步骤和配置选项。MySQL是一个开源的关系型数据库管理系统,以其高效、可靠和易用性而闻名,广泛应用于Web应用、数据分析、数据存储...

    MySQL运维内参.part2

    MySQL运维内参.part2MySQL运维内参.part2MySQL运维内参.part2MySQL运维内参.part2MySQL运维内参.part2MySQL运维内参.part2MySQL运维内参.part2MySQL运维内参.part2MySQL运维内参.part2

    mysql-connector-java.jar,mysql-essential-5.1.68-win32.zip,mysql安装包

    MySQL是一种广泛使用的开源关系型数据库管理系统(RDBMS),它以其高效、稳定和易于管理的特点在Web开发领域占据了重要地位。MySQL的安装包通常包含了多种组件,以满足不同用户的需求。这里提到的“mysql-connector-...

Global site tag (gtag.js) - Google Analytics