Mysql进阶-存储对象
# 视图
视图(View)是一种**虚拟存在的表**。视图中的数据并**不在数据库中实际存在**,行和列数据来自定义视图的查询中使用的表,并且是在使用视图时动态生成的。
通俗的讲,视图只保存了查询的SQL逻辑,不保存查询结果。所以我们在创建视图的时候,主要的工作就落在创建这条SQL查询语句上。
CASCADED会传递到依赖的视图,需要判断检查选项。
## LOCAL
如果依赖的视图没有检查选项,则不检查
## 视图的更新
要使视图可更新,视图中的行与基础表中的行之间必须存在一对一的关系。如果视图包含以下任何一项,则该视图不可更新
1. 聚合函数或窗口函数 (SUM()、MIN()、MAX()、COUNT()等)
2. DISTINCT
3. GROUP BY
4. HAVING
5. UNION 或者 UNION ALL
## 作用
- 简单
视图不仅可以简化用户对数据的理解,也可以简化他们的操作。那些被经常使用的查询可以被定义为视图,从而使得用户不必为以后的操作每次指定全部的条件。
- 安全
数据库可以授权,但不能授权到数据库特定行和特定的列上。通过视图用户只能查询和修改他们所能见到的数据。
- 数据独立
视图可帮助用户屏蔽真实表结构变化带来的影响。
# 存储过程
## 介绍
存储过程是事先经过编译并存储在数据库中的一段 SOL 语句的集合,调用存储过程可以简化应用开发人员的很多工作,减少数据在数据库和应用服务器之间的传输,对于提高数据处理的效率是有好处的。
存储过程思想上很简单,就是数据库 SQL 语言层面的代码封装与重用。
## 特点
- 封装,复用;
- 可以接收参数,也可以返回数据;
- 减少网络交互,效率提升;
==注意: 在命令行中,执行创建存储过程的SQL时,需要通过关键字 delimiter 指定SQL语句的结束符==。
## 变量
**系统变量** 是MySQL服务器提供,不是用户定义的,属于服务器层面。分为全局变量 (==GLOBAL==)、会话变量(默认:==SESSION==)。
**用户定义变量** 是用户根据需要自己定义的变量,用户变量不用提前声明,在用的时候直接用“@变量名”使用就可以。其作用域为当前连接。
**局部变量** 是根据需要定义的在局部生效的变量,访问之前,需要DECLARE声明。可用作存储过程内的局部变量和输入参数,局部变量的范围是在其内声明的BEGIN ... END块。
```sql
create procedure p2 ()
begin
# 声名
declare stu_count int default 0;
# 赋值
select count(*) into stu_count from student;
# 查看
select stu_count
end;
# 调用
call p2();
```
## if
举个例子:
```sql
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;
```
## 参数
举个例子:
```sql
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;
# 调用存储过程,@result为用户自定义变量
call p4( 18, @result);
# 查看结果
select @result;
----------------------------------------
# 如果为inout类型参数,在call()之前需要先赋值,如
set @result =66;
call p4(@result);
```
## case
举个例子:
```sql
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 >= 1 and month <= 12 then
set result :='第四季度';
else
set result :='非法参数'
end case
select concat('您输入的月份为:,month,,所属的季度为:,result);
end;
```
## while
举个例子:
```sql
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;
```
## repeat
举个例子:
```sql
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;
```
## loop
LOOP 实现简单的循环,如果不在SOL逻辑中增加退出循环的条件,可以用其来实现简单的死循环。LOOP可以配合一下两个语句使用:
- LEAVE:配合循环使用,退出循环。
- ITERATE:必须用在循环中,作用是跳过当前循环剩下的语句,直接进入下一次循环。

举个例子:
```sql
create procedure p9(in n int)
begin
declare total int default 0;
sum:Toop
if n<=0 then
leave sum;
end if;
set total := total + n;
set n := n - 1;
end loop sum;
select total;
end;
```
## 游标
游标(CURSOR)是用来存储查询结果集的数据类型,在存储过程和函数中可以使用游标对结果集进行循环的处理。游标的使用包括游标的声明、OPEN、FETCH 和 CLOSE,其语法分别如下。
举个例子:
```sql
-- 从一张表中找出符合条件的数据插入另一张表
create procedure p11(in uage int)
begin
declare uname varchar(100):
declare upro varchar(100):
declare U_cursor cursor for select name,profession from tb_user where age <= uage:
-- while循环的退出条件
declare exit handler for SQLSTATE '02000' close u_cu