MySQL之視圖

一:視圖

1. 什麼是視圖?

本質上是一個虛擬的表。即看的見,可是實際不存在。mysql

2. 爲何須要虛擬表,使用場景是什麼?

​ 場景1:咱們但願某些查詢語句只能查看到某個表中的一部分數據,就能夠使用視圖sql

​ 場景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: 0

mysql> 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

注意:修改視圖,也會引發原表的變化,咱們不要這麼作,視圖僅用於查詢spa

相關文章
相關標籤/搜索