一、存储引擎
1.1MySQL体系结构
1.2存储引擎简介
存储引擎就是存储数据、建立索引、更新/查询数据等技术的实现方式。存储引擎是基于表的,而不是基于库的,所以存储引擎也可被称为表类型。
-- 创建表any_myisan,并指定MyISAM存储引擎
create table my_myisam(
id int ,
name varchar(10)
)engine = MyISAM;
-- 查看当前数据库支持的存储引擎
SHOW ENGINES;
1.3存储引擎特点
- InnoDB
介绍
InnoDB是一种兼顾高可靠性和高性能的通用存储引擎,在MySQL5.5之后,InnoDB是默
认的MySQL存储引擎。
特点
DML操作遵循ACID模型,支持事务;
行级锁,提高并发访问性能
支持外键FOREIGN KEY约束,保证数据的完整性和正确性;
文件
xxx.ibd:xxx代表的是表名,innoDB引擎的每张表都会对应这样一个表空间文件,存储该表的表结构(from、sdi)、数据和索引
1.4存储引擎选择
在选择存储引擎时,应该根据应用系统的特点选择合适的存储引擎。对于复杂的应用系统还可以根据实际情况选择多种存储引擎进行组合。
- InnoDB:是MySQL默认的存储引擎,支持事务、外键。如果应用对事务的完整性有比较高的要求,在并发条件下要求数据的一致性,数据操作除了插入和查询之外,还包括很多的更新、删除操作,那么InnoDB存储引擎是比较合适的选择
- MyISAM:如果应用时以读操作和插入操作为主,只有很少的更新和删除操作,并且对事务的完整性、并发性要求不是很高,那么这个存储引擎是非常合适的。
- MEMORY:将所有数据保存在内存中,访问速度快,通常用于临时表及缓存。MEMORY的缺陷就是对表的大小有限制,太大的表无法缓存在内存中,而且无法保障数据的安全性
二、索引
2.1索引概述
介绍
索引(index)是帮助MySQL高效获取数据的数据结构(有序).在数据之外,数据库系统还维护着满足特定查找算法的数据结构,这些数据结构以某种方式引用(指向)数据,这样就可以在这些数据结构上实现高级查找算法,这些数据结构就是索引
注:上述二叉树索引结构的只是一个示意图,并不是真实的索引结构。
优缺点
2.2索引结构
MySQL的索引是在存储引擎层实现的,不同的存储引擎有不同的结构,主要包含以下几种:
通常说的索引,如果没有特别指明,都是指B+树结构组织的索引
二叉树
二叉树缺点:顺序插入时,会形成一个链表,查询性能大大降低,大数据量情况下,层级较深,检索速度慢
红黑树:大数据量情况下,层级较深,检索速度慢。
B-Tree(多路平衡查找树)
以一颗最大度数(max-degree)为5(5阶)的b-tree为例(每个节点最多存储4个key,5个指针):
B+Tree
以一颗最大度数(max-degree)为4(4阶)的b+tree为
相对于B-Tree区别:
①所有的数据都会出现在叶子节点
②叶子节点形成一个单向链表
2.2.1B+Tree的数据结构
MySQL索引数据结构对经典的B+Tree进行了优化。在原B+Tree的基础上,增加一个指向相邻叶子结点的链表指针,就形成了带有顺序指针的B+Tree,提高区间访问的性能。
2.2.2Hash索引的数据结构
哈希索引就是采用一定的hash算法,将键值算成新的hash值,映射到对应得槽位上,然后存储到hash表中。如果两个(或多个)键值,映射到一个相同的槽位上,他们就产生恶劣hash冲突(也叫hash碰撞),可以通过链表来解决
Hash索引的特点
①Hash只能用于对等比较(=,in)不支持范围查询(between,>,<,...)
②无法利用索引完成排序操作
③查询效率高,通常只需要一次检索就可以可,效率通常要高于B+tree索引
存储引擎支持
在MySQL中,支持hash索引的是Memor引擎,而InnoDB中具有自适应hash功能,hash索引是存储引擎根据B+Tree索引在指定条件下自动构建的。
为什么InnoDB存储引擎选择使用B+tree索引结构?
- 相对于二叉树,层级更少,搜索效率高;
- 对于B-tree,无论是叶子结点还是非叶子节点,都会保存数据,这样导致一页中存储的键值减少,指针跟着减少,要同样保存大量数据,只能增加树的高度,导致性能降低;
- 相对Hash索引,B+tree支持范围匹配及排序操作;
2.3索引分类
在InnoDB存储引擎中,根据索引的存储形式,又可以分为以下两种:
聚集索引选取规则;
- 如果存在主键,主键索引就是聚集索引。
- 如果不存在主键,将使用第一个唯一(UNIQUE)索引作为聚集索引。
- 如果没有主键,或没有合适的唯一索引,则InnoDB会自动生成一个rowid作为隐藏的聚集索引。
InnoDB主键索引的B+tree高度为多高?
2.4索引语法
- 创建索引
CREATE [UNIQUE|FULLTEXT] INDEX index_name ON table_name(index_col_name,...);
- 查看索引
SHOW INDEX FROM table_name;
- 删除索引
DROP INDEX index_name ON table_name;
CREATE INDEX idx_user_name ON tb_user(name);
CREATE UNIQUE INDEX idx_user_phone ON tb_user(phone)
CREATE INDEX idx_user_pro_age_sta ON tb_user(profession,age,status);
CREATE INDEX idx_email ON tb_user(emial);
2.5SQL性能分析
2.5.1SQL执行频率
MySQL客户端连接成功后,通过show [session|global] status命令可以提供服务器状态信息。通过如下命令,可以查看当前数据库的INSERT、UPDATE、DELETE、SELECT的访问频次;
show global status like 'Com_______';
2.5.2慢查询日志
记录了所有执行时间超过指定参数的所有SQL语句的日志。
MySQL的慢查询日志默认没有开启,需要在MySQL的配置文件(/etc/my.cnf)中配置如下信息:
# 开启MySQL慢日志查询开关
show_query_log=1
#设置慢日志的时间为2秒,SQL语句执行时间超过2秒,就会被视为慢查询,记录慢查询日志
long_query_time=2
配置完毕后,通过以下指令重新启动Mysql服务器进行测试,查看慢日志文件中记录的信息/var/lib/mysql/localhost-slow.log
2.5.3profile详情
show profiles能够在做SQL优化时帮助我们了解时间都耗费到哪了去了,通过have_profiling参数,能够看到当前MySQL是否支持profile操作:
SELECT @@have_profiling;
默认profiling是关闭的,可以通过set语句在session/globa级别开启profiling:
SET profiling=1;
执行一系列的业务SQL的操作,然后通过如下指令查看指令的执行耗时:
#查看每一条SQL的耗时基本情况
show profiles;
#查看指定query_id的SQL语句各个阶段的耗时情况
show profile for query query_id;
#查看指定query_id的SQL语句CPU的使用情况
show profile cpu for query query_id;
2.5.4explain执行计划
EXPLAIN或者DESC命令获取MySQL如何执行SELECT语句的信息,包括在SELECT语句执行过程中表如何连接和连接的顺序。
语法:
# 直接在select语句之前加上关键字explain/desc
EXPLAIN SELECT 字段列表 FROM 表名 WHERE 条件;
EXPLAIN执行计划各字段含义:
- id
select查询的序列号,表示查询中执行select子句或者是操作表的顺序(id相同,执行顺序从上到下;id不同,值越大,越先执行).
- select_type
表示SELECT的类型,常见的取值有SIMPLE(简单表,即不使用表连接或者子查询)、PRIMARY(主查询,即外层的查询)、UNION(UNION中的第二个或者后面的查询语句)、SUBQUERY(SELECT/WHERE之后包含了子查询)等
- type
表示连接类型,性能由好到差的连接类型为NULL、system、const、eq_ref、ref、range、index、all.
- possible_key
显示可能应用在这张表上的索引,一个或者多个
- key
实际使用的索引,如果为NULL,则没有使用索引
- key_len
表示索引中使用的字节数,该值为索引字段最大可能长度,并非实际使用长度,在不损失精确性的前提下,长度越短越好。
- rows
MySQL认为必须要执行的行数,在Innodb引擎的表中,是一个估计值,可能并不总是准确的
- filtered
表示返回结果的行数占读取行数的百分比,filtered的值越大越好
2.6索引使用
验证索引效率
在未建立索引之前,执行如下SQL语句,查看SQL的耗时
SELECT * FROM tb_sku WHERE sn = '100000003145001';
针对字段创建索引
create index idx_sku_sn on tb_sku(sn);
然后再次执行相同的SQL语句,再次查看SQL的耗时
SELECT * FROM tb_sku WHERE sn = '1000000003145001';
- 最左前缀法则
如果索引了多列(联合索引),要遵循最左前缀法则。最左前缀法则指的是查询从索引的最左列开始,并且不能跳过索引中的列,索引将部分失效(后面的字段索引失效)。
- 范围查询
联合索引中,出现范围查询(>,<),范围查询右侧的列索引失效
避免出现,尽量使用(>=,<=)
2.6.1索引失效
- 索引列运算
不要在索引列上进行运算操作,索引将失效
explain select from tb_user where substring(phone,10,2) = '15';
- 字符串不加单引号
字符串类型字段使用时,不加引号,索引将失效
explain select * from tb_user where profession = '软件工程' and age - 31 and status = 0;
explain select * from tb_user where phone = 12345678998;
- 模糊查询
如果仅仅是尾部模糊匹配,索引不会失效。如果是头部模糊匹配,索引失效。
explain select * from tb_user where profession like '软件%';
explain select * from tb_user where profession like '%工程';
explain select * from tb_user where profession like '%工%';
- or连接的条件
用or分割开的条件,如果or前的条件中的列有索引,而后面的列中没有索引,那么涉及的索引都不会被用到。
explain select * from tb_user where id = 10 or age = 23;
explain select * from tb_user where phone='12345678998' or age = 23;
由于age没有索引,所以即使id、phone有索引也会失效。所以需要针对age也要建立索引.
create index idx_user_age on tb_user(age);
- 数据分布影响
如果MySQL评估使用索引比全表更慢,则不使用索引。
select * from tb_user where phone >= '12345678987';
select * from tb_user where phone >= '1234567899';
2.6.2SQL提示
SQL提示,是优化数据库的一个重要手段,简单来说,就是在SQL语句中加入一些人为的提示来达到优化操作的目的。
use index:
explain select * from tb_user use index(idx_user_pro) where profession='软件工程';
ignore index:
explain select * from tb_user ignore index(idx_user_pro) where profession='软件工程';
forece index:
explain select * from tb_user force index(idx_user_pro) where profession='软件工程';
2.6.3覆盖索引
尽量使用覆盖索引(查询使用了索引,并且需要返回的列,在该索引中已经全部能够找到),减少了select.
explain select id,profession from tb_user where profession='软件工程' and age =31 and status='0';
explain select id, profession,age,status from tb_user where profession='软件工程' and age = 21 and status = '0';
explain select * from tb_user where profession = '软件工程' and age = 31 and status = '0';
注:
using index condition :查找使用了索引,但是需要回表查询数据
using where;using index:查找使用了索引,但是需要的数据都在索引中能找到,所以不需要回表查询数据
2.6.4前缀索引
当字段类型为字符串(varcher,text等)时,有时候需要索引很长的字符串,这灰让索引变得很大,查询时,浪费大量的磁盘IO,响应查询效率,此时可以只将字符串的一部分前缀,建立索引,这样可以大大节约索引空间,从而提高索引效率。
create index idx_xxxxx on table_name(column(n));
前缀长度
可以根据索引的选择性来决定,而选择性是指不重复的索引值(基数)和数据表的记录总数的比值,索引选择性越高则查询效率越高,
唯一索引的选择性是1,这是最好的索引选择性,性能也是最好的
select count(no) from student;
select count(distinct no) from student;
select count(distinct substring(no,1,10)/count(*) from student;
2.6.5单列索引与联合索引
单列索引:即一个索引只包含单个列。
联合索引:即一个索引包包含了多个列。
在业务场景中,如果存在多个查询条件,考虑对于查询字段建立索引时,建立联合索引,而非单列索引。
单列索引情况:
explain select id,phone,name from tb_user where phone = '17777999023' and name = '韩信';
多条件联合查询时,MySQL优化器会评估哪个字段的索引效率更高,会选择该索引完成本次查询
2.7索引设计原则
1.针对于数据量较大,且查询比较频繁的表建立索引。
2.针对于常作为查询条件(where)、排序(order by)、分组(group by)操作的字段建立索引。
3.尽量选择分区度高的列作为索引,尽量建立唯一索引,区分度高,使用索引的效率更高。
4.如果是字符串类型的字段,字段的长度比较长,可以针对字段的特点,建立前缀索引
5.尽量使用联合索引,减少单列索引,查询时,联合索引很多时候可以覆盖索引,节省存储空间,避免回表,提高查询效率
6.要控制索引的数量,索引并不是多多益善,索引越多,维护索引结构的代价也就会越大,会影响增删改查的效率。
7.如果索引列不能存储NULL值,请在创建表使用NOT NOTT约束它,当优化器知道每列是否包含NULL值时,它可以更好地确定哪个索引最有效地用于查询。
三、SQL优化
3.1插入数据
insert优化
批量插入
insert into tb_test values(1,"Tom"),(2,"Car"),(3,"Jerry");
手动提交事务
start transaction;
insert into tb_test values(1,"Tom"),(2,"Car"),(3,"Jerry");
insert into tb_test values(4,"Tom"),(5,"Car"),(6,"Jerry");
commit;
主键顺序插入
主键乱序插入:8,1,9,4,2,6
主键顺序插入:1,2,3,4,7,9
大批量插入数据
如果一次性需要插入大批量数据,使用insert语句插入性能较低,此时可以使用Mysql数据库提供的load指令进行插入。操作如下:
#客户端连接服务端时,加上--local-infile
mysql --local-infile -u root -p
#设置全局参数local_infile为1,开启从本地加载文件导入数据的开关
set global local_infile=1
#执行load指令将准备好的数据,加载到表结构中
load data local infile '/root/load_user_100w_sort.sql' into table tb_user fields terminated by ',' lines terminated by '\n';
主键顺序插入性能高于乱序插入
3.2主键优化
数据组织方式
在InnoDB存储引擎中,表数据都是根据主键顺序组织存放的,这种存储方式的表称为索引组织表
页分裂
页可以为空,也可以填充一半,也可以填充100%,每个页包含了2-N行数据(如果一行数据多大,会行溢出)根据主键排列
叶合并
当删除一行记录时,实际上记录并没有被物理删除,只是被标记为删除并且它的空间变得允许被其他记录声明使用。
当页中的删除的记录达到MERGE_THRESHOLD(默认为页的50%),InnoDB会开始 寻找最靠近的页(前或后)看看是否可以将两个页合并以优化空间使用
MERGE_THRESHOLD:合并页的阈值,可以自己设置在创建表或者索引时指定。
主键设计原则:
满足业务需求的情况下,尽量降低主键的长度
插入数据时,尽量选择顺序插入,选择使用AUTO_INCREMENT自增主键
尽量不要使用UUID做主键或者是其他自然主键,如身份证号,
业务操作时,避免对主键的修改。
3.3order by优化
①Using filesort:通过表的索引或全表扫描,读取满足条件的数据行,然后再排序缓冲区sort buffer中完成操作,所有不是通过索引值直接返回排序结果的排序都叫FileSort排序
②Using indx:通过有序索引顺序扫描直接返回有序数据,这种情况即为using index,不需要额外排序,操作效率更高。
#没有创建索引时,根据age,phone进行排序
explain select id,age,phone from tb_user order by age,phone;
#创建索引
create index idx_user_age_phone_aa on tb_user(age,phone);
#创建索引后根据age,phone进行升序排序
explain select id,age,phone from tb_user order by age,phone;
#创建索引后,根据age,phone进行降序排序
explain select id,age,phone from tb_user order by age desc,phone desc;
#根据age,phone进行排序,一个升序,一个降序
explain select id,age,phone from tb_user order by age asc,phone desc;
#创建索引
create index idx_user_age_phone_ad on tb_user(age asc,phone desc);
#根据age,phone进行排序,一个升序,一个降序。
explain select id,age,phone from tb_user order by age asc,phone desc;
- 根据排序字段建立合适的索引,多字段排序时,也遵循最左前缀法则
- 尽量使用覆盖索引
- 多字段排序,一个升序一个降序,此时需要注意联合索引在创建时的规则(ASC/DESC)
- 如果不可避免的出现filesort,大数据量排序时,可以适当增大排序缓冲区大小sort_buffer_size(默认256k)
3.4group by优化
#执行分组操作,根据profession字段分组
explain select profession,count(*) from tb_user group by profession;
#创建索引
create index idx_user_pro_age_sta on tb_user(profession,age,status);
#执行分组操作,根据profession字段分组
explain select profession,count(*) from tb_user group by profession;
#执行分组操作,根据profession字段分组(满足最左前缀)
explain select profession,count(*) from tb_user group by profession age;
3.5limit优化
如果查询limit 2000000,10,此时需要MySQL排序前20000010记录,仅仅返回2000000-2000010的记录,其他记录丢失,代价非常大
优化思路:一般分页查询时,通过创建覆盖索引能够比较好地提高性能,可以通过覆盖索引加子查询形式进行优化。
explain select * from tb_sku t,(select id from tb_sku order by id limit 2000000,10) a where t.id = a.id;
3.6count优化
3.7update优化
update student set no = '20000' where id = 1;
update student set no = '20001' where name = '战三';
InnoDB的行锁是针对索引加的锁,不是针对记录加的锁,并且该索引不能失效,否则会从行锁升级为表锁
四、视图/存储过程/触发器
4.1视图
#创建视图
create or replace view stu_v_1 as select id,name from student;
#查询视图
select * from stu_v_1;
#修改视图
create or replace view stu_v_1 as where id,name,no from student id <= 10;
alter view stu_v_1 as select id,name from student where id <= 10;
#删除视图
drop view if exists stu_v_1;
视图的检查选项
当使用WITH CHECK OPTION子句创建视图时,MySQL会通过视图检查正在更改的每个行,例如插入,更新,检查,以使其符合视图的定义。MySQL允许基于另一个视图创建视图,它还会检查依赖视图中的规则以保持一致性。为了确定检查的范围,mysql提供了两个选项:
CASCADED和LOCAL,默认值为CASCADED.
CASCASED:
-- Cascaded
create or replace view v1 as select id, name from user where id <=20;
#成功
insert into v1 values (5, 'AA');
#成功
insert into v1 values (25, 'AA');
create or replace view v2 as select id,name from v1 where id >= 10 with cascaded check option ;
insert into v2 (id, name) values (11,'bb');
#失败
insert into v2 (id, name) values (7,'bb');
create or replace view v3 as select id, name from user where id <=15;
insert into v2 (id, name) values (11,'bb');
insert into v2 (id, name) values (17,'bb');
#失败
insert into v2 (id, name) values (7,'bb');
LOCAL:
-- Cascaded
create or replace view vv as select id, name from user where id <=20;
#成功
insert into v1 values (5, 'AA');
#成功
insert into v1 values (25, 'AA');
create or replace view v5 as select id,name from v1 where id >= 10 with local check option ;
insert into v2 (id, name) values (13,'bb');
insert into v2 (id, name) values (17,'bb');
create or replace view v6 as select id, name from user where id <=20;
insert into v2 (id, name) values (14,'bb');
视图的更新
要使视图可更新,视图中的行与基础表中的行直接必须存在一对一的关系。如果视图包含以下任何一项,则该视图不可更新:
1.聚合函数或窗口函数(SUM()、MIN()、MAX()、COUNT()等)
2.DISTINCT
3.GROUP BY
4.HAVING
5.UNION或者UNION ALL
- 作用
简单
视图不仅可以简化用户对数据的理解,可以简化他们的操作。那些经常使用的查询可以被定义为视图,从而使得用户不必为以后的操作每次指定全部的条件
安全
数据库可以授权,但不能授权到数据库特定行和特定的列上。通过视图用户只能查询和修改他们能见到的数据
数据独立
视图可帮助用户屏蔽真实表结构变化带来的影响。
4.2存储过程
介绍
存储过程是事先经过编译并存储在数据库中的一段SQL语句的集合,调用存储过程可以简化应用开发人员的很多工作,减少数据在数据库和应用服务器之间的传输,对于提高数据处理的效率是有好处的
存储过程思想上很简单,就是数据库SQL语言层面的代码封装与重用
- 特点
封装,复用
可以接受参数,也可以返回数据,减少网络交互,效率提升
-- 创建
create procedure p1()
begin
select count(*) from student;
end;
-- 调用
call p1();
-- 查看
-- 查询指定数据库的存储过程及状态信息
select * from information_schema.routines where routine_schema='mysql_study';
show create procedure p1;-- 查询某个存储过程的定义
-- 删除
drop procedure [if exists] p1;
注意:在命令行中,执行创建存储过程的SQL时,需要关键字delimiter指定SQL语句的结束符。
4.2.1变量
- 系统变量
-- 变量:系统变量
-- 查看系统变量
show [session|global] variables ;
-- 可以通过模糊匹配方式查找变量
show [session|global] variables like 'auto%';
show [session|global] variables like 'auto%';
-- 查看指定变量的值
select @@session.autocommit;
-- 设置系统变量
-- 会话
set session autocommit = 0;
-- 全局
set global autocommit = 0;
注意:如果没有指定SESSION/GLOBAL,默认是SESSION,会话变量。
mysql服务重新启动之后,所设置的全局参数会失效,要想不失效,可以在/etc/my.cnf中配置。
- 用户变量
用户定义变量是用户根据需要自己定义的变量,用户变量不用提前声明,在用的时候直接用"@变量名"使用就可以,其作用域为当前连接。
-- 赋值
set @myname = 'itcast';
set @myage := 10;
set @mygender := '男',@myhobby := 'java';
select count(*) into @mycount from student;
-- 使用
select @myname,@myage,@mygender;
select @mycount;
注意:用户定义的变量无需对其声明或者初始化,只不过获取的值为NULL.
- 局部变量
局部变量是根据需要定义的在局部生效的变量,访问之前,需要DECLARE声明,可用作存储过程内的局部变量和输入参数,局部变量的范围是在其内声明的BEGIN...END块
-- 声明
create procedure p2()
begin
declare stu_count int default 0;
#set stu_count := 100;
select count(*) into stu_count from student;
select stu_count;
end;
call p2();
4.2.2if
create procedure p3()
begin
declare score int default 58;
declare result varchar(10);
if score > 85 then
set result := '优秀 ';
elseif score >= 60 then
set result := '及格';
else
set result := '不及格';
end if;
select result;
end;
call p3;
4.2.3参数
用法
-- 根据传入的参数score,判断当前分数等级,并返回
create procedure p4(in score int, out result varchar(10))
begin
if score > 85 then
set result := '优秀 ';
elseif score >= 60 then
set result := '及格';
else
set result := '不及格';
end if;
end;
call p4(60,@result);
select @result;
-- 将传入的200分制的分数,进行换算,换算成百分制,然后返回分数 --> inout
create procedure p5(inout score double)
begin
set score := score * 0.5;
end;
set @score = 78;
call p5(@score);
select @score;
-
case
create procedure p6(in month int)
begin
declare result varchar(10);
case
when month >= 1 and month <= 3 then
set result := '第一季度';
when month >= 4 and month <= 6 then
set result := '第二季季度';
when month >= 7 and month <= 9 then
set result := '第三季度';
when month >= 10 and month <= 12 then
set result := '第四季度';
else
set result := '非法参数';
end case;
select concat('您输入的月份为:',month,',所属的季节为:',result);
end;
call p6(1);
- while
-- 定义局部变量记录累加之后的值
-- 每循环一次,就会对n减1,如果n为0,则退出循环
create procedure p7(in n int)
begin
declare total int default 0;
while n>0 do
set total = total + n;
set n = n-1;
end while;
select total;
end;
call p7(10);
- repeat
create procedure p8(in n int)
begin
declare total int default 0;
repeat
set total := total + n;
set n := n - 1;
until n <= 0
end repeat;
select total;
end;
call p8(10);
- loop
loop实现简单的循环,如果不在SQL逻辑中增加退出循环的条件,可用来实现简单的死循环。LOOP可以配合一下两个语句使用:
LEAVE:配合循环使用,退出循环
ITERATE:必须用在循环中,作用是跳过当前循环剩下的语句,直接进到下一次循环
create procedure p9(in n int)
begin
declare total int default 0;
sum:loop
if n <= 0 then
leave sum;
end if;
set total := total + n;
set n := n - 1;
end loop;
select total;
end;
call p9(10);
create procedure p10(in n int)
begin
declare total int default 0;
sum:loop
if n <= 0 then
leave sum;
end if;
if n%2 = 1 then
set n := n -1;
iterate sum;
end if;
set total := total + n;
set n := n - 1;
end loop;
select total;
end;
call p10(10);
- 游标
- 条件处理程序
条件处理程序可以用来定义在流程控制结构中遇到问题时相应的处理步骤。具体语法为:
create procedure p11(in uage int)
begin
declare uname varchar(100);
declare usex varchar(100);
declare u_cursor cursor for select name,sex from emp where age <= uage;
declare exit handler for SQLSTATE '02000' close u_cursor;
drop table if exists tb_emp_name;
create table if not exists tb_emp_name(
id int primary key auto_increment,
name varchar(100),
sex varchar(12)
);
open u_cursor;
while true do
fetch u_cursor into uname,usex;
insert into tb_emp_name values (null,uname,usex);
end while;
close u_cursor;
end;
call p11(20);
4.3存储函数
存储函数是有返回值的存储过程,存储函数的 参数只能是IN类型的,具体语法如下:
4.4触发器
触发器是与表有关的数据库对象,指在insert/update/delete之前或之后,触发并执行触发器中定义的SQL语句集合。触发器的这种特性可以协助应用在数据库端确保数据的完整性,日志记录,数据校验等操作。
使用别名OLD和NEW来引用触发器中发生变化的记录内容,这与其他的数据库是相似的。现在触发器还只支持行级触发,不支持语句级触发。
语法
创建
CREATE TRIGGER trigger_name
BEFORE/AFTER INSERT/UPDATE/DELETE
ON table_name FOR EACH ROW --行级触发器
BEGIN
trigger_stmt:
END;
查看
SHOW TRIGGERS;
删除
DROP TRIGGER [schema_name] trigger_name;--如果没有指定schema_name,默认为当前数据库。
create table user_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 user_insert_trigger
after insert on user for each row
begin
insert into user_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,',salart=',salary,',job=',new.job));
end;
show triggers;
-- 删除
drop trigger user_insert_trigger;
修改数据触发器
-- 修改数据触发器
create trigger tb_user_update_trigger
after update on tb_user for each row
begin
insert into user_logs(id, operation, operate_time, operate_id, operate_params) VALUES
(null, 'update', now(), new.id,
concat('更新之前的数据: id=',old.id,',name=',old.name, ', phone=', old.phone, ', email=', old.email, ', profession=', old.profession,
' | 更新之后的数据: id=',new.id,',name=',new.name, ', phone=', NEW.phone, ', email=', NEW.email, ', profession=', NEW.profession));
end;
show triggers ;
update tb_user set profession = '会计' where id = 23;
update tb_user set profession = '会计' where id <= 5;
删除触发器
-- 删除数据触发器
create trigger tb_user_delete_trigger
after delete on tb_user for each row
begin
insert into user_logs(id, operation, operate_time, operate_id, operate_params) VALUES
(null, 'delete', now(), old.id,
concat('删除之前的数据: id=',old.id,',name=',old.name, ', phone=', old.phone, ', email=', old.email, ', profession=', old.profession));
end;
show triggers ;
delete from tb_user where id = 26;