meaning
Virtual table, and use the same common table. Mysql 5.1 version of the new features, by ordinary table dynamically generated data, save only the sql logic, do not save the query results.
Scenarios
- Used in more than one place with the kind of query results
- The sql query results using more complex
A simple example
mysql> select * from account;
+----+----------+---------+
| id | username | balance |
+----+----------+---------+
| 2 | 哈哈哈 | 1000 |
| 3 | 嘿嘿和 | 2000 |
| 4 | 嘻嘻嘻 | 1000 |
| 5 | 拉拉阿拉 | 1 |
| 6 | 哇哇哇 | 545 |
+----+----------+---------+
5 rows in set (0.06 sec)
mysql> SELECT * FROM `account` where username like '%哈%';
+----+----------+---------+
| id | username | balance |
+----+----------+---------+
| 2 | 哈哈哈 | 1000 |
+----+----------+---------+
1 row in set (0.06 sec)
mysql> create view select_like_username as (SELECT * FROM `account` where username like '%哈%');
Query OK, 0 rows affected (0.01 sec)
mysql> select * from select_like_username where username like '%哈%';
+----+----------+---------+
| id | username | balance |
+----+----------+---------+
| 2 | 哈哈哈 | 1000 |
+----+----------+---------+
1 row in set (0.07 sec)
View benefits
- Reuse sql statement
- Sql simplify complex operations, not allowed to know the details of its inquiry
- Data protection, security
Modifying views
method one
create or replace view view name as a query;
Second way
alter view view name as a query;
Delete view
grammar
drop view view name, view name, .........; # can delete multiple
View View
grammar
desc view name;
show create view view name;
Update view
grammar
# 创建视图
create or replace view select_all_account as (SELECT * FROM `account`);
# 查看
select * from select_all_account;
# 插入
insert into select_all_account values(null,'张飞',111);
# 修改
update select_all_account set username = '关云长' where id = 7;
# 删除
delete from select_all_account where id = 7;
The definition of a relationship can update and view query view, the following types of view is not updated
- Sql statement containing the following keywords: group functions, distinct, group by, having, union or union all
- Constants view
- Select contains sub-queries
- jion
- from a view can not be updated
- Sub-clause where the query references from the table clause
Comparison of view and table
Creating keyword syntax | Whether physical memory occupied | use | |
---|---|---|---|
view | create view | Takes up little, saved the sql logic | CRUD |
table | create table | Occupation, save the data | CRUD |