MySQL資料庫之檢視
阿新 • • 發佈:2020-12-15
檢視
-
概述
- 檢視是一張虛擬表,它表示一張表的部分資料或多張表的綜合資料,其結構和資料是建立在對錶的查詢基礎上
- 檢視中並不存放資料,而是存放在檢視所引用的原始表(基表)中
- 同一張原始表,根據不同使用者的不同需求,可以建立不同的檢視
-
作用
- 篩選表中的行
- 防止未經許可的使用者訪問敏感資料
- 隱藏資料表的結構
- 降低資料表的複雜程度
建立檢視
- 語法
-- 建立檢視
create view 檢視名
as
select 語句;
-- 查詢檢視
select 列名 from 檢視
MariaDB [sel]> create view best -> as -> select * from grades; # `Query OK, 0 rows affected (0.012 sec)` MariaDB [sel]> select math from best; +------+ | math | +------+ | 96 | | 91 | | 94 | | 94 | +------+ # `4 rows in set (0.008 sec)`
- 檢視可以使得降低SQL語句的複雜度
mysql> create view view2
-> as
-> select stuno,stusex,writtenexam,labexam from stuinfo natural join stumarks;
# `Query OK, 0 rows affected (0.01 sec)`
修改檢視
- 語法
alter view 檢視名
as
select 語句
mysql> alter view view2 -> as -> select stuname from stuinfo; # `Query OK, 0 rows affected (0.00 sec)`
刪除檢視
- 語法
drop view [if exists ] 檢視1,檢視,...
mysql> drop view view2;
# `Query OK, 0 rows affected (0.00 sec)`
檢視檢視資訊
- 方法一
- 顯示所有的表和檢視
mysql> show tables;
- 方法二
- 精確查詢檢視(檢視資訊儲存在information_schema下的views表中)
mysql> select table_name from information_schema.views; +------------+ | table_name | +------------+ | view1 | +------------+ # `1 row in set (0.05 sec)`
- 方法三
- 通過表的comment屬性查詢檢視
-- 查詢所有表和檢視的詳細狀態資訊
mysql> show table status\G;
-- 只查詢檢視資訊
mysql> show table status where comment='view'\G
查詢檢視的結構
mysql> desc view1;
查詢建立檢視的語法
mysql> show create view view1\G;
檢視演算法
- 一般場景
- 找出數學成績最高的男生和女生
MariaDB [sel]> select * from (select * from grades order by math desc) tab group by sex;
+-------+------+---------+------+
| name | sex | chinese | math |
+-------+------+---------+------+
| Sunny | boy | 93 | 96 |
| Marry | girl | 95 | 94 |
+-------+------+---------+------+
# `2 rows in set (0.001 sec)`
MariaDB [sel]> create view bestMath
-> as
-> select * from grades order by math desc;
# `Query OK, 0 rows affected (0.010 sec)`
MariaDB [sel]> select * from bestMath group by sex;
+-------+------+---------+------+
| name | sex | chinese | math |
+-------+------+---------+------+
| Sunny | boy | 93 | 96 |
| Marry | girl | 95 | 94 |
+-------+------+---------+------+
# `2 rows in set (0.001 sec)`
檢視的演算法
-
檢視的演算法有
merge
合併演算法- 將檢視語句和外層語句合併後再執行
temptable
臨時表演算法- 將檢視作為一個臨時表來執行
undefined
未定義演算法- 用哪種演算法有MySQL決定,這是預設演算法,檢視一般會選merge演算法
- 重新通過檢視實現
-
場景
- 找出語文成績最高的男生和女生
-
方法一
mysql> select * from (select * from stu order by ch desc) t group by stusex;
+--------+----------+--------+--------+---------+------------+------+------+
| stuNo | stuName | stuSex | stuAge | stuSeat | stuAddress | ch | math |
+--------+----------+--------+--------+---------+------------+------+------+
| s25321 | Tabm | 女 | 23 | 9 | 河北 | 88 | 77 |
| s25318 | 爭青小子 | 男 | 26 | 6 | 天津 | 86 | 92 |
+--------+----------+--------+--------+---------+------------+------+------+
# `2 rows in set (0.00 sec)`
- 方法二
mysql> create view view3
-> as
-> select * from stu order by ch desc;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from view3 group by stusex;
+--------+---------+--------+--------+---------+------------+------+------+
| stuNo | stuName | stuSex | stuAge | stuSeat | stuAddress | ch | math |
+--------+---------+--------+--------+---------+------------+------+------+
| s25301 | 張秋麗 | 男 | 18 | 1 | 北京 | 80 | NULL |
| s25303 | 李斯文 | 女 | 22 | 2 | 北京 | 55 | 82 |
+--------+---------+--------+--------+---------+------------+------+------+
# `2 rows in set (0.00 sec)`
- 方法三
mysql> create or replace algorithm=temptable view view3
-> as
-> select * from stu order by ch desc;
# `Query OK, 0 rows affected (0.00 sec)`
mysql> select * from view3 group by stusex;
+--------+----------+--------+--------+---------+------------+------+------+
| stuNo | stuName | stuSex | stuAge | stuSeat | stuAddress | ch | math |
+--------+----------+--------+--------+---------+------------+------+------+
| s25321 | Tabm | 女 | 23 | 9 | 河北 | 88 | 77 |
| s25318 | 爭青小子 | 男 | 26 | 6 | 天津 | 86 | 92 |
+--------+----------+--------+--------+---------+------------+------+------+
# `2 rows in set (0.00 sec)`
- 結論
- 方法一和方法二的結果不一樣,這是因為檢視的演算法造成的
- 方法三指定演算法為臨時表演算法,和子查詢結果一致