一、MySQL 视图
1.1 mysql视图概述
1.1.1 视图介绍
• 什么是视图 (View)
– 虚拟表
– 内容与真实的表相似,包含一系列带有名称的列和行数据。
– 视图并不在数据库中以存储的数据的形式存在。
– 行和列的数据来自定义视图时查询所引用的基表,并
且在具体引用视图时动态生成。
– 更新视图的数据,就是更新基表的数据
– 更新基表数据,视图的数据也会跟着改变
1.1.2 视图优点
• 简单
– 使用视图的用户完全不需要关心视图中的数据是通过什么查询得到的。
– 视图中的数据对用户来说已经是过滤好的符合条件的结果集。
• 安全
– 用户只能看到视图中的数据。
• 数据独立
– 一旦视图的结构确定了,可以屏蔽表结构变化对用户的影响。
1.1.3 使用视图的限制
• 不能在视图上创建索引
• 在视图的 FROM 子句中不能使用子查询
• 以下情形中的视图是不可更新的
– 包含以下关键字的 SQL 语句:聚合函数 (SUM 、 MIN
、 MAX 、 COUNT 等 ) 、 DISTINCT 、 GROUP BY
、 HAVING 、 UNION 或 UNION ALL
– 常量视图
– JOIN
– FROM 一个不能更新的视图
– WHERE 子句的子查询引用了 FROM 子句中的表
– 使用了临时表,视图是不可更新
1.2 视图的基本使用
1.2.1 创建视图
• 语法格式
– create view 视图名称 as SQL 查询;
– create view 视图名称 (字段名列表) as SQL 查询;
mysql> create view t11 as select * from t1;
Query OK, 0 rows affected (0.05 sec)
1.2.2 查看视图
• 查看当前库下所有表的状态信息
– show table status;
– show table status where comment=”view”\G;
mysql> show table status where comment=“view”\G;
1.2.3 使用视图
• 查询记录
– Select 字段名列表 from 视图名 where 条件;
• 插入记录
– Insert into 视图名 ( 字段名列表 ) values( 字段值列表);
• 更新记录
– Update 视图名 set 字段名 = 值 where 条件;
• 删除记录
– Delete from 视图名 where 条件;注意:对视图操作即是对基本操作,反之亦然!!!
mysql> select * from v1;
mysql>select * from t1 left join t2 on t1.name=t2.name and t1.uid = t2.uid;
mysql> insert into v2(uname,password) values(“lijun”,123456);
mysql>update v2 set id=9 where name=”root”;
mysql> delete from v2 where id=10;
1.2.4 删除视图
• 语法格式
– drop view 视图名;
mysql> drop view t11;
1.3 视图的高级使用
1.3.1 创建视图完全格式
1.完全命令格式
• 命令格式
– CREATE
[OR REPLACE]
[ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE}]
[DEFINER = { user | CURRENT_USER }]
[SQL SECURITY { DEFINER | INVOKER }]
VIEW view_name [(column_list)]
AS select_statement
[WITH [CASCADED | LOCAL] CHECK OPTION]
2.设置字段别名
• 命令格式
– 视图中的字段名不可以重复 所以要定义别名
Create view 视图名 as
select 表别名 . 源字段名 as 字段别名
from 源表名 表别名 left join 源表名 表别名
on 条件;
mysql> create view v2 as
select a.name as aname , b.name as bname , a.uid as auid , b.uid as buid
from user a left join info b on a.uid=b.uid;
1.3.2重要选项说明
1.OR REPLACE
• 语法格式
– 创建时,若视图已存在,会替换已有的视图
– Create or replace view 视图名 as select 查询
mysql> create or replace view t1 as select * from userinfo;
2.ALGORITHM [定义处理视图的方式]
• 定义处理视图的方式
– ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE}
• MERAGE (替换方式)
– 视图名直接使用视图的公式替换掉,把视图公式合并到了 select 中。
• TEMPTABLE (具体化方式)
– 先得到视图的执行结果,该结果形成一个中间结果暂时存在
内存中,之后,外面的 select 语句就调用了这些中间结果。
• UNDEFINED (未定义)
– ALGORITHM 选项的值是 UNDEFINED 表示使用的是 MERAGE 替换方式。
mysql> create algorithm=merge view v2 as select * from userinfo;
mysql> show create view v2;
mysql> create algorithm=temptable view v3 as select * from userinfo;
mysql> show create view v3;
3.WITH CHECK OPTION [限制字段赋值]
• 当视图是根据另一个视图定义时 , 对视图更新 / 删除 / 插入
– LOCAL 和 CASCADED 关键字决定了检查的范围。
– LOCAL 仅检查当前视图的限制。
– CASCADED 同时要满足基表的限制。
mysql> create or replace view v1 as select * from userinfo where id<=10 with check option; //更新/插入v1表时,既要满足自己的条件又要满足基表的条件
mysql> create or replace view v2 as select * from v1 where id >=5 with local check option; //更新/插入v2表时,只需要满足自己的条件即可。
二、mysql存储过程
2.1 存储过程概述
2.1.1 存储过程介绍
• 什么是存储过程
– 数据库中保存的一系列 sql 命令的集合
– 编写存储过程时,可以使用变量、条件判断、流程控制等
– 存储过程,就是 MySQL 中的脚本
2.1.2 存储过程优点
• 存储过程优点
– 运行速度更快
– 增强了安全性
– 提高了可扩展性
– 避免重复的 sql 操作,提高了带宽利用率
1. 运行速度:对于很简单的sql,存储过程没有什么优势。对于复杂的业务逻辑,因为在存储过程创建的时候,数据库已经对其进行了一次解析和优化。存储过程一旦执行,在内存中就会保留一份这个存储过程,这样下次再执行同样的存储过程时,可以从内存中直接调用,所以执行速度会比普通sql快。
2. 增强安全性:提高代码安全,防止 SQL注入。这一点sql语句也可以做到。
3. 可扩展性:应用程序和数据库操作分开,独立进行,而不是相互在一起。方便以后的扩展和DBA维护优化。
4.避免重复的SQL操作,有效的减轻了网络负担
2.2 存储过程基本使用
2.2.1 创建存储过程
- 语法格式
delimiter //
create procedure
()
begin
…
…
end
//
delimiter ;
- 查看db9.user表中的用户数量
delimiter //
create procedure p1()
begin
select count(name) from db9.user;
end
//
delimiter ;
2.2.2 查看存储过程
• 方法 1
– mysql> show procedure status;
• 方法 2
– mysql> select db,name,type from mysql.proc where name=“ 存储过程名 “;
mysql> show procedure status\G;
mysql> select db,name,type,body from mysql.proc where name=\G;
2.2.3 调用 / 删除存储过程
• 调用存储过程
– Call 存储过程名 (); 【存储过程没有参数时, () 可以省略。有参数时,在调用存储过程时,必须传参。】
• 删除存储过程
– drop procedure 存储过程名;
mysql> call say();
mysql> drop procedure say;
2.3 存储过程进阶
2.3.1 存储过程参数类型: in out inout
• MySQL 存储过程,共有三种参数类型 IN,OUT,INOUT
Create procedure 名称 (类型 参数名 数据类型 ,类型 参数名 数据类型)
mysql> delimiter //
mysql> create procedure say(in username char(10)) # 定义 in 类型的参数变量 username
-> begin
-> select username;
-> select * from user where name=username;
-> end
-> //
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter ;
2.3.2 mysql 变量类型
变量的种类:会话变量 全局变量 用户变量 局部变量
mysql> show global variables; // 查看全局变量
mysql> show session variables; // 查看会话变量
mysql> set session sort_buffer_size = 40000; // 设置会话变量
mysql> show session variables like “sort_buffer_size”; // 查看会话变量
Mysql> show global variables like “% 关键字 %”; // 查看全局变量
mysql> set @y = 3; // 用户自定义变量,直接赋值
mysql> select max(uid) into @y from user; // 使用 sql 命令查询结果赋值
创建say48的存储过程
mysql> delimiter //
mysql> create procedure say48()
-> begin
-> declare x int default 9; // 局部变量 x
-> declare y char(10); // 局部变量 y
-> set y = “jim”;
-> select x;
-> select y;
-> end
-> //
Query OK, 0 rows affected (0.03 sec)
mysql> delimiter ;
2.3.3 算术运算
mysql运算符号 : + - * / DIV %
mysql> set @z=1+2;select @z;
mysql> set @x=1; set @y=2;set @[email protected]*@y; select @z;
mysql> set @x=1; set @y=2;set @[email protected]@y; select @z;
mysql> set @x=1; set @y=2;set @[email protected]/@y; select @z;
//给定用户名称,返回用户总数
mysql> drop procedure if exists say;
mysql> delimiter //
mysql> create procedure say(
in bash char(20), in nologin char(25), out x int , out y int
)
begin
declare z int ;
set z=0;
select count(name) into @x from db9.user where shell=bash;
select count(name) into @y from userdb.user where shell=nologin;
set
[email protected][email protected];
select z;
end
//
mysql> delimiter ;
call say(“/bin/bash”,”/sbin/nologin”,@x,@y);
2.3.4 条件判断
1.数值比较
> >= < <= = != between….and….
2.逻辑比较、范围、空、非空、模糊、正则
or and ! in not in is null is not null like regexp

