类似于python中的自定义函数
delimiter 临时结束符
create procedure 名字(参数,参数)
begin
sql语句;
end 临时结束符
delimiter ;
delimiter $$
create procedure p1(in m int,# in表示这个参数必须只能是传入不能被返回出去in n int,
out res int# out表示这个参数可以被返回出去,还有一个inout表示即可以传入也可以被返回出去)
begin
select tname from teacher where tid > m and tid < n;set res=0;# 用来标志存储过程是否执行
end $$
delimiter ;# 针对res需要先提前定义set @res=10; 定义
select @res; 查看
call p1(1,5,@res) 调用
select @res 查看
"""
查看存储过程具体信息
show create procedure pro1;
查看所有存储过程
show procedure status;
删除存储过程
drop procedure pro1;
"""
触发器与存储过程的不同
相同点:1. 触发器是一种特殊的存储过程,触发器和存储过程一样是一个能够完成特定功能、存储在数据库服务器上的SQL片段。
不同点:2. 存储器调用时需要调用SQL片段,而触发器不需要调用,当对数据库表中的数据执行DML操作时自动触发这个SQL片段的执行,无需手动调用。
内置函数
"ps:可以通过help 函数名 查看帮助信息!"# 1.移除指定字符
Trim、LTrim、RTrim
# 2.大小写转换
Lower、Upper
# 3.获取左右起始指定个数字符
Left、Right
# 4.返回读音相似值(对英文效果)
Soundex
"""
eg:客户表中有一个顾客登记的用户名为J.Lee
但如果这是输入错误真名其实叫J.Lie,可以使用soundex匹配发音类似的
where Soundex(name)=Soundex('J.Lie')
"""# 5.日期格式:date_format'''在MySQL中表示时间格式尽量采用2022-11-11形式'''
CREATE TABLE blog (id INT PRIMARY KEY auto_increment,
NAME CHAR (32),
sub_time datetime
);
INSERT INTO blog (NAME, sub_time)
VALUES
('第1篇','2015-03-01 11:31:21'),('第2篇','2015-03-11 16:31:21'),('第3篇','2016-07-01 10:21:31'),('第4篇','2016-07-22 09:23:21'),('第5篇','2016-07-23 10:11:11'),('第6篇','2016-07-25 11:21:31'),('第7篇','2017-03-01 15:33:21'),('第8篇','2017-03-01 17:32:21'),('第9篇','2017-03-01 18:31:21');
select date_format(sub_time,'%Y-%m'),count(id)from blog group by date_format(sub_time,'%Y-%m');1.where Date(sub_time)='2015-03-01'2.where Year(sub_time)=2016 AND Month(sub_time)=07;# 更多日期处理相关函数
adddate 增加一个日期
addtime 增加一个时间
datediff 计算两个日期差值
流程控制
# if条件语句
delimiter //
CREATE PROCEDURE proc_if ()
BEGIN
declare i int default 0;if i =1 THEN
SELECT 1;
ELSEIF i =2 THEN
SELECT 2;
ELSE
SELECT 7;
END IF;
END //
delimiter ;# while循环
delimiter //
CREATE PROCEDURE proc_while ()
BEGIN
DECLARE num INT ;
SET num =0;
WHILE num <10 DO
SELECT
num ;
SET num = num +1;
END WHILE ;
END //
delimiter ;
索引
1)索引就好比一本书的目录,它能让你更快的找到自己想要的内容。
2)让获取的数据更有目的性,从而提高数据库检索数据的性能
索引在MySQL中也叫做“键”,是存储引擎用于快速找到记录的一种数据结构
primary key
unique key
index key
上述的三种键在数据查询的时候使用都可以加快查询的速度
primary key、unique key除了可以加快数据查询还有额外的限制
index key只能加快数据查询 本身没有任何的额外限制
真正理解索引加快数据查询的含义
索引的存在可以加快数据的查询 但是会减慢数据的增删
#1. 准备表
create table s1(idint,
name varchar(20),
gender char(6),
email varchar(50));#2. 创建存储过程,实现批量插入记录
delimiter $$ #声明存储过程的结束符号为$$
create procedure auto_insert1()
BEGIN
declare i int default 1;while(i<3000000)do
insert into s1 values(i,'jason','male',concat('jason',i,'@oldboy'));set i=i+1;
end while;
END$$ #$$结束
delimiter ;#重新声明分号为结束符号#3. 查看存储过程
show create procedure auto_insert1\G
#4. 调用存储过程
call auto_insert1();# 表没有任何索引的情况下
select *from s1 where id=30000;# 避免打印带来的时间损耗
select count(id)from s1 where id=30000;
select count(id)from s1 where id=1;# 给id做一个主键
alter table s1 add primary key(id);# 速度很慢
select count(id)from s1 where id=1;# 速度相较于未建索引之前两者差着数量级
select count(id)from s1 where name ='jason'# 速度仍然很慢"""
范围问题
"""# 并不是加了索引,以后查询的时候按照这个字段速度就一定快
select count(id)from s1 where id>1;# 速度相较于id = 1慢了很多
select count(id)from s1 where id>1andid<3;
select count(id)from s1 where id>1andid<10000;
select count(id)from s1 where id!=3;
alter table s1 drop primary key;# 删除主键 单独再来研究name字段
select count(id)from s1 where name ='jason';# 又慢了
create index idx_name on s1(name);# 给s1表的name字段创建索引
select count(id)from s1 where name ='jason'# 仍然很慢!!!"""
再来看b+树的原理,数据需要区分度比较高,而我们这张表全是jason,根本无法区分
那这个树其实就建成了“一根棍子”
"""
select count(id)from s1 where name ='xxx';# 这个会很快,我就是一根棍,第一个不匹配直接不需要再往下走了
select count(id)from s1 where name like 'xxx';
select count(id)from s1 where name like 'xxx%';
select count(id)from s1 where name like '%xxx';# 慢 最左匹配特性# 区分度低的字段不能建索引
drop index idx_name on s1;# 给id字段建普通的索引
create index idx_id on s1(id);
select count(id)from s1 where id=3;# 快了
select count(id)from s1 where id*12=3;# 慢了 索引的字段一定不要参与计算
drop index idx_id on s1;
select count(id)from s1 where name='jason'and gender ='male'andid=3and email ='xxx';# 针对上面这种连续多个and的操作,mysql会从左到右先找区分度比较高的索引字段,先将整体范围降下来再去比较其他条件
create index idx_name on s1(name);
select count(id)from s1 where name='jason'and gender ='male'andid=3and email ='xxx';# 并没有加速
drop index idx_name on s1;# 给name,gender这种区分度不高的字段加上索引并不难加快查询速度
create index idx_id on s1(id);
select count(id)from s1 where name='jason'and gender ='male'andid=3and email ='xxx';# 快了 先通过id已经讲数据快速锁定成了一条了
select count(id)from s1 where name='jason'and gender ='male'andid>3and email ='xxx';# 慢了 基于id查出来的数据仍然很多,然后还要去比较其他字段
drop index idx_id on s1
create index idx_email on s1(email);
select count(id)from s1 where name='jason'and gender ='male'andid>3and email ='xxx';# 快 通过email字段一剑封喉
联合索引
select count(id)from s1 where name='jason'and gender ='male'andid>3and email ='xxx';# 如果上述四个字段区分度都很高,那给谁建都能加速查询# 给email加然而不用email字段
select count(id)from s1 where name='jason'and gender ='male'andid>3;# 给name加然而不用name字段
select count(id)from s1 where gender ='male'andid>3;# 给gender加然而不用gender字段
select count(id)from s1 where id>3;# 带来的问题是所有的字段都建了索引然而都没有用到,还需要花费四次建立的时间
create index idx_all on s1(email,name,gender,id);# 最左匹配原则,区分度高的往左放
select count(id)from s1 where name='jason'and gender ='male'andid>3and email ='xxx';# 速度变快
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)