设为首页 加入收藏

TOP

MySQL视图的使用(一)
2015-11-21 01:55:33 来源: 作者: 【 】 浏览:0
Tags:MySQL 使用

概述:

视图即是虚拟表,也称为派生表,因为它们的内容都派生自其它表的查询结果。虽然视图看起来感觉和基本表一样,但是它们不是基本表。基本表的内容是持久的,而视图的内容是在使用过程中动态产生的。――摘自《SQLite权威指南》

使用视图的优点:

1.可靠的安全性
2.查询性能提高
3.有效应对灵活性的功能需求
4.轻松应对复杂的查询需求

视图的基本使用:

创建:

例如我们本身有一个这样的基本表:

mysql> select * from students;
+------+----------------+-------+
| id | name | age |
+------+----------------+-------+
| 1 | bumblebee | 200 |
| 1 | king of monkey | 10000 |
+------+----------------+-------+

那么就可以像这样来创建一个视图:

CREATE VIEW stu_view AS SELECT name FROM students;

Query OK, 0 rows affected (0.01 sec)

创建完一个视图,可以通过查看数据库中的全部数据表来查看:

mysql> show tables;
+-------------------+
| Tables_in_student |
+-------------------+
| stu_view |
| students |
+-------------------+
2 rows in set (0.00 sec)

可以看到当前数据库中已经把刚刚创建的视图放进数据库的表集合中了。因为视图也是一种表,是虚拟表。

查询:

视图的查询和基本表的查询一样,因为视图也是一种数据表,所以你可以像这样的来查询它

mysql> select * from stu_view;
+----------------+
| name |
+----------------+
| bumblebee |
| king of monkey |
+----------------+

删除:

DROP VIEW stu_view;

删除之后可以再次查询进行验证:

mysql> select * from stu_view;
ERROR 1146 (42S02): Table 'student.stu_view' doesn't exist

接下来我们看看如果我们变动了原始的基本表,视图会有什么改变:

mysql> INSERT INTO students(id, name, age) VALUES (2, 'Zeus', 100000);
Query OK, 1 row affected (0.00 sec)

检查基本表:

mysql> SELECT * FROM students;
+------+----------------+--------+
| id | name | age |
+------+----------------+--------+
| 1 | bumblebee | 200 |
| 1 | king of monkey | 10000 |
| 2 | Zeus | 100000 |
+------+----------------+--------+
3 rows in set (0.00 sec)

检查视图:

mysql> SELECT * FROM stu_view;
+----------------+
| name |
+----------------+
| bumblebee |
| king of monkey |
| Zeus |
+----------------+
3 rows in set (0.00 sec)

更新:

mysql> CREATE VIEW stu_view2 AS SELECT id, name FROM students;
Query OK, 0 rows affected (0.01 sec)

验证:

mysql> select * from stu_view2;
+------+----------------+
| id | name |
+------+----------------+
| 1 | bumblebee |
| 1 | king of monkey |
| 2 | Zeus |
+------+----------------+
3 rows in set (0.00 sec)

更新视图:

mysql> UPDATE stu_view2 SET name='Medusa' WHERE id=2;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0

检查视图更新结果:

mysql> SELECT * FROM stu_view2;
+------+----------------+
| id | name |
+------+----------------+
| 1 | bumblebee |
| 1 | king of monkey |
| 2 | Medusa |
+------+----------------+
3 rows in set (0.00 sec)

检查基本表更新结果:

mysql> SELECT * FROM students;
+------+----------------+--------+
| id | name | age |
+------+----------------+--------+
| 1 | bumblebee | 200 |
| 1 | king of monkey | 10000 |
| 2 | Medusa | 100000 |
+------+----------------+--------+
3 rows in set (0.00 sec)

关联多表的视图:

以上都是基于单表的操作,接下来我们从两张表中来做一些实战。

我们额外创建一个info表作为辅助的数据表,如下:

mysql> select * from info;
+----+--------+---------------------------------+
| id | stu_id | info |
+----+--------+---------------------------------+
| 1 | 1 | A member of the deformed steel. |
| 2 | 2 | Hero in Chinese Mythology. |
| 3 | 3 | In Greek mythology the Gorgon. |
+----+--------+---------------------------------+
3 rows in set (0.00 sec)

我们创建一个连接了两张基本表的视图stu_view3

mysql> CREATE VIEW stu_view3 AS SELECT s.id, s.name, s.age, i.info FROM students s, info i WHERE i.stu_id=s.id;
Query OK, 0 rows affected (0.00 sec)

验证过程:

mysql> select * from stu_view3;
+------+----------------+--------+---------------------------------+
| id | name | age | info |
+------+----------------+--------+---------------------------------+
| 1 | bumblebee | 200 | A member of the deformed steel. |
| 2 | king of monkey | 10000

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇debian8安装mysql5.6和简单配置 下一篇mysqlgenerallog查看mysql执行历史

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: