活动地址:CSDN21天学习挑战赛
作者简介:大家好我是狂暴于涛侠本侠
🦸个人主页:狂暴于涛侠
目录
🥭MySQL的触发器
🍒概述
-
介绍:
-
触发器,就是一种特殊的存储过程。触发器和存储过程一样是一个能够完成特定功能、存储在数据库服务器上的SQL片段,但是触发器无需调用,当对数据库表中的数据执行DML操作时自动触发这个SQL片段的执行,无需手动条用。
-
在MySQL中,只有执行insert,delete,update操作时才能触发触发器的执行
-
触发器的这种特性可以协助应用在数据库端确保数据的完整性 , 日志记录 , 数据校验等操作 。
-
使用别名 OLD 和 NEW 来引用触发器中发生变化的记录内容,这与其他的数据库是相似的。现在触发器还只支持行级触发,不支持语句级触发。
-
触发器的特性:
-
1、什么条件会触发:I(insert)、D(delete)、U(update)
-
2、什么时候触发:在增删改前或者后
-
3、触发频率:针对每一行执行
-
4、触发器定义在表上,附着在表上
🍒操作-创建触发器
格式
1、创建只有一个执行语句的触发器
create trigger触发器名 before|after 触发事件
on 表名 for each row
执行语句;2、创建有多个执行语句的触发器
create trigger 触发器名 before|after 触发事件
on 表名 for each row
begin
执行语句列表
end;
操作
--数据准备
create database if not exists mydb10_trigger;
use mydb10_trigger;
-- 用户表
create table user(
uid int primary key ,
username varchar(50) not null,
password varchar( 50) not null
);
-- 用户信息操作日志表
create table user_logs(
id int primary key auto_increment,
time timestamp,
log_text varchar(255)
);
-- 需求1:当user表添加一行数据,则会自动在user_log添加日志记录
-- 定义触发器:trigger_test1
create trigger trigger_test1 after insert
on user for each row
insert into user_logs values(NULL,now(),'有新用户添加');-- now()当前时间
-- 在user表添加数据,让触发器自动执行
insert into user values(1,'张三','123456');
-- 需求2:当user表数据被修改时,则会自动在user_log添加日志记录
delimiter $$
create trigger trigger_test2 before update
on user for each row
begin
insert into user_logs values(NULL, now(), '有用户信息被修改');
end $$
delimiter;
-- 在user表中修改数据,让触发器自动执行
update user set password = '888888' where uid = 1;
update user set password = '86666' where uid = 1;
🍒操作-NEW与OLD
格式:
MySQL 中定义了 NEW 和 OLD,用来表示触发器的所在表中,触发了触发器的那一行数据,来引用触发器中发生变化的记录内容,具体地:
使用方法:
NEW.columnName (columnName为相应数据表某一列名)
-- NEW和IOLD
-- insert类型的触发器
-- NEw
-- 定义触发器:trigger_test3
drop trigger if exists trigger_test3;-- 删除触发器trigger_test3
create trigger trigger_test3 after insert
on user for each row
insert into user_logs values(NULL,now(),concat('有新用户添加信息为:',NEW.uid,NEW.username,NEW.password));
insert into user values(4,'赵六', '123456');
-- update类型的触发器
-- OLD
drop trigger if exists trigger_test4;
create trigger trigger_test4 after update
on user for each row
insert into user_logs values(NULL, now(),concat('有用户信息修改,信息修改之前为:',OLD.uid,OLD.username,OLD.password));
update user set password = '999999' where uid = 4;
-- update类型的触发器
-- NEW
drop trigger if exists trigger_test5;
create trigger trigger_test5 after update
on user for each row
insert into user_logs values(NuLL,now(),concat_ws(',', '有用户信息修改,信息修改之后为:',NEW.uid,NEW.username,NEW.password));
update user set password = '000000' where uid =1;
-- delete类型触发器
-- OLD
drop trigger if exists trigger_test6;
create trigger trigger_test6 after delete
on user for each row
insert into user_logs values(NULL,now(),concat_ws(',', '有用户信息被删除,被删除信息用户为::',old.uid,old.username,old.password));
delete from user where uid = 4;
🍒操作-查看触发器
show triggers;
🍒操作-删除触发器
– drop trigger [if exists] trigger_name
drop trigger if exists trigger_test1;
🍒注意事项:
-
1.MYSQL中触发器中不能对本表进行 insert ,update ,delete 操作,以免递归循环触发
-
2.尽量少使用触发器,假设触发器触发每次执行1s,insert table 500条数据,那么就需要触发500次触发器,光是触发器执行的时间就花费了500s,而insert 500条数据一共是1s,那么这个insert的效率就非常低了。
-
3.触发器是针对每一行的;对增删改非常频繁的表上切记不要使用触发器,因为它会非常消耗资源。
🥭MySQL的索引
介绍:
索引是通过某种算法,构建出一个数据模型,用于快速找出在某个列中有一特定值的行,不使用索引,MySQL必须从第一条记录开始读完整个表,直到找出相关的行,表越大,查询数据所花费的时间就越多,如果表中查询的列有一个索引,MySQL能够快速到达一个位置去搜索数据文件,而不必查看所有数据,那么将会节省很大一部分时间。
索引类似一本书的目录,比如要查找’student’这个单词,可以先找到s开头的页然后向后查找,这个就类似索引。
索引的分类:
索引是存储引擎用来快速查找记录的一种数据结构,按照实现的方式类分,主要有Hash索引和B+Tree索引
Hash索引:
B+Tree索引:
按照功能划分,索引划为以下分类:
🍍索引的操作-创建索引-单列索引-普通索引
介绍
-
单列索引:一个索引只包含单个列,但一个表中可以有多个单列索引;
-
普通索引:MySQL中基本索引类型,没有什么限制,允许在定义索引的列中插入重复值和空值,纯粹为了查询数据更快一点。
单列索引格式:
create database mydb5;
use mydb5;
-- 方式1-创建表的时候直接指定
create table student(
sid int primary key,
card_id varchar(20),
name varchar(20),
gender varchar(20),
age int,
birth date,
phone_num varchar(20),
score double,
index index_name(name) -- 给name列创建索引
);
-- 方式2-直接创建
-- create index indexname on tablename(columnname);
create index index_gender on student(gender);
-- 方式3-修改表结构(添加索引)
-- alter table tablename add index indexname(columnname)
alter table student add index index_age(age);
-- 1、查看数据库所有索引
-- select * from mysql.`innodb_index_stats` a where a.`database_name` = '数据库名’;
select * from mysql.`innodb_index_stats` a where a.`database_name` = 'mydb5';
-- 2、查看表中所有索引
-- select * from mysql.`innodb_index_stats` a where a.`database_name` = '数据库名' and a.table_name like '%表名%’;
select * from mysql.`innodb_index_stats` a where a.`database_name` = 'mydb5' and a.table_name like '%student%';
-- 3、查看表中所有索引
-- show index from table_name;
show index from student;
索引的操作-删除索引
格式
drop index 索引名 on 表名
– 或
alter table 表名 drop index 索引名
-- 操作
drop index index_gender on student
-- 或
alter table student drop index index_name
🍍索引的操作-创建索引-单列索引-唯一索引
介绍:
- 唯一索引与前面的普通索引类似,不同的就是:索引列的值必须唯一,但允许有空值。如果是组合索引,则列值的组合必须唯一。它有以下几种创建方式:
-- 方式1-创建表的时候直接指定
create table student2(
sid int primary key,
card_id varchar(20),
name varchar(20),
gender varchar(20),
age int,
birth date,
phone_num varchar(20),
score double,
unique index_card_id(card_id) -- 给card_id列创建索引
);
-- 方式2-直接创建
-- create unique index 索引名 on 表名(列名)
create unique index index_card_id on student2(card_id);
-- 方式3-修改表结构(添加索引)
-- alter table 表名 add unique [索引名] (列名)
alter table student2 add unique index_phone_num(phone_num);
索引的操作-删除索引
和上面一样
格式
drop index 索引名 on 表名
– 或
alter table 表名 drop index 索引名
-- 操作
drop index index_card_id on student2;
-- 或
alter table student2 drop index index_phone_num;
🍍索引的操作-创建索引-单列索引-主键索引
介绍
每张表一般都会有自己的主键,当我们在创建表时,MySQL会自动在主键列上建立一个索引,这就是主键索引。主键是具有唯一性并且不允许为NULL,所以他是一种特殊的唯一索引。
show index from student2;
🍍索引的操作-创建索引-组合索引
介绍
- 组合索引也叫复合索引,指的是我们在建立索引的时候使用多个字段,例如同时使用身份证和手机号建立索引,同样的可以建立为普通索引或者是唯一索引。
- 复合索引的使用复合最左原则。
格式
– 创建索引的基本语法
create index indexname on table_name(column1(length),column2(length));
操作:
-- 组合索引
use mydb5;
-- 创建索引的基本语法-- 普通索引
-- create index indexname on table_name(column1(length),column2(length));
create index index_phone_name on student(phone_num,name);
-- 操作-删除索引
drop index index_phone_name on student;
-- 创建索引的基本语法-- 唯一索引
create unique index index_phone_name on student(phone_num,name);
select * from student where name = '张三'; -- 先匹配电话,所以不行
select * from student where phone_num = '15100046637'; -- 可以,第二部才匹配name
select * from student where phone_num = '15100046637' and name = '张三'; -- 可以
select * from student where name = '张三' and phone_num = '15100046637'; -- 可以 and 这里可以理解为或者
/*
三条sql只有 2 、 3、4能使用的到索引idx_phone_name,因为条件里面必须包含索引前面的字段 才能够进行匹配。
而3和4相比where条件的顺序不一样,为什么4可以用到索引呢?是因为mysql本身就有一层sql优化,他会根据sql来识别出来该用哪个索引,我们可以理解为3和4在mysql眼中是等价的。
*/
🍍索引的操作-全文索引
概述
-
全文索引的关键字是fulltext
-
全文索引主要用来查找文本中的关键字,而不是直接与索引中的值相比较,它更像是一个搜索引擎,基于相似度的查询,而不是简单的where语句的参数匹配。
-
用 like + % 就可以实现模糊匹配了,为什么还要全文索引?like + % 在文本比较少时是合适的,但是对于大量的文本数据检索,是不可想象的。全文索引在大量的数据面前,能比 like + % 快 N 倍,速度不是一个数量级,但是全文索引可能存在精度问题。
全文索引的版本、存储引擎、数据类型的支持情况:
-
MySQL 5.6 以前的版本,只有 MyISAM 存储引擎支持全文索引;
-
MySQL 5.6 及以后的版本,MyISAM 和 InnoDB 存储引擎均支持全文索引;
-
只有字段的数据类型为 char、varchar、text 及其系列才可以建全文索引;
-
在数据量较大时候,现将数据放入一个没有全局索引的表中,然后再用create index创建fulltext索引,要比先为一张表建立fulltext然后再将数据写入的速度快很多;
-
测试或使用全文索引时,要先看一下自己的 MySQL 版本、存储引擎和数据类型是否支持全文索引。
MySQL 中的全文索引,有两个变量,最小搜索长度和最大搜索长度,对于长度小于最小搜索长度和大于最大搜索长度的词语,都不会被索引。通俗点就是说,想对一个词语使用全文索引搜索,那么这个词语的长度必须在以上两个变量的区间内。这两个的默认值可以使用以下命令查看:
show variables like '%ft%';
-- 1.创建表的时候添加全文索引
create table t_article (
id int primary key auto_increment ,
title varchar(255) ,
content varchar(1000) ,
writing_date date ,
fulltext (content) -- 创建表的同时创建全文索引
);
-- 2.创建表后添加全文索引--推荐
create table t_article (
id int primary key auto_increment ,
title varchar(255) ,
content varchar(1000) ,
writing_date date -- ,
-- fulltext (content) -- 创建全文检索
);
insert into t_article values(null,"Yesterday Once More","When I was young I listen to the radio",'2021-10-01');
insert into t_article values(null,"Right Here Waiting","Oceans apart, day after day,and I slowly go insane",'2021-10-02');
insert into t_article values(null,"My Heart Will Go On","every night in my dreams,i see you, i feel you",'2021-10-03');
insert into t_article values(null,"Everything I Do","eLook into my eyes,You will see what you mean to me",'2021-10-04');
insert into t_article values(null,"Called To Say I Love You","say love you no new year's day, to celebrate",'2021-10-05');
insert into t_article values(null,"Nothing's Gonna Change My Love For You","if i had to live my life without you near me",'2021-10-06');
insert into t_article values(null,"Everybody","We're gonna bring the flavor show U how.",'2021-10-07');
-- 修改表结构添加全文索引
alter table t_article add fulltext index_content(content)
-- 直接添加全文索引
create fulltext index index_content on t_article(content);
操作-使用索引
使用全文索引
和常用的模糊匹配使用 like + % 不同,全文索引有自己的语法格式,使用 match 和 against 关键字
格式:
match (col1,col2,…) against(expr [search_modifier])
select * from t_article where match(content) against('yo'); -- 没有结果 单词数需要大于等于3
select * from t_article where match(content) against('you'); -- 有结果
🍍索引的操作-空间索引
介绍
- MySQL在5.7之后的版本支持了空间索引,而且支持OpenGIS几何数据模型
- 空间索引是对空间数据类型的字段建立的索引,MYSQL中的空间数据类型有4种,分别是GEOMETRY、POINT、LINESTRING、POLYGON。
- MYSQL使用SPATIAL关键字进行扩展,使得能够用于创建正规索引类型的语法创建空间索引。
- 创建空间索引的列,必须将其声明为NOT NULL。
- 空间索引一般是用的比较少,了解即可。
操作:
创建空间索引案例:
create table shop_info (
id int primary key auto_increment comment 'id',
shop_name varchar(64) not null comment '门店名称',
geom_point geometry not null comment '经纬度',
spatial key geom_index(geom_point)
);
🍍索引的原理-概述
- 一般来说,索引本身也很大,不可能全部存储在内存中,因此索引往往以索引文件的形式存储的磁盘上。
- 这样的话,索引查找过程中就要产生磁盘I/O消耗,相对于内存存取,I/O存取的消耗要高几个数量级,所以评价一个数据结构作为索引的优劣最重要的指标就是在查找过程中磁盘I/O操作次数的渐进复杂度。
- 换句话说,索引的结构组织要尽量减少查找过程中磁盘I/O的存取次数。
🍍索引的特点
索引的优点
-
大大加快数据的查询速度
-
使用分组和排序进行数据查询时,可以显著减少查询时分组和排序的时间
-
创建唯一索引,能够保证数据库表中每一行数据的唯一性
-
在实现数据的参考完整性方面,可以加速表和表之间的连接
索引的缺点
-
创建索引和维护索引需要消耗时间,并且随着数据量的增加,时间也会增加
-
索引需要占据磁盘空间
-
对数据表中的数据进行增加,修改,删除时,索引也要动态的维护,降低了维护的速度
创建索引的原则
-
更新频繁的列不应设置索引
-
数据量小的表不要使用索引(毕竟总共2页的文档,还要目录吗?)
-
重复数据多的字段不应设为索引(比如性别,只有男和女,一般来说:重复的数据超过百分之15就不该建索引)
-
首先应该考虑对where 和 order by 涉及的列上建立索引
🥭索引的原理-相关的算法
🍈Hash算法
🍈二叉树
特性:分为左子树、右子树和根节点,左子树比根节点值要小,右子树比根节点值要大
缺点:有可能产生不平衡 类似于链表的结构 。
🍈平衡二叉树
特点:
a、它的左子树和右子树都是平衡二叉树
b、左子树比中间小,右子树比中间值大
c、左子树和右子树的深度之差的绝对值不超过1
缺点:
a、插入操作需要旋转
b、支持范围查询,但回旋查询效率较低,比如要查找大于8的,会回旋到父节点7、10。
c、如果存放几百条数据的情况下,树高度越高,查询效率会越慢
🍈BTREE树
Btree |
B+Tree |
目前大部分数据库系统及文件系统都采用B-Tree或其变种B+Tree作为索引结构,Btree结构可以有效的解决之前的相关算法遇到的问题。
🍈MyISAM引擎使用B+Tree
MyISAM引擎使用B+Tree作为索引结构,叶节点的data域存放的是数据记录的地址。
🍈InnoDB引擎使用B+Tree
InnoDB的叶节点的data域存放的是数据,相比MyISAM效率要高一些,但是比较占硬盘内存大小。