MySQL之视图
一:視圖
1. 什么是視圖?
本質上是一個虛擬的表。即看的見,但是實際不存在。
2. 為什么需要虛擬表,使用場景是什么?
? 場景1:我們希望某些查詢語句只能查看到某個表中的一部分數據,就可以使用視圖
? 場景2:簡化sql語句的編寫
3. 使用方法
創建的語法:
# 語法 create [or replace] view view_name as 查詢語句 or replace 如果視圖已經存在了,就替換里面的查詢語句# 使用: 測試數據 create table salarys(id int primary key auto_increment,name char(10),money float ) charset utf8; insert into salarys values(null,"張三豐",500000),(null,"張無忌",40000);# 第一種使用方式: 只能查看一部分數據(隔離數據) mysql> create view zwj_view as select money from salarys where name="張無忌"; Query OK, 0 rows affected (0.29 sec)mysql> show tables; +-------------------+ | Tables_in_day41_1 | +-------------------+ | salarys | | stu_class_view | | stu_info | | student | | zwj_view | +-------------------+ 5 rows in set (0.00 sec)mysql> select * from zwj_view; +-------+ | money | +-------+ | 40000 | +-------+ 1 row in set (0.00 sec)mysql> update salarys set money = 100 where name = "張無忌"; Query OK, 1 row affected (0.29 sec) Rows matched: 1 Changed: 1 Warnings: 0mysql> select * from zwj_view; +-------+ | money | +-------+ | 100 | +-------+ 1 row in set (0.00 sec)# 總結:當我們在查詢zwj_view視圖的時候,就去執行“select * from salarys where name = "張無忌";”這個sql。如果salarys表中的張無忌的數據改變了。那么zwj的視圖的結果也會發生改變。因為視圖的sql語句是根據salarys表中的數據來查詢的# 第二種使用方式:簡化sql測試數據 create table student(s_id int(3),name varchar(20),math float,chinese float ); insert into student values(1,'tom',80,70),(2,'jack',80,80),(3,'rose',60,75);create table stu_info(s_id int(3),class varchar(50),addr varchar(100) ); insert into stu_info values(1,'二班','安徽'),(2,'二班','湖南'),(3,'三班','黑龍江');# 查詢班級和學員的對應關系 select student.s_id,student.name,stu_info.class from student join stu_info on student.s_id = stu_info.s_id; # 然后將對應的sql,制作成一個視圖 create view stu_class_view as select student.s_id,student.name,stu_info.class from student join stu_info on student.s_id = stu_info.s_id;# 之后就使用視圖,做對應的查詢 mysql> create view stu_class_view as select student.s_id,student.name,stu_info.class from student join stu_info on student.s_id = stu_info.s_id; Query OK, 0 rows affected (0.29 sec)mysql> show tables; +-------------------+ | Tables_in_day41_1 | +-------------------+ | salarys | | stu_class_view | | stu_info | | student | | zwj | +-------------------+ 5 rows in set (0.00 sec)mysql> select * from stu_class_view; +------+------+--------+ | s_id | name | class | +------+------+--------+ | 1 | tom | 二班 | | 2 | jack | 二班 | | 3 | rose | 三班 | +------+------+--------+ 3 rows in set (0.00 sec)修改表
# 語法 alter view view_name as sql語句mysql> select * from zwj; +----+-----------+-------+ | id | name | money | +----+-----------+-------+ | 2 | 張無忌 | 100 | +----+-----------+-------+ 1 row in set (0.00 sec)# 對應的zwj的視圖,我們修改為查看“張三豐”的視圖mysql> alter view zwj as select * from salarys where id=1; Query OK, 0 rows affected (0.29 sec)mysql> select * from zwj; +----+-----------+--------+ | id | name | money | +----+-----------+--------+ | 1 | 張三豐 | 500000 | +----+-----------+--------+刪除
# 語法 drop view view_name;mysql> drop view zwj; Query OK, 0 rows affected (0.00 sec)mysql> show tables; +-------------------+ | Tables_in_day41_1 | +-------------------+ | salarys | | stu_class_view | | stu_info | | student | +-------------------+ 4 rows in set (0.00 sec)查看
# 語法 desc view_name;show create view view_name注意:修改視圖,也會引起原表的變化,我們不要這么做,視圖僅用于查詢
轉載于:https://www.cnblogs.com/plf-Jack/p/11194922.html
總結
- 上一篇: Elastic Job入门(1) - 简
- 下一篇: 「PKUWC2018」随机游走