MySQL高级-视图

视图

  • 本节例子中用到的表结构:
  • 1.视图概述
  • 2.创建或者修改视图
  • 3.查看视图
  • 4.删除视图

本节例子中用到的表结构:

create database demo_01 default charset=utf8mb4;

use demo_01;

CREATE TABLE `city` (
`city_id` int(11) NOT NULL AUTO_INCREMENT,
`city_name` varchar(50) NOT NULL,
`country_id` int(11) NOT NULL,
PRIMARY KEY (`city_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `country` (
`country_id` int(11) NOT NULL AUTO_INCREMENT,
`country_name` varchar(100) NOT NULL,
PRIMARY KEY (`country_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

insert into `city` (`city_id`, `city_name`, `country_id`) values(1,'西安',1);
insert into `city` (`city_id`, `city_name`, `country_id`) values(2,'NewYork',2);
insert into `city` (`city_id`, `city_name`, `country_id`) values(3,'北京',1);
insert into `city` (`city_id`, `city_name`, `country_id`) values(4,'上海',1);

insert into `country` (`country_id`, `country_name`) values(1,'China');
insert into `country` (`country_id`, `country_name`) values(2,'America');
insert into `country` (`country_id`, `country_name`) values(3,'Japan');
insert into `country` (`country_id`, `country_name`) values(4,'UK');

city表(使用查询语句select * from city;)
这里city_id是主键,非空自增。country_id是外键

+---------+-----------+------------+
| city_id | city_name | country_id |
+---------+-----------+------------+
|       1 | 西安      |          1 |
|       2 | NewYork   |          2 |
|       3 | 北京      |          1 |
|       4 | 上海      |          1 |
+---------+-----------+------------+

country表(使用查询语句select * from country;)
country_id是主键非空自增。

+------------+--------------+
| country_id | country_name |
+------------+--------------+
|          1 | China        |
|          2 | America      |
|          3 | Japan        |
|          4 | UK           |
+------------+--------------+

1.视图概述

视图(View)是一种虚拟存在的表。视图并不在数据库中实际存在,行和列数据来自定义视图的查询中使用的表, 并且是在使用视图时动态生成的。通俗的讲,视图就是一条SELECT语句执行后返回的结果集。所以我们在创建视 图的时候,主要的工作就落在创建这条SQL查询语句上。
视图相对于普通的表的优势主要包括以下几项。

  • 简单:使用视图的用户完全不需要关心后面对应的表的结构、关联条件和筛选条件,对用户来说已经是过滤 好的复合条件的结果集。
  • 安全:使用视图的用户只能访问他们被允许查询的结果集,对表的权限管理并不能限制到某个行某个列,但 是通过视图就可以简单的实现。
  • 数据独立:一旦视图的结构确定了,可以屏蔽表结构变化对用户的影响,源表增加列对视图没有影响;源表 修改列名,则可以通过修改视图来解决,不会造成对访问者的影响。

2.创建或者修改视图

创建视图的语法为:

CREATE [OR REPLACE] [ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE}]
 
VIEW view_name [(column_list)]
 
AS select_statement
 
[WITH [CASCADED | LOCAL] CHECK OPTION]

例:我们有两个表city和country表(文章前面已经给出),其中country_id是city表的外键。我们想展示国家和城市之间的信息。所以需要使用到连接查询

select c.*,t.country_name  from city c,country t where c.country_id=t.country_id;
+---------+-----------+------------+--------------+
| city_id | city_name | country_id | country_name |
+---------+-----------+------------+--------------+
|       1 | 西安      |          1 | China        |
|       3 | 北京      |          1 | China        |
|       4 | 上海      |          1 | China        |
|       2 | NewYork   |          2 | America      |
+---------+-----------+------------+--------------+
mysql> create view view_city_country as select c.*,t.country_name  from city c,country t where c.country_id=t.country_id;
Query OK, 0 rows affected (0.05 sec)

创建视图成功
此时视图相当于一张虚拟的表,怎么操作基本表,就怎么操作视图。

mysql> update view_city_country set city_name='西安市' where city_id =1;
Query OK, 1 row affected (0.04 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from view_city_country;
+---------+-----------+------------+--------------+
| city_id | city_name | country_id | country_name |
+---------+-----------+------------+--------------+
|       1 | 西安市    |          1 | China        |
|       3 | 北京      |          1 | China        |
|       4 | 上海      |          1 | China        |
|       2 | NewYork   |          2 | America      |
+---------+-----------+------------+--------------+
4 rows in set (0.00 sec)

此时基本表的数据也被更新了。

+---------+-----------+------------+
| city_id | city_name | country_id |
+---------+-----------+------------+
|       1 | 西安市    |          1 |
|       2 | NewYork   |          2 |
|       3 | 北京      |          1 |
|       4 | 上海      |          1 |
+---------+-----------+------------+

虽然视图能够更新,但是不建议更新视图
修改视图的语法为:

ALTER [ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE}]
 
VIEW view_name [(column_list)]
 
AS select_statement
 
[WITH [CASCADED | LOCAL] CHECK OPTION]
选项 :     
WITH [CASCADED | LOCAL] CHECK OPTION 
决定了是否允许更新数据使记录不再满足视图的条件。        
		LOCAL : 只要满足本视图的条件就可以更新。    
    	CASCADED : 必须满足所有针对该视图的所有视图的条件才可以更新。 默认值.

3.查看视图

从 MySQL 5.1 版本开始,使用 SHOW TABLES 命令的时候不仅显示表的名字,同时也会显示视图的名字,而不存 在单独显示视图的 SHOW VIEWS 命令。

mysql> show tables;
+-------------------+
| Tables_in_demo_01 |
+-------------------+
| city              |
| country           |
| view_city_country |
+-------------------+
3 rows in set (0.00 sec)

如果需要查询某个视图的定义,可以使用 SHOW CREATE VIEW 命令进行查看 :

mysql> show create view view_city_country;
+-------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+
| View              | Create View                                                                                                                                                                                                                                                                                                                    | character_set_client | collation_connection |
+-------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+
| view_city_country | CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `view_city_country` AS select `c`.`city_id` AS `city_id`,`c`.`city_name` AS `city_name`,`c`.`country_id` AS `country_id`,`t`.`country_name` AS `country_name` from (`city` `c` join `country` `t`) where (`c`.`country_id` = `t`.`country_id`) | utf8                 | utf8_general_ci      |
+-------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+
1 row in set (0.00 sec)

4.删除视图

语法 :

DROP VIEW [IF EXISTS] view_name [, view_name] ...[RESTRICT | CASCADE]   

示例 , 删除视图city_country_view :

mysql> drop view view_city_country;
Query OK, 0 rows affected (0.00 sec)

mysql> show tables;
+-------------------+
| Tables_in_demo_01 |
+-------------------+
| city              |
| country           |
+-------------------+
2 rows in set (0.00 sec)

你可能感兴趣的:(MySQL,数据库,mysql,数据库)