1视图:
-创建
create view 视图名称 as SQL
ps:虚拟
-修改
alter view 视图名称 as SQL
-删除
drop view 视图名称
-创建
create view 视图名称 as SQL
ps:虚拟
-修改
alter view 视图名称 as SQL
-删除
drop view 视图名称
2触发器
3自定义函数:
delimiter
create function f1(
i1 int,
i2 int)
returns int
BEGIN
declare num int default 0;
set num = i1 + i2
return (num);
delimiter;
select f1(1,100);
)
4存储过程:
1.简单
Create procedure p1()
BEGIN
select * from student;
INSERT into teacher(tname) values('ct');
END
call p1()
cursor.callproc('p1')
2.传参数(in, out, inout)
delimiter//
create procedure p2(
in n1 int,
in n2 int
)
BEGIN
select * from student where sid > n1;
END //
delimiter;
call p2(12,2)
cursor.callproc('p2', (12,2))
3.参数 out
delimiter//
create procedure p2(
in n1 int,
out n2 int
)
BEGIN
set n2 = 123123;
select * from student where sid > n1;
END//
delimiter;
set @v1 = 0;
call p2(12,@v1)
@v1
4.事务
delimiter //
create procedure p4()
BEGIN
1.声明如果出现异常则执行{
set status = 1
rollback;
}
开始事务
-- 由a账户减去100
-- b 账户加90
-- c 账户加10
commit;
结束
set status = 2;
END//
delimiter;
3自定义函数:
delimiter
create function f1(
i1 int,
i2 int)
returns int
BEGIN
declare num int default 0;
set num = i1 + i2
return (num);
delimiter;
select f1(1,100);
)
4存储过程:
1.简单
Create procedure p1()
BEGIN
select * from student;
INSERT into teacher(tname) values('ct');
END
call p1()
cursor.callproc('p1')
2.传参数(in, out, inout)
delimiter//
create procedure p2(
in n1 int,
in n2 int
)
BEGIN
select * from student where sid > n1;
END //
delimiter;
call p2(12,2)
cursor.callproc('p2', (12,2))
3.参数 out
delimiter//
create procedure p2(
in n1 int,
out n2 int
)
BEGIN
set n2 = 123123;
select * from student where sid > n1;
END//
delimiter;
set @v1 = 0;
call p2(12,@v1)
@v1
4.事务
delimiter //
create procedure p4()
BEGIN
1.声明如果出现异常则执行{
set status = 1
rollback;
}
开始事务
-- 由a账户减去100
-- b 账户加90
-- c 账户加10
commit;
结束
set status = 2;
END//
delimiter;
================
delimiter\
create PROCEDURE p5(
OUT p_return_code tinyint
)
BEGIN
-- ERROR
set p_return_code = 1;
rollback;
END;
START TRANSACTION;
DELETE from tb1;
insert into tb2 (name) values('seven');
COMMIT;
-- SUCCESS
set p_return_code = 2;
END\
delimiter;
5.游标
delimiter//
create procedure p6()
begin
declare row_id int; --自定义变量1
declare row_num int; --自定义变量2
declare done INT DEFAULT FALSE;
declare temp int;
declare my_cursor CURSOR FOR select id,num from A;
declare CONTINUE HANDLER FOR NOT FOUND SET done = RRUE;
open my_cursor;
xxoo: LOOP
fetch my_cursor into row_id, row_num;
if done then
leave xxoo;
END IF;
set temp = row_id + row_num;
insert into B(number) values(temp);
end loop xxoo;
close my_cursor;
delimiter\
create PROCEDURE p5(
OUT p_return_code tinyint
)
BEGIN
-- ERROR
set p_return_code = 1;
rollback;
END;
START TRANSACTION;
DELETE from tb1;
insert into tb2 (name) values('seven');
COMMIT;
-- SUCCESS
set p_return_code = 2;
END\
delimiter;
5.游标
delimiter//
create procedure p6()
begin
declare row_id int; --自定义变量1
declare row_num int; --自定义变量2
declare done INT DEFAULT FALSE;
declare temp int;
declare my_cursor CURSOR FOR select id,num from A;
declare CONTINUE HANDLER FOR NOT FOUND SET done = RRUE;
open my_cursor;
xxoo: LOOP
fetch my_cursor into row_id, row_num;
if done then
leave xxoo;
END IF;
set temp = row_id + row_num;
insert into B(number) values(temp);
end loop xxoo;
close my_cursor;
6.动态执行SQL(防止SQL注入)
伪代码
delimiter//
create procedure p7(
in tpl varchar(255),
in arg int
)
begin
1.预检测 SQL语句合法性
2. SQL = 格式化 tpl + arg
3. 执行SQL语句
set @xo = arg;
EXECUTE xxx FROM 'select * from student where sid >?';
PREPARE xxx USING @xo;
DEALLOCATE prepare prod;
end//
delimter;
call p7("select * from tb where id>?",9)
真代码
delimiter\
CREATE PROCEDURE p8(
in nid int
)
BEGIN
PREPARE prod FROM 'select * from student where sid > ?';
EXECUTE prod USING @nid;
DEALLOCATE prepare prod;
END\
delimiter;
5索引
作用
-约束
-加速查找
索引种类:
-普通所以:加速查找
-主键索引:加速查找+不能为空+不能重复
-唯一索引:加速查找+不能重复
-联合索引(多列)
-联合主键索引
-联合唯一索引
-联合普通索引
hash索引:
ps:按哈希值排序
单值快
btree索引
ps:按二叉树查找
二叉树
创建索引:
- 额外的文件保存特殊的数据结构
- 查询快;插入更新慢
- 命中索引
普通索引:
create index ix_name on userinfo1(email); 建
drop index 索引名 on 表名(列名) 删
唯一索引:
create unique 索引名称 on 表名(列名)
联合索引(组合): 最左前缀匹配
create index 索引名称 on 表名(列名,列名)
覆盖索引:
- 在索引文件中直接获取数据
索引合并:
- 把多个单列索引合并使用
伪代码
delimiter//
create procedure p7(
in tpl varchar(255),
in arg int
)
begin
1.预检测 SQL语句合法性
2. SQL = 格式化 tpl + arg
3. 执行SQL语句
set @xo = arg;
EXECUTE xxx FROM 'select * from student where sid >?';
PREPARE xxx USING @xo;
DEALLOCATE prepare prod;
end//
delimter;
call p7("select * from tb where id>?",9)
真代码
delimiter\
CREATE PROCEDURE p8(
in nid int
)
BEGIN
PREPARE prod FROM 'select * from student where sid > ?';
EXECUTE prod USING @nid;
DEALLOCATE prepare prod;
END\
delimiter;
5索引
作用
-约束
-加速查找
索引种类:
-普通所以:加速查找
-主键索引:加速查找+不能为空+不能重复
-唯一索引:加速查找+不能重复
-联合索引(多列)
-联合主键索引
-联合唯一索引
-联合普通索引
hash索引:
ps:按哈希值排序
单值快
btree索引
ps:按二叉树查找
二叉树
创建索引:
- 额外的文件保存特殊的数据结构
- 查询快;插入更新慢
- 命中索引
普通索引:
create index ix_name on userinfo1(email); 建
drop index 索引名 on 表名(列名) 删
唯一索引:
create unique 索引名称 on 表名(列名)
联合索引(组合): 最左前缀匹配
create index 索引名称 on 表名(列名,列名)
覆盖索引:
- 在索引文件中直接获取数据
索引合并:
- 把多个单列索引合并使用
慢日志:
-执行时间>10
-未命中索引
-日志文件路径
配置
-内存
show variables like '%query%'
set gobal 变量名 = 值
-配置文件
mysqld --defaults-file='文件路径'
my.conf内容:
slow_query_log = ON
slow_query_log_file = D:/...
注意:修改配置文件之后,需要重启服务
-执行时间>10
-未命中索引
-日志文件路径
配置
-内存
show variables like '%query%'
set gobal 变量名 = 值
-配置文件
mysqld --defaults-file='文件路径'
my.conf内容:
slow_query_log = ON
slow_query_log_file = D:/...
注意:修改配置文件之后,需要重启服务
===分页===
a.select * from userinfo3 limit 20,10;
b.
-不让看
-索引表中扫:
select * from userinfo3 where id in(select id from userinfo3 limit 200000,10);
-方案:
记录当前页最大或最小ID
1.页面只有上一页, 下一页
# max_id
# min_id
下一页:
select * from userinfo3 where id> max_id limit 10;
上一页:
select * from userinfo3 where id< min_id order by id desc limit 10;
2.上一页 192 193 [196] 197 198 199 下一页:
select * from userinfo3 where id in(
select id from (select id from userinfo3 where id> max_id limit 30) as N order by N.id desc limit 10
)
c.