`
DavyJones2010
  • 浏览: 151650 次
  • 性别: 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-8.0.28

    java连接mysql的稳定驱动

    jsp+mysql:动漫网站

    jsp+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. **执行...

    MySQL官方提供的驱动包 mysql-connector-java-5.1.30.zip (亲测可用)

    String url = "jdbc:mysql://localhost:3306/mydatabase"; String user = "username"; String password = "password"; try { Connection conn = DriverManager.getConnection(url, user, password); System....

    r2d2-mysql:MySQL对r2d2连接池的支持(Rust)

    r2d2-mysql 连接池支持库。 文档位于安装只需在您的Cargo.toml中包含另一个[dependencies]部分: [ dependencies ]r2d2_mysql = " * "例子extern crate mysql;extern crate r2d2_mysql;extern crate r2d2;use std ::...

    mysql-8.0.37 docker离线镜像安装包

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

    C++访问MySQL

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

    DBD-mysql-3.0008

    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文档完美中文版

    在不同系统中安装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查询或更新。例如: ``...

Global site tag (gtag.js) - Google Analytics