10_MySQL视图

视图简介

1. 视图是什么?
一张虚表,和真实表一样,视图包含一系列带有名称的行和列数据。视图是从一个或多个表中导出来的,我们也可以通过insert,update,delete来操作视图。当通过视图看到数据被修改时,相对应的原表的数据也会变化。同时原表发变化,则这种变化也可以自动反映到视图中。

2. 视图的优点
(1)简单化:看到的就是需要的。视图不仅可以简化用户对数据的理解,也可以简化操作,经常被使用的查询可以制作成一个视图
(2)安全性:通过视图用户只能查询和修改所能见到的数据,数据库中其他的数据即看不见也取不到。数据库授权命令可以让每个用户对数据库的检索限制到特定的数据库对象上,但不能授权到数据库特定的行,列上。
(3)逻辑数据独立性:视图可以帮助用户屏蔽真实表结构变化带来的影响。

创建视图

Create [or replace替换] [algroithm视图选择的算法={
    
    undefined|merge |temptable}] view 视图名
[(column_list)] as select_statement [with[cascaded|local]check option]
[algroithm视图选择的算法={
    
    undefined|merge |temptable}] Undefined:不常用。 merge :表示将使用
的视图语句与视图定义合并起来,使视图定义的某一部分取代语句对应的部分 temptable :表示将视图的结果存入临时表,然后用临时表来执行语句
with[cascaded|local] Cascaded :默认为cascaded,表示更新视图时,满足所有相关视图和表的条件
Local :表示更新视图时,满足该视图本身定义的条件即可

1.创建单表视图

mysql> create table t1(quantity int, price int);
Query OK, 0 rows affected (0.01 sec)

mysql> insert into t1 values(3,50);
Query OK, 1 row affected (0.00 sec)
mysql> create view view_t1 
    -> as select quantity,price,quantity*price
    -> from t1;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from view_t1;
+----------+-------+----------------+
| quantity | price | quantity*price |
+----------+-------+----------------+
|        3 |    50 |            150 |
+----------+-------+----------------+
1 row in set (0.00 sec)

//加上别名显示

mysql> create view view_t2(qty,price,total)
    -> as select quantity,price,quantity*price
    -> from t1;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from view_t2;
+------+-------+-------+
| qty  | price | total |
+------+-------+-------+
|    3 |    50 |   150 |
+------+-------+-------+
1 row in set (0.00 sec)

2.创建多表视图

mysql> create table student
    -> (
    -> s_id int primary key,
    -> s_name char(22),
    -> s_age int,
    -> s_sex char(22)
    -> );
Query OK, 0 rows affected (0.01 sec)

mysql> create table stu_info
    -> (
    -> s_id int,
    -> class char(22),
    -> addr varchar(22)
    -> );
Query OK, 0 rows affected (0.01 sec)

mysql> insert into stu_info(s_id,class,addr)
    -> values
    -> (1,'erban','anhui'),
    -> (2,'sanban','chongqing'),
    -> (3,'yiban','shandong');
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> insert into student(s_id,s_name)
    -> values
    -> (1,'zhangsan'),
    -> (2,'lisi'),
    -> (3,'wangwu');
mysql> create view stu_class(id,name,class) 
    -> as select student.s_id,student.s_name,stu_info.class
    -> from student,stu_info 
    -> where student.s_id = stu_info.s_id;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from stu_class;
+----+----------+--------+
| id | name     | class  |
+----+----------+--------+
|  1 | zhangsan | erban  |
|  2 | lisi     | sanban |
|  3 | wangwu   | yiban  |
+----+----------+--------+
3 rows in set (0.00 sec)

查看视图

1.查看视图的基本信息

语法:Show table status like ‘视图名’\G;

mysql> show table status\G

mysql> show table status like 'view_t2'\G

2.查看视图的详细信息

语法:show create view 视图名\G

mysql> show create view view_t1\G
*************************** 1. row ***************************
                View: view_t1
         Create View: CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `view_t1` AS select `t1`.`quantity` AS `quantity`,`t1`.`price` AS `price`,(`t1`.`quantity` * `t1`.`price`) AS `quantity*price` from `t1`
character_set_client: utf8
collation_connection: utf8_general_ci
1 row in set (0.00 sec)

在mysql中,information schema 数据库下的views表中存储了所有视图的定义。通过对views表的查询,可以查看数据库中所有视图的详细信息。

mysql> use information_schema 
mysql> select * from views where TABLE_NAME = 'view_t1'\G
*************************** 1. row ***************************
       TABLE_CATALOG: def
        TABLE_SCHEMA: qin
          TABLE_NAME: view_t1
     VIEW_DEFINITION: select `qin`.`t1`.`quantity` AS `quantity`,`qin`.`t1`.`price` AS `price`,(`qin`.`t1`.`quantity` * `qin`.`t1`.`price`) AS `quantity*price` from `qin`.`t1`
        CHECK_OPTION: NONE
        IS_UPDATABLE: YES
             DEFINER: root@localhost
       SECURITY_TYPE: DEFINER
