• MySQL 触发器


    触发器:trigger
    触发器是与表有关的数据库对象,指在 insert/update/delete 之前或之后,触发并执行触发器中定义的SQL语句集
    合。
    
    语法:
    create trigger trigger_name
    before|after    insert|update|delete on table_name
    for each row
    begin
      ----trigger_stmt;
    end;
    
    ---通过触发器记录 emp 表的数据变更1 日志 , 包含增加, 修改 , 删除
    
    create table emp_logs(
    id int(11) not null auto_increment,
    operation varchar(20) not null comment '操作类型, insert/update/delete',
    operate_time datetime not null comment '操作时间',
    operate_id int(11) not null comment '操作表的ID',
    operate_params varchar(500) comment '操作参数',
    primary key(`id`)
    )engine=innodb default charset=utf8;
    
    ===========================================================
    触发器--插入数据:
    create trigger emp_insert_trigger
    after insert
    on emp
    for each row
    begin
      insert into emp_logs(id,operation,operate_time,operate_id,operate_params) values(null,'insert',now(),new.id,concat('插入后(id:',new.id,', name:',new.name,', age:',new.age,', salary:',new.salary,')'));
      
    end;
    
    insert into emp(id,name,age,salary) values(null, '光明左使',30,3500);
    
    select * from emp;
    select * from emp_logs;
    
    
    
    触发器--修改数据:
    create trigger emp_update_trigger
    after update
    on emp
    for each row
    begin
      insert into emp_logs(id,operation,operate_time,operate_id,operate_params) values(null,'update',now(),new.id,concat('修改前(id:',old.id,', name:',old.name,', age:',old.age,', salary:',old.salary,'), 修改后(',new.id,', name:',new.name,', age:',new.age,', salary:',new.salary));
      
    end;
    
    update emp set age = 39 where id = 3;
    
    select * from emp;
    select * from emp_logs;
    
    concat  合并数组
    
    
    
    
    触发器--删除数据:
    create trigger emp_delete_trigger
    after delete
    on emp
    for each row
    begin
      insert into emp_logs(id,operation,operate_time,operate_id,operate_params) values(null,'delete',now(),old.id,concat('删除前(id:',old.id,', name:',old.name,', age:',old.age,', salary:',old.salary,')'));
      
    end;
    
    select * from emp;
    delete from emp where id = 5;
    select * from emp_logs;
    
    
    查看触发器
    show triggers;
    
    删除触发器
    
    drop trigger trigger_name;
    
    
    mysql的体系结构
    
    整个MySQL Server由以下组成
    
    Connection Pool : 				  	连接池组件
    Management Services & Utilities : 	管理服务和工具组件
    SQL Interface : 				  	SQL接口组件
    Parser : 						  	查询分析器组件
    Optimizer : 					  	优化器组件
    Caches & Buffers : 					缓冲池组件
    Pluggable Storage Engines : 		存储引擎
    File System : 						文件系统
    
    存储引擎就是存储数据,建立索引,更新查询数据等等技术的实现方式 。存储引擎是基于表的,而不是基于库的。
    所以存储引擎也可被称为表类型。
    
    MySQL5.0支持的存储引擎包含 : InnoDB 、MyISAM 、BDB、MEMORY、MERGE、EXAMPLE、NDB Cluster、
    ARCHIVE、CSV、BLACKHOLE、FEDERATED等,其中InnoDB和BDB提供事务安全表,其他存储引擎是非事务安
    全表。
    
    show engines;
    show variables like '%storage_engine%';
    
    MyISAM  不支持事务   存储一张表需要三个文件且名字相同,后缀不同
    .frm 存储表定义
    .MYD 存储数据
    .MYI 储存索引
    
    
    InnoDB 引擎
    .frm 表结构
    .ibd 表数据和索引
    
    create table goods_innodb(
    id int NOT NULL AUTO_INCREMENT,
    name varchar(20) NOT NULL,
    primary key(id)
    )ENGINE=innodb DEFAULT CHARSET=utf8;
    
    start transaction;
    insert into goods_innodb(id,name)values(null,'Meta20');
    insert into goods_innodb(id,name)values(null,'Meta21');
    commit;
    
    create database demo_02 default charset=uff8mb4;
    use demo_02;
    
    
    create table country_innodb(
    country_id int NOT NULL AUTO_INCREMENT,
    country_name varchar(100) NOT NULL,
    primary key(country_id)
    )ENGINE=InnoDB DEFAULT CHARSET=utf8;
    
    
    create table city_innodb(
    city_id int NOT NULL AUTO_INCREMENT,
    city_name varchar(50) NOT NULL,
    country_id int NOT NULL,
    primary key(city_id),
    key idx_fk_country_id(country_id),
    CONSTRAINT `fk_city_country` FOREIGN KEY(country_id) REFERENCES
    country_innodb(country_id) ON DELETE RESTRICT ON UPDATE CASCADE
    )ENGINE=InnoDB DEFAULT CHARSET=utf8;
    
    
    insert into country_innodb values(null,'China'),(null,'America'),(null,'Japan');
    
    insert into city_innodb values(null,'Xian',1),(null,'NewYork',2),
    (null,'BeiJing',1);
    
    
    start transaction;      //开启事务
    
    外键约束:FOREIGN KEY   ---ON DELETE RESTRICT ON UPDATE CASCADE
    
    在创建外键的时候, 要求父表必须有对应的索引 , 子表在创建外键的
    时候, 也会自动的创建对应的索引。
    
    ON DELETE RESTRICT ---->删除主表数据时,如果有关联记录,不删除;
    ON UPDATE CASCADE ----> 更新主表,如果子表有关联记录,也会更新子表记录;
    

      

  • 相关阅读:
    安装mysql
    date, Calendar
    心态
    jdbc ---- DBUTilDao 类
    jdbc--------JdbcUtilDao 类
    Python与C/C++相互调用
    用链表和数组实现HASH表,几种碰撞冲突解决方法
    用链表实现消息队列
    文件系统的发展
    [深度学习]开源的深度学习框架
  • 原文地址:https://www.cnblogs.com/walkersss/p/15129844.html
Copyright © 2020-2023  润新知