当前位置: 移动技术网 > IT编程>数据库>Mysql > MySql基础知识之视图的特点、创建、操作等

MySql基础知识之视图的特点、创建、操作等

2018年11月01日  | 移动技术网IT编程  | 我要评论

1.视图简介

为了提高复杂sql语句的复用性和表操作的安全性,mysql管理提供了视图特性。所谓视图,本质上是一种虚拟表,其内容与真实的表相似,包含一系列带有名称的列和行数据。但是,视图并不在数据库中以存储数据值的形式存在。行和列数据来自定义视图的查询所引用基本表,并且在具体引用时动态生成。

视图使程序员只关心感兴趣的某些特定数据和他们所负责的特定任务。这样我们只能看到视图中所定义的数据,而不是视图所引用表中的全部数据,从而提高数据库中数据的安全性。

视图有以下几个特点:

(1)视图的列可以来自不同的表,是表的抽象和在逻辑意义上建立的新关系。

(2)视图是由基本表(实表)产生的表(虚表)。

(3)视图的建立和删除不影响基本表。

(4)对视图内容的更新(添加,删除和修改)直接影响基本表。

(5)当视图来自多个基本表时,不允许添加和删除数据。

视图的操作包括创建视图,查看视图,删除视图和修改视图。

2. 创建视图

具体讲解之前,请执行以下两条语句,创建一个名为t_product的表并插入四条数据

mysql> create table t_product(id int,name varchar(20),price float);
query ok, 0 rows affected (0.03 sec)

mysql> insert into t_product values(1,'apple',6.5),(2,'banana',4.5),(3,'orange',1.5),(4,'pear',2.5);
query ok, 4 rows affected (0.00 sec)
records: 4  duplicates: 0  warnings: 0

mysql> desc t_product;
+-------+-------------+------+-----+---------+-------+
| field | type        | null | key | default | extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | yes  |     | null    |       |
| name  | varchar(20) | yes  |     | null    |       |
| price | float       | yes  |     | null    |       |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> select * from t_product;
+------+--------+-------+
| id   | name   | price |
+------+--------+-------+
|    1 | apple  |   6.5 |
|    2 | banana |   4.5 |
|    3 | orange |   1.5 |
|    4 | pear   |   2.5 |
+------+--------+-------+
4 rows in set (0.00 sec)

2.1 创建视图的语法形式

从视图的概念可以发现其数据来源于查询语句,因此创建视图的语法为:

create view view_name as 查询语句;

示例:创建出隐藏价格字段price的视图view_selectproduct:

mysql> create view view_selectproduct as select id,name from t_product;
query ok, 0 rows affected (0.02 sec)

视图的查询就像查询表一样简单:

mysql> select * from view_selectproduct;
+------+--------+
| id   | name   |
+------+--------+
|    1 | apple  |
|    2 | banana |
|    3 | orange |
|    4 | pear   |
+------+--------+
4 rows in set (0.01 sec)

2.2 创建各种视图

2.2.1 封装实现查询语句的视图,即所谓的常量视图

具体语句如下:

create view view_test1 as select 3.1415926;

2.2.2 封装使用聚合函数查询语句的视图

聚合函数包括但不限于sum,min,max,count,具体语句如下:

create view view_test2 as select count(name) from t_product;

2.2.3 封装了实现排序功能(order by)查询语句的视图

具体语句如下:

create view view_test3 as select name from t_product order by id desc;

*desc表示降序,asc表示升序。

2.2.4 封装了实现表内连接查询语句的视图

具体语句如下:

create view view_test4 as select s.name from t_student as s ,t_group as g where s.group_id=g.id and g.id=2; 

其中,t_student,t_group分别时学生表和分组表,学生表中有一个字段为_id,分组表中有一个字段为id,且都为int型。

2.2.5 封装了实现表外连接的查询语句视图

具体语句如下:

create view view_test5 as select s.name from t_student as s left join t_group as g on s.group_id=g.id where g.id=2;

 

2.2.6 封装了实现子查询相关查询语句的视图

具体语句如下:

create view view_test6 as select s.name from t_student as s where s.group_id in (select id from t_group);

2.2.7 封装了实现记录联合(union和union all)查询语句的视图

具体语句如下:

create view view_test7 as select id, name from t_student union all select id,name from t_group;

2.3 查看视图

2.3.1 查看视图名:

具体语句:

show tables;

以上语句不仅会显示当前数据库中的数据包名,还会显示视图名。

2.3.2 语句查询视图的详细信息

show table status from table_name \g
上述语句会列出当前数据库中表和视图的详细信息,如下所示:
*************************** 2. row ***************************
           name: t_product
         engine: innodb
        version: 10
     row_format: dynamic
           rows: 4
 avg_row_length: 4096
    data_length: 16384
max_data_length: 0
   index_length: 0
      data_free: 0
 auto_increment: null
    create_time: 2018-05-13 21:42:08
    update_time: 2018-05-13 21:43:48
     check_time: null
      collation: latin1_swedish_ci
       checksum: null
 create_options: 
        comment: 
*************************** 3. row ***************************
           name: view_selectproduct
         engine: null
        version: null
     row_format: null
           rows: null
 avg_row_length: null
    data_length: null
max_data_length: null
   index_length: null
      data_free: null
 auto_increment: null
    create_time: null
    update_time: null
     check_time: null
      collation: null
       checksum: null
 create_options: null
        comment: view
3 rows in set (0.00 sec)

也可以使用show table查看指定视图的详细信息,具体语句如下:

