Date: 20140223
Auth: Jin

一、介绍
1、概念
视图是从一个或几个基本表(或视图)导出的表,是数据库的用户使用数据库的观点。
视图是一个虚表即所应对的数据不进行实际存储,数据库中只存储视图的定义。
2.视图的优点:
1).为用户集中数据简化用户的查询和处理。
2).屏蔽数据库的复杂性
3).简化用户权限的管理 grant语句可以针对视图进行授予权限。
4).便于数据库共享
5).可以重新组织数据以便输出到其他应用程序
视点集中,简化操作,定制数据,合并分割数据,安全性

3.视图的缺点
1).只有在当前数据库中才能创建视图,视图到命名必须遵循标示符命名规则不能与表同名
视图的来源数据可以是同一个实例下的其他库
2).不能把规则,默认值,或触发器与视图相关联。

二、操作SQL
1、查看库中有的视图
表明上视图表和没什么区别
SQL语句:

show table status where Comment='VIEW';
3304-phpcms>show table status where Comment='VIEW';
+--------------------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------

+-------------+-------------+------------+-----------+----------+----------------+---------+
| Name               | Engine | Version | Row_format | Rows | Avg_row_length | Data_length | Max_data_length | Index_length | Data_free | Auto_increment | Create_time 

| Update_time | Check_time | Collation | Checksum | Create_options | Comment |
+--------------------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------

+-------------+-------------+------------+-----------+----------+----------------+---------+
| v_carseats_article | NULL   |    NULL | NULL       | NULL |           NULL |        NULL |            NULL |         NULL |      NULL |           NULL | NULL        

| NULL        | NULL       | NULL      |     NULL | NULL           | VIEW    |
+--------------------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------

+-------------+-------------+------------+-----------+----------+----------------+---------+

2、查看创建视图的SQL语句

3304-phpcms>show create view v_carseats_article\G
*************************** 1. row ***************************
                View: v_carseats_article
         Create View: CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`127.0.0.1` SQL SECURITY DEFINER VIEW `v_carseats_article` AS select `a`.`id` AS `id`,`a`.`title` AS 

`title`,`a`.`catid` AS `module_id`,`a`.`thumb` AS `img`,`c`.`catname` AS `module_name`,`b`.`content` AS `content`,`a`.`username` AS `publish_user`,`a`.`status` AS 

`is_del`,`a`.`listorder` AS `order`,`a`.`updatetime` AS `update_time`,`a`.`inputtime` AS `add_time`,`b`.`copyfrom` AS `copyfrom`,`a`.`keywords` AS 

`keywords`,`a`.`islink` AS `islink`,`a`.`url` AS `url` from ((`v9_news` `a` join `v9_news_data` `b`) join `v9_category` `c`) where ((`a`.`catid` = `c`.`catid`) and 

(`a`.`id` = `b`.`id`))
character_set_client: utf8
collation_connection: utf8_general_ci
1 row in set (0.00 sec)3304-anquanzuo>show create view carseats_article\G
*************************** 1. row ***************************
                View: carseats_article
         Create View: CREATE ALGORITHM=UNDEFINED DEFINER=`dbproxy`@`192.168.201.201` SQL SECURITY DEFINER VIEW `carseats_article` AS select `a`.`id` AS 

`id`,`a`.`title` AS `title`,`a`.`catid` AS `module_id`,`a`.`thumb` AS `img`,`c`.`catname` AS `module_name`,`b`.`content` AS `content`,`a`.`username` AS 

`publish_user`,`a`.`status` AS `is_del`,`a`.`listorder` AS `order`,`a`.`updatetime` AS `update_time`,`a`.`inputtime` AS `add_time`,`b`.`copyfrom` AS 

`copyfrom`,`a`.`keywords` AS `keywords`,`a`.`islink` AS `islink`,`a`.`url` AS `url` from ((`phpcms`.`v9_news` `a` join `phpcms`.`v9_news_data` `b`) join 

`phpcms`.`v9_category` `c`) where ((`a`.`catid` = `c`.`catid`) and (`a`.`id` = `b`.`id`))
character_set_client: utf8
collation_connection: utf8_general_ci
1 row in set (0.01 sec)

