-- 查看基表数据
mysql> select * from user;
+----+----------+------+
| id | name | age |
+----+----------+------+
| 1 | zhangsan | 25 |
| 2 | lisi | 26 |
| 3 | wangwu | 27 |
| 4 | nike | 28 |
| 5 | lucy | 29 |
| 6 | shell | 30 |
| 7 | git | 31 |
| 8 | svn | 32 |
+----+----------+------+
8 rows in set (0.00 sec)
-- 创建视图
mysql> create view v_user
-> as
-> select * from user where age < 30;
Query OK, 0 rows affected (0.03 sec)
-- 查看视图数据
mysql> select * from v_user;
+----+----------+------+
| id | name | age |
+----+----------+------+
| 1 | zhangsan | 25 |
| 2 | lisi | 26 |
| 3 | wangwu | 27 |
| 4 | nike | 28 |
| 5 | lucy | 29 |
+----+----------+------+
5 rows in set (0.01 sec)
-- 更新视图数据,也就更新了基表数据
mysql> update v_user set age=26 where name='zhangsan';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
-- 查看更新后的基表数据
mysql> select * from user;
+----+----------+------+
| id | name | age |
+----+----------+------+
| 1 | zhangsan | 26 |
| 2 | lisi | 26 |
| 3 | wangwu | 27 |
| 4 | nike | 28 |
| 5 | lucy | 29 |
| 6 | shell | 30 |
| 7 | git | 31 |
| 8 | svn | 32 |
+----+----------+------+
8 rows in set (0.00 sec)
-- 查看更新后的视图数据
mysql> select * from v_user;
+----+----------+------+
| id | name | age |
+----+----------+------+
| 1 | zhangsan | 26 |
| 2 | lisi | 26 |
| 3 | wangwu | 27 |
| 4 | nike | 28 |
| 5 | lucy | 29 |
+----+----------+------+
5 rows in set (0.00 sec)