show table status from database_name like 'view_name';

例如显示视图view_selectproduct的详细信息:

mysql> show table status from canvas like 'view_selectproduct' \g
*************************** 1. row ***************************
           name: view_selectproduct
         engine: null
        version: null
     row_format: null
           rows: null
 avg_row_length: null
    data_length: null
max_data_length: null
   index_length: null
      data_free: null
 auto_increment: null
    create_time: null
    update_time: null
     check_time: null
      collation: null
       checksum: null
 create_options: null
        comment: view
1 row in set (0.00 sec)

2.3.3 show create view语句查看视图定义信息

如果像查看视图的定义信息,可以使用如下语句:

show create view view_name;

例如,查看视图view_selectproduct视图的定义信息:

mysql> show create view view_selectproduct \g
*************************** 1. row ***************************
                view: view_selectproduct
         create view: create algorithm=undefined definer=`root`@`localhost` sql security definer view `view_selectproduct` as select `t_product`.`id` as `id`,`t_product`.`name` as `name` from `t_product`
character_set_client: utf8
collation_connection: utf8_general_ci
1 row in set (0.00 sec)

2.3.4 describe|desc语句查看视图设计信息

如果想要查看视图的设计信息,可以使用如下语句:

describe|desc view_name;

例如查看视图view_selectproduct的设计信息:

mysql> desc view_selectproduct;
+-------+-------------+------+-----+---------+-------+
| field | type        | null | key | default | extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | yes  |     | null    |       |
| name  | varchar(20) | yes  |     | null    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

2.4 删除视图

删除视图的语法形式如下:

drop view view_name1,view_name2...view_namen;

可以看出执行删除操作可以同时删除多个视图,示例略。

2.5 修改视图

2.5.1 create or replace view 语句修改视图

mysql提供了可以实现替换的创建视图语法,具体创建语法如下:

create or replace view view_name as 查询语句;

示例,重新创建view_selectproduct视图:

mysql> create or replace view view_selectproduct as select name from t_product;
query ok, 0 rows affected (0.02 sec)

mysql> desc view_selectproduct;
+-------+-------------+------+-----+---------+-------+
| field | type        | null | key | default | extra |
+-------+-------------+------+-----+---------+-------+
| name  | varchar(20) | yes  |     | null    |       |
+-------+-------------+------+-----+---------+-------+
1 row in set (0.00 sec)

2.5.2 alter修改语句

和修改表一样,alter语句也可以修改视图,具体语法如下:

alter view view_name as 查询语句;

例如,使用alter语句修改视图view_selectproduct:

mysql> alter view view_selectproduct as select name,price from t_product;
query ok, 0 rows affected (0.02 sec)

mysql> desc view_selectproduct;
+-------+-------------+------+-----+---------+-------+
| field | type        | null | key | default | extra |
+-------+-------------+------+-----+---------+-------+
| name  | varchar(20) | yes  |     | null    |       |
| price | float       | yes  |     | null    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

2.6 利用视图操作基本表

2.6.1 检索(查询)数据

通过视图查询数据与通过表查询数据一样,只不过通过视图查询比表更安全,更简单实用。在具体使用时只需要把表名换成视图名即可。

具体语法如下:

select * from view_name;

示例略。

2.6.2 利用视图操作基本表数据

对于视图的操作不仅限于查询,还可以对视图进行更新(增加,删除和更新)数据操作。由于视图时“虚表”,所以对视图数据进行的更新操作,实际是对其基本表数据进行更新操作。在具体更新视图数据时,需要注意以下两点:

(1)对视图数据进行添加,删除会直接影响基本表。

(2)视图来自于多个基本表时,不允许添加和删除数据。

2.6.2.1 添加数据操作

具体语法形式如下:

insert into view_name(属性名1,属性名2...)values(数据1,数据2...);
或者
insert into view_name values(数据1,数据2...);

示例略。

2.6.2.2 删除数据

语法形式如下:

delete from view_name where 条件语句;

示例略。

2.6.2.3 更新数据

语法形式如下:

update view_name set 新的属性值 where 条件语句;

示例:通过更新view_selectproduct中apple的售价为10.5:

mysql> select * from view_selectproduct;
+--------+-------+
| name   | price |
+--------+-------+
| apple  |   6.5 |
| banana |   4.5 |
| orange |   1.5 |
| pear   |   2.5 |
+--------+-------+
4 rows in set (0.00 sec)

mysql> select * from t_product;
+------+--------+-------+
| id   | name   | price |
+------+--------+-------+
|    1 | apple  |   6.5 |
|    2 | banana |   4.5 |
|    3 | orange |   1.5 |
|    4 | pear   |   2.5 |
+------+--------+-------+
4 rows in set (0.00 sec)

mysql> update view_selectproduct set price=10.5 where name='apple';
query ok, 1 row affected (0.01 sec)
rows matched: 1  changed: 1  warnings: 0

mysql> select * from view_selectproduct;
+--------+-------+
| name   | price |
+--------+-------+
| apple  |  10.5 |
| banana |   4.5 |
| orange |   1.5 |
| pear   |   2.5 |
+--------+-------+
4 rows in set (0.00 sec)

mysql> select * from t_product;
+------+--------+-------+
| id   | name   | price |
+------+--------+-------+
|    1 | apple  |  10.5 |
|    2 | banana |   4.5 |
|    3 | orange |   1.5 |
|    4 | pear   |   2.5 |
+------+--------+-------+
4 rows in set (0.00 sec)

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网