MySQL视图

发布时间:2020-02-25 14:34:16 作者:warrent
来源:网络 阅读:187

前言:

什么是视图?

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

视图具有以下优点:

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

两者的区别:

两者的联系:

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

一、创建视图

1、创建单表视图

mysql> create table t(quantity int,price int);          <!--创建表-->
Query OK, 0 rows affected (0.01 sec)

mysql> insert into t values(3,50);               <!--插入数据-->
Query OK, 1 row affected (0.00 sec)
<!--创建视图-->
mysql> create view view_t as select quantity,price,quantity*price as tottal from t;
Query OK, 0 rows affected (0.01 sec)

mysql> select * from view_t;         <!--查看视图的数据-->
+----------+-------+-------+
| quantity | price | total |
+----------+-------+-------+
|        3 |    50 |   150 |
+----------+-------+-------+
1 row in set (0.00 sec)

2、创建多表视图

<!--创建基本表-->
mysql> create table student
    -> (
    -> s_id int(3) primary key,
    -> s_name varchar(30),
    -> s_age int(3),
    -> s_sex varchar(8)
    -> );
Query OK, 0 rows affected (0.01 sec)

mysql> create table stu_info
    -> (
    -> s_id int(3),
    -> class varchar(50),
    -> addr varchar(100)
    -> );
Query OK, 0 rows affected (0.01 sec)
<!--插入些数据-->
mysql> insert into stu_info values
    -> (1,'erban','anhui'),
    -> (2,'sanban','chongqing'),
    -> (3,'yiban','shangdong');
<!--创建视图-->
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;

3、查看视图的相关信息

<!--查看视图的表结构-->
mysql> desc stu_class;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(3)      | NO   |     | NULL    |       |
| name  | varchar(30) | YES  |     | NULL    |       |
| class | varchar(50) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
<!--查看视图的基本信息-->
mysql> show table status like 'stu_class'\G
*************************** 1. row ***************************
           Name: stu_class
         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.01 sec)
<!--查看视图的详细信息-->
mysql> show create view stu_class\G
*************************** 1. row ***************************
                View: stu_class
         Create View: CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `stu_class` AS select `student`.`s_id` AS `id`,`student`.`s_name` AS `name`,`stu_into`.`class` AS `class` from (`student` join `stu_into`) where (`student`.`s_id` = `stu_into`.`s_id`)
character_set_client: utf8
collation_connection: utf8_general_ci
1 row in set (0.00 sec)
<!--也可以直接查询information_schema库中的views表,来查看所有的视图-->
mysql> select * from information_schema.views where TABLE_SCHEMA='test02'\G
<!--where后面指定的是一个库名,也就是查看test02这个库中的所有视图-->

4、修改视图

方式一:

<!--修改视图-->
mysql> create or replace view view_t as select * from t;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from view_t;     <!--查看修改后的视图-->
+----------+-------+
| quantity | price |
+----------+-------+
|        3 |    50 |
+----------+-------+
1 row in set (0.00 sec)

方式二:

mysql> alter view view_t(abc) as select quantity from t;   <!--修改时指定视图的列名-->
Query OK, 0 rows affected (0.00 sec)

mysql> desc view_t;          <!--查看修改后的表结构-->
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| abc   | int(11) | YES  |     | NULL    |       |
+-------+---------+------+-----+---------+-------+
1 row in set (0.00 sec)

5、更新视图

1)update指令更新
<!--查看表以及视图的数据,其中quantity对应视图的abc字段-->
mysql> select * from t;
+----------+-------+
| quantity | price |
+----------+-------+
|        3 |    50 |
+----------+-------+
1 row in set (0.00 sec)

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

mysql> update view_t set abc=5;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from view_t;               <!--查看更新后的视图-->
+------+
| abc  |
+------+
|    5 |
+------+
1 row in set (0.00 sec)

mysql> select * from t;                  <!--查看更新后的表-->
+----------+-------+
| quantity | price |
+----------+-------+
|        5 |    50 |
+----------+-------+
1 row in set (0.00 sec)
2)insert指令更新
mysql> select * from t;               <!--查看表的数据-->
+----------+-------+
| quantity | price |
+----------+-------+
|        5 |    50 |
+----------+-------+
1 row in set (0.00 sec)

mysql> select * from view_t;             <!--查看视图的数据-->
+------+
| abc  |
+------+
|    5 |
+------+
1 row in set (0.00 sec)

mysql> insert into t values(3,5);       <!--向表中插入数据-->
Query OK, 1 row affected (0.00 sec)

mysql> select * from view_t;             <!--查看视图的数据-->
+------+
| abc  |
+------+
|    5 |
|    3 |
+------+
2 rows in set (0.00 sec)
mysql> select * from t;                     <!--查看表的数据-->
+----------+-------+
| quantity | price |
+----------+-------+
|        5 |    50 |
|        3 |     5 |
+----------+-------+
2 rows in set (0.00 sec)
3) delete指令删除表数据
<!--创建新的视图-->
mysql> create view view_t2(qty,price,total) as select quantity,price,quantity*price from t;
mysql> select * from view_t2;         <!--查看视图的内容-->
+------+-------+-------+
| qty  | price | total |
+------+-------+-------+
|    5 |    50 |   250 |
|    3 |     5 |    15 |
+------+-------+-------+
2 rows in set (0.00 sec)
mysql> delete from view_t2 where price=5;           <!--删除视图的数据-->
Query OK, 1 row affected (0.00 sec)

mysql> select * from view_t2;            <!--再次查看视图的数据-->
+------+-------+-------+
| qty  | price | total |
+------+-------+-------+
|    5 |    50 |   250 |
+------+-------+-------+
1 row in set (0.00 sec)
mysql> select * from t;              <!--查看原表的数据-->
+----------+-------+
| quantity | price |
+----------+-------+
|        5 |    50 |
+----------+-------+
1 row in set (0.00 sec)

6、删除视图

mysql> drop view view_t;                <!--删除视图-->
推荐阅读:
  1. 如何创建mysql视图
  2. MYSQL 视图详细操作

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

mysql视图

上一篇:磁盘阵列3块有硬盘掉线使用第二种校验方式恢复数据

下一篇:CAD制图初学入门教程 教你如何将CAD转换成DWF格式

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》