CHARACTER_SET_CLIENT: utf8
COLLATION_CONNECTION: utf8_general_ci
1 row in set (0.00 sec)

修改视图

语法:Create or replace view

mysql> create or replace view view_t1 as select * from t1;
Query OK, 0 rows affected (0.00 sec)

mysql> desc view_t1;
+----------+---------+------+-----+---------+-------+
| Field    | Type    | Null | Key | Default | Extra |
+----------+---------+------+-----+---------+-------+
| quantity | int(11) | YES  |     | NULL    |       |
| price    | int(11) | YES  |     | NULL    |       |
+----------+---------+------+-----+---------+-------+
2 rows in set (0.00 sec)

//alter修改视图

mysql> desc view_t2;
+-------+------------+------+-----+---------+-------+
| Field | Type       | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| qty   | int(11)    | YES  |     | NULL    |       |
| price | int(11)    | YES  |     | NULL    |       |
| total | bigint(21) | YES  |     | NULL    |       |
+-------+------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> alter view view_t2 as select quantity from t1;
Query OK, 0 rows affected (0.00 sec)

mysql> desc view_t2;
+----------+---------+------+-----+---------+-------+
| Field    | Type    | Null | Key | Default | Extra |
+----------+---------+------+-----+---------+-------+
| quantity | int(11) | YES  |     | NULL    |       |
+----------+---------+------+-----+---------+-------+
1 row in set (0.00 sec)

更新视图

update
//查看原数据

mysql> select * from t1;
+----------+-------+
| quantity | price |
+----------+-------+
|        3 |    50 |
+----------+-------+
1 row in set (0.00 sec)

mysql> select * from view_t2;
+----------+
| quantity |
+----------+
|        3 |
+----------+
1 row in set (0.00 sec)

//修改后再次查看

mysql> update view_t2 set quantity=5;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from view_t2;
+----------+
| quantity |
+----------+
|        5 |
+----------+
1 row in set (0.00 sec)

mysql> select * from t1;
+----------+-------+
| quantity | price |
+----------+-------+
|        5 |    50 |
+----------+-------+
1 row in set (0.00 sec)

//原表插入数据视图表也会跟着改变

mysql> insert into t1 values(3,5);
Query OK, 1 row affected (0.00 sec)

mysql> select * from t1;
+----------+-------+
| quantity | price |
+----------+-------+
|        5 |    50 |
|        3 |     5 |
+----------+-------+
2 rows in set (0.00 sec)

mysql> select * from view_t2;
+----------+
| quantity |
+----------+
|        5 |
|        3 |
+----------+
2 rows in set (0.00 sec)

//删除视图表字段,原表也会跟着删除
相反删除原表字段,视图表也会跟着删除

mysql> select * from view_t1;
+----------+-------+
| quantity | price |
+----------+-------+
|        5 |    50 |
|        3 |     5 |
+----------+-------+
2 rows in set (0.00 sec)

mysql> delete from view_t1 where price=5;
Query OK, 1 row affected (0.00 sec)

mysql> select * from view_t1;
+----------+-------+
| quantity | price |
+----------+-------+
|        5 |    50 |
+----------+-------+
1 row in set (0.00 sec)

mysql> select * from t1;
+----------+-------+
| quantity | price |
+----------+-------+
|        5 |    50 |
+----------+-------+
1 row in set (0.00 sec)

视图存在以下情况时,更新操作无法执行
(1)视图中不包含原表中被定义为非空的列
(2) 在定义视图的select语句后的字段列表中使用了数学表达式
(3)在定义视图的select 语句后字段列表中使用了聚合函数时不接受更新操作
(4)select中,使用了union \top \group by 或having 无法接受

删除视图

语法 Drop view [IF EXISTS]视图名1,视图名2 ……

mysql> drop view if exists stu_class;
Query OK, 0 rows affected (0.00 sec)

扩展

MySQL中视图和表的区别以及联系是什么?

1.两者的区别
(1)视图是已经编译好的SQL语句,是基于SQL语句的结果集的可视化的表。而表不是。
(2)视图没有实际的物理记录,而表有。
(3)表是内容,视图窗口
(4)表和视图虽然都占用物理空间,但是视图只是逻辑概念存在,而表可以及时对数据进行修改,但是视图只能用创建语句来修改 。
(5)视图是查看数据表的一种方法,可以查询数据表中某些字段构成的数据,只是一些SQL 语句的集合。从安全角度来说,视图可以防止用户接触数据表,因而知道表结构 。
(6)表属于全局模式中的表,是实表。而视图属于局部模式的表,是虚表
(7)视图的建立和删除只影响视图本身,而不影响对应表的基本表。

2.两者的联系
视图是在基本表之上建立的表,它的结构和内容都来自于基本表,它依赖基本表存在而存在。一个视图可以对应一个基本表,也可以对应多个基本表。视图是基本的抽象和逻辑意义上建立的关系。

猜你喜欢

转载自blog.csdn.net/weixin_45310323/article/details/111562555