3、创建视图
create [or replace] [algorithm = {undefined | merge | temptable}] view [db_name.]view_name [(column_list)] as select_statement [with [cascaded | local] check option]通
过该语句可以创建视图,若给定了[or replace],则表示当已具有同名的视图时,将覆盖原视图。select_statement是一个查询语句,这个查询语句可从表或其它的视图中查询。视图属于数
据库,因此需要指定数据库的名称,若未指定时,表示在当前的数据库创建新视图。

表和数据库共享数据库中相同的名称空间,因此,数据库不能包含相同名称的表和视图,并且,视图的列名也不能重复。

创建视图存在如下注意事项:
(1) 运行创建视图的语句需要用户具有创建视图(crate view)的权限,若加了[or replace]时,还需要用户具有删除视图(drop view)的权限;
(2) select语句不能包含from子句中的子查询;
(3) select语句不能引用系统或用户变量;
(4) select语句不能引用预处理语句参数;
(5) 在存储子程序内,定义不能引用子程序参数或局部变量;
(6) 在定义中引用的表或视图必须存在。但是,创建了视图后,能够舍弃定义引用的表或视图。要想检查视图定义是否存在这类问题,可使用check table语句;
(7) 在定义中不能引用temporary表,不能创建temporary视图;
(8) 在视图定义中命名的表必须已存在;
(9) 不能将触发程序与视图关联在一起;
(10) 在视图定义中允许使用order by,但是,如果从特定视图进行了选择,而该视图使用了具有自己order by的语句,它将被忽略。

实例1:
DROP VIEW IF EXISTS `v_carseats_article`;
CREATE VIEW `v_carseats_article` AS
SELECT
`a`.`id` AS `id`,
`a`.`title` AS `title`,
`a`.`catid` AS `module_id`,
`a`.`thumb` AS `img`,
`c`.`catname` AS `module_name`,
`b`.`content` AS `content`,
`a`.`username` AS `publish_user`,
`a`.`status` AS `is_del`,
`a`.`listorder` AS `order`,
`a`.`updatetime` AS `update_time`,
`a`.`inputtime` AS `add_time`,
`b`.`copyfrom` AS `copyfrom`,
`a`.`keywords` AS `keywords`,
`a`.`islink` AS `islink`,
`a`.`url` AS `url`
FROM ((`v9_news` AS `a` JOIN `v9_news_data` AS `b`) JOIN `v9_category` AS `c`)
WHERE ((`a`.`catid` = `c`.`catid`) AND (`a`.`id` = `b`.`id`));
重点是用SELECT语句确定数据源。

实例2
本例创建一个产品表(product)和一个购买记录表(purchase),再通过视图purchase_detail查询出购买的详细信息。
准备表和数据

create table product (
product_id int not null,
name varchar(50) not null,
price double not null
);
insert into product values(1, 'apple ', 5.5);create table purchase(
id int not null,
product_id int not null,
qty int not null default 0,
gen_time datetime not null
);
insert into purchase values(1, 1, 10, now());


创建视图

create view purchase_detail as 
select 
product.name as name, 
product .price as price, 
purchase.qty as qty, 
product.price * purchase.qty as total_value 
from product, purchase where product.product_id = purchase.product_id;

使用视图获取数据

select * from purchase_detail;
+-------+-------+-----+-------------+| name | price | qty | total_value |
+-------+-------+-----+-------------+
| apple | 5.5 | 10 | 55 |
+-------+-------+-----+-------------+

4.修改
alter [algorithm = {undefined | merge | temptable}] view view_name [(column_list)] as select_statement [with [cascaded | local] check option]该语句用于更改已有视图的定
义。其语法与create view类似。

将上面创建的视purchase_detail进行修改,去掉qty列,语句如下:

alter view purchase_detail as 
select 
product.name as name, 
product.price as price, 
product.price * purchase.qty as total_value 
from product, purchase where product.product_id = purchase.product_id;


查询

mysql> select * from  purchase_detail;
+--------+-------+-------------+
| name   | price | total_value |
+--------+-------+-------------+
| apple  |   5.5 |          55 |
+--------+-------+-------------+
1 row in set (0.00 sec)

三、通过更新视图实现更新真实表
参考:
注意:视图一般只查询,尽量避免修改,防止出现数据问题

四、视图用于远程表

五、备份和迁移
1、备份和表一样
2、迁移和表一样
和存储过程不一样,DUMP时候不用特殊参数