标签:mysql
使用DESC查看视图
mysql> desc view_user;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| name | varchar(10) | YES | | NULL | |
| age | int(11) | YES | |NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.01 sec)
使用show table atstus查看视图
mysql> show table status like‘view_userinfo‘\G;
*************************** 1. row***************************
Name: view_userinfo
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)
ERROR:
No query specified
物理表
mysql> show table status like ‘user‘\G;
*************************** 1. row***************************
Name: user
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: 2016-01-20 14:45:41
Update_time: NULL
Check_time: NULL
Collation: latin1_swedish_ci
Checksum: NULL
Create_options:
Comment:
1 row in set (0.00 sec)
ERROR:
No query specified
使用Show create view查看视图
mysql> show create view view_userinfo\G;
*************************** 1. row***************************
View: view_userinfo
Create View: CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQLSECURITY DEFINER VIEW `view_userinfo` AS select `user`.`name` AS `name1`,`userinfo`.`phone`AS `phone1` from (`user` join `userinfo`) where (`user`.`id` =`userinfo`.`fid`)
character_set_client: utf8
collation_connection: utf8_general_ci
1 row in set (0.00 sec)
ERROR:
No query specified
在VIEW表中查看视图
mysql> select * frominformation_schema.views\G;
修改和删除视图
语法
CREATE [OR REPLACE] [ALGORITHM] ={undefined|MERGE|TEMPTABLE}]
VIEW view_name [(column_list)]
AS SELECT_statement
[WITH[CASCADED|LOCAL] CHECK OPTION]
mysql> select * from view_user;
+---------+------+
| name | age |
+---------+------+
| TubeLiu | 20 |
| Kevin | 20 |
| Mark | 30 |
| July | 40 |
+---------+------+
4 rows in set (0.00 sec)
mysql> create or replace view view_user
-> as
-> select id,name from user;
Query OK, 0 rows affected (0.01 sec)
mysql> select * from view_user;
+----+---------+
| id | name |
+----+---------+
| 1| TubeLiu |
| 2| Kevin |
| 3| Mark |
| 4| July |
+----+---------+
4 rows in set (0.00 sec)
使用alter语句修改视图
语法
ALTER [ALGORITHM] ={undefined|MERGE|TEMPTABLE}]
VIEW view_name [(column_list)]
AS SELECT_statement
[WITH[CASCADED|LOCAL] CHECK OPTION]
mysql> alter view view_user
-> as
-> select name from user;
Query OK, 0 rows affected (0.01 sec)
mysql> select * from view_user;
+---------+
| name |
+---------+
| TubeLiu |
| Kevin |
| Mark |
| July |
+---------+
4 rows in set (0.00 sec)
更新视图
更新视图是指通过视图来插入、更新、删除表中的数据,因为视图是一个虚拟表,其中没有数据。通过视图更新的时候都是转到基本表进行更新的,如果对视图增加或者删除记录,实际上是对其基本表增加或者删除记录。
mysql> select * from view_userinfo;
+---------+--------+
| name1 | phone1 |
+---------+--------+
| TubeLiu | 12345 |
| Kevin | 54321 |
| Mark | 32145 |
| July | 34521 |
+---------+--------+
4 rows in set (0.09 sec)
mysql> select * from userinfo;
+-----+-------+----------+
| fid | phone | location |
+-----+-------+----------+
| 1| 12345 | Shanghai |
| 2| 54321 | Beijing |
| 3| 32145 | Shenzhen |
| 4| 34521 | Dalian |
+-----+-------+----------+
4 rows in set (0.00 sec)
mysql> select * from user;
+----+---------+------+------+
| id | name | age | sex |
+----+---------+------+------+
| 1| TubeLiu | 20 | F |
| 2| Kevin | 20 | F |
| 3| Mark | 30 | F |
| 4| July | 40 | M |
+----+---------+------+------+
4 rows in set (0.00 sec)
mysql> update view_userinfo setphone1=43521 where name1=‘July‘;
Query OK, 1 row affected (0.02 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from userinfo;
+-----+-------+----------+
| fid | phone | location |
+-----+-------+----------+
| 1| 12345 | Shanghai |
| 2| 54321 | Beijing |
| 3| 32145 | Shenzhen |
| 4| 43521 | Dalian |
+-----+-------+----------+
4 rows in set (0.00 sec)
使用drop语句删除视图
mysql> drop view if exists view_user;
Query OK, 0 rows affected (0.00 sec)
[root@localhost viewdb]# pwd
/var/lib/mysql/viewdb
[root@localhost viewdb]# ls
db.opt user.frm user.ibd userinfo.frm userinfo.ibd view_userinfo.frm
[root@localhost viewdb]#
表的数据放在IBD文件里,表的信息放在FRM文件里。
View_user只有FRM文件,数据不是放在硬盘上。放在内存中。所以查的快。
本文出自 “阳光的蜗牛” 博客,谢绝转载!
标签:mysql
原文地址:http://sunshinesnail.blog.51cto.com/1179761/1737236