MySQL 触发器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | 触发器: 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 ----> 更新主表,如果子表有关联记录,也会更新子表记录; |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· DeepSeek “源神”启动!「GitHub 热点速览」
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· NetPad:一个.NET开源、跨平台的C#编辑器
2020-08-11 MEM0005 Persistent correctable memory error limit reached for DIMM 1-8 reseat memory
2020-08-11 Dell 服务器错误代码
2020-08-11 设置DELL R720 的CPU 风扇转速