zoukankan      html  css  js  c++  java
  • mysql触发器小实验

    今天实验了一下mysql的触发器

    mysql> use test;
    Database changed
    mysql> desc time;
    +-------+---------------------+------+-----+---------+-------+
    | Field | Type                | Null | Key | Default | Extra |
    +-------+---------------------+------+-----+---------+-------+
    | id    | bigint(60) unsigned | NO   |     | 0       |       |
    +-------+---------------------+------+-----+---------+-------+
    1 row in set (0.01 sec)
    
    mysql> desc time_2;
    +-------+---------------------+------+-----+---------+-------+
    | Field | Type                | Null | Key | Default | Extra |
    +-------+---------------------+------+-----+---------+-------+
    | id    | bigint(60) unsigned | NO   |     | 0       |       |
    +-------+---------------------+------+-----+---------+-------+
    1 row in set (0.01 sec)
    
    #创建触发器 当 time表插入一条的时候 time_2表也插入这条新增的数据
    mysql> delimiter $$
    mysql> create trigger t_afterinsert_on_time 
        -> after insert on time for each row 
        -> begin 
        ->     insert into time_2(id) values (new.id);
        -> end
        -> $$
    Query OK, 0 rows affected (0.14 sec)
    
    mysql> insert into time values (100);
        -> $$
    Query OK, 1 row affected (0.13 sec)
    
    mysql> select * from time;$$
    +-----+
    | id  |
    +-----+
    | 100 |
    +-----+
    1 row in set (0.00 sec)
    
    mysql> select * from time_2;$$
    +-----+
    | id  |
    +-----+
    | 100 |
    +-----+
    1 row in set (0.00 sec)
    
    mysql> show triggers;$$
    +-----------------------+--------+-------+-----------------------------------------------------+--------+---------+----------+----------------+----------------------+----------------------+--------------------+
    | Trigger               | Event  | Table | Statement                                           | Timing | Created | sql_mode | Definer        | character_set_client | collation_connection | Database Collation |
    +-----------------------+--------+-------+-----------------------------------------------------+--------+---------+----------+----------------+----------------------+----------------------+--------------------+
    | t_afterinsert_on_time | INSERT | time  | begin 
        insert into time_2(id) values (new.id);
    end | AFTER  | NULL    |          | root@localhost | gbk                  | gbk_chinese_ci       | utf8_general_ci    |
    +-----------------------+--------+-------+-----------------------------------------------------+--------+---------+----------+----------------+----------------------+----------------------+--------------------+
    1 row in set (0.01 sec)
    
    
    #创建触发器 当 time表删除一条的时候 time_2表也删除这条数据
    mysql> create trigger t_afterdelete_on_time 
        -> after delete on time for each row 
        -> begin 
        ->     delete from time_2 where id=old.id;
        -> end
        -> $$
    Query OK, 0 rows affected (0.14 sec)
    
    mysql> show triggers;
        -> $$
    +-----------------------+--------+-------+-----------------------------------------------------+--------+---------+----------+----------------+----------------------+----------------------+--------------------+
    | Trigger               | Event  | Table | Statement                                           | Timing | Created | sql_mode | Definer        | character_set_client | collation_connection | Database Collation |
    +-----------------------+--------+-------+-----------------------------------------------------+--------+---------+----------+----------------+----------------------+----------------------+--------------------+
    | t_afterinsert_on_time | INSERT | time  | begin 
        insert into time_2(id) values (new.id);
    end | AFTER  | NULL    |          | root@localhost | gbk                  | gbk_chinese_ci       | utf8_general_ci    |
    | t_afterdelete_on_time | DELETE | time  | begin 
        delete from time_2 where id=old.id;
    end     | AFTER  | NULL    |          | root@localhost | gbk                  | gbk_chinese_ci       | utf8_general_ci    |
    +-----------------------+--------+-------+-----------------------------------------------------+--------+---------+----------+----------------+----------------------+----------------------+--------------------+
    2 rows in set (0.01 sec)
    
    mysql> delete from time where id=100;$$
    Query OK, 1 row affected (0.09 sec)
    
    mysql> select * from time;$$
    Empty set (0.00 sec)
    
    mysql> select * from time_2;$$
    Empty set (0.00 sec)
    
    mysql> exit
  • 相关阅读:
    Redis-高级教程-Java 使用
    Redis-高级教程-分区
    Redis-高级教程-管道技术
    Redis-高级教程-客户端连接
    Redis-高级教程-性能测试
    Redis-高级教程-安全
    Redis-高级教程-数据备份与恢复
    Redis-命令-Stream
    NG-ZORRO + Angular11增加自定义全局样式,不影响其他页面全局样式,仅作用于当前页面
    VUE上传表格文件发送后端,后端解析以及上传文件,前端进行解析的实现方法
  • 原文地址:https://www.cnblogs.com/lizhaoyao/p/6763433.html
Copyright © 2011-2022 走看看