2.3.5 流程控制
1.顺序结构
• 当“条件成立”时执行命令序列
• 否则,不执行任何操作
if 顺序结构
if 条件判断 then
代码
…..
end if ;
案例1:
mysql> drop procedure if exists say;
mysql> delimiter //
mysql> create procedure say(in x int(1) )
begin
if x <= 10 then
select * from userdb.user where id <=x;
end if;
end
//
mysql> delimiter ;
mysql> call say(1); # 条件判断成立
if 条件判断 then
代码1
…..
else
代码2
…..
end if;
案例2:
mysql> drop procedure if exists say;
mysql> delimiter //
mysql> create procedure say(in x int(1) )
begin
if x is null then
set @x = 1;
select * from userdb.user where id=x;
end if;
if x <= 10 then
select * from userdb.user where id <=x;
end if;
end
//
mysql> delimiter ;
MySQL> call say(@x) // 调用未定义变量 x
2.循环结构
2.1 条件式循环
– 反复测试条件,
– 只要成立就执行命令序列语法格式:
while 条件判断 do
循环体
…….
end while ;
案例3:
mysql> delimiter //
mysql> create procedure say()
-> begin
-> declare i int;
-> set i=1;
-> while i <= 5 do
-> select i;
-> set i=i+1;
-> end while;
-> end
-> //
mysql> delimiter ;
2.2 条件式循环
– 无循环条件loop
循环体
……
end loop ;
案例4:
mysql> delimiter //
mysql> create procedure say2( )
-> begin
-> declare i int;
-> set i=1;
-> loop
-> select i;
-> set i=i+1;
-> end loop;
-> end
-> //
mysql> delimiter ;
mysql> call say2( ); # 不按 ctrl+c 结束 会一直输出变量 i 的值
2.3 条件式循环
– until 条件判断,不成立时结束循环repeat
循环体
until 条件判断
end repeat ;
案例5:
mysql> delimiter //
mysql> create procedure say3( )
-> begin
-> declare i int;
-> set i=1;
-> repeat
-> select i;
-> set i=i+1;
-> until i=6 // 此处不需要使用;
-> end repeat;
-> end
-> //
mysql> delimiter ;