《MySQL必知必会》--视图

使用视图

什么是视图

视图是对检索过程的封装。
我们知道,当使用计算字段时,我们可以对其进行别名命名,以简化检索命令。
同样的道理,当我们需要重复使用某一段检索命令时,我们也可以对其进行“别名命名”,也即打包,使其成为视图,以供我们使用。

我们为什么要使用视图呢?

  1. 重复SQL语句;
  2. 简化复杂的SQL操作;
  3. 使用表的组成部分而不是整个表;
  4. 保护数据;
  5. 更改数据格式和表示

创建视图的规则:

  • 与表一样,视图必须唯一命名;
  • 对于可以创建的视图数目没有限制;
  • 为了创建视图,必须具有一定的权限;
  • 视图可以嵌套,即可以利用从其他视图中检索数据的查询来构造一个视图;
  • ODER BY 可以出现在视图中,但是,如果从该视图检索数据的SELECT语句中也包含ORDER BY,那么,该视图中ORDER BY语句将被覆盖;
  • 视图不能有索引,也不能有关联的触发器或默认值;
  • 视图可以和表一起使用。

使用视图的基本语句:

CREATE VIEW view_name来创建视图;
SHOE CREATE VIEW view_name来查看创建视图的语句;
DROP VIEW view_name来删除视图;
跟新视图时,可以先DROP 后CREATE,也可以直接用CREATE OR REPLACE VIEW

如何创建视图

下面讲述几种创建视图的场景,也可以说是创建视图的形式,或者是视图的几种作用。

联结性质视图

使用视图来简化联结语句。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
-------------------------
-- 未使用视图时的语句
-------------------------
select cust_name, prod_id
from customers, orders, orderitems
where customers.cust_id = orders.cust_id
and orders.order_num = orderitems.order_num
and prod_id = 'BNBG01';

-------------------------
-- 创建视图
-------------------------
create view cust_prod as
select cust_name, prod_id
from customers, orders, orderitems
where customers.cust_id = orders.cust_id
and orders.order_num = orderitems.order_num;


-------------------------
-- 使用视图时的语句
-------------------------
select * -- 或者SELECT cust_name或者SELECT cust_name, prod_id
from cust_prod
where prod_id = 'BNBG01';

格式化性质视图

使用视图重新定义数据输出格式。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
-------------------------------------
-- 在第一个视图的基础上检索特殊格式的数据
-------------------------------------
select concat(cust_name, '(', cust_email, ')')
from cust_prod
where prod_id = 'BNBG01';

-----------------------------
-- 视图嵌套
-----------------------------
create or replace view Name_Email as
select prod_id, cust_name, cust_email, concat(cust_name, '(', cust_email, ')') AS name_email
from cust_prod;

----------------------------
-- 使用视图后的格式化检索
----------------------------
select name_email
from Name_Email
where prod_id = 'BNBG01';

过滤性质的视图

使用视图可以用来过滤不想要的数据。

比如上面的例子检索结果会包含NULL值,当我们只想检索出不含该值的数据时,也可以使用视图封装,具体如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
------------------------
-- 未使用过滤性质视图前
------------------------
select name_email
from name_email
where prod_id = 'BNBG01'
AND name_email is not null;

-------------------------
-- 创建过滤性质视图
-------------------------
create or replace view NameViewNN AS
select prod_id, cust_name, cust_email, concat(cust_name, '(', cust_email, ')') AS name_email
from name_email
where name_email is not null;

--------------------------
-- 使用过滤性质视图
--------------------------
select name_email
from NameViewNN
where prod_id = 'BNBG01';

计算字段性质视图

也可以使用视图包装计算字段,具体形式与格式化视图创建类似,本章进行简单的展示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
-------------------
-- 未使用视图前
-------------------
select prod_id,
order_num,
quantity,
item_price,
order_item,
quantity*item_price as expanded_price
from orderitems
where prod_id = 'BNBG01';

-------------------
-- 视图创建
-------------------
create view orderitemsexpanded as
select prod_id,
order_num,
quantity,
item_price,
order_item,
quantity*item_price as expanded_price
from orderitems;

-------------------
-- 使用视图
-------------------
select order_num, expanded_price
from orderitemsexpanded
where prod_id = 'BNBG01';

总结

视图为虚拟的表,只包含检索结构,不包含数据。