視圖概述
- 視圖是由數(shù)據(jù)庫中的一個(gè)表或多個(gè)表導(dǎo)出的虛擬表,是一種虛擬存在的表,方便用戶對數(shù)據(jù)的操作。
- 視圖是一個(gè)虛擬表,是從數(shù)據(jù)庫中一個(gè)或多個(gè)表中導(dǎo)出來的表,其內(nèi)容由查詢定義。
- 同真實(shí)表一樣,視圖包含一系列帶有名稱的列和行數(shù)據(jù)
- 數(shù)據(jù)庫中只存放了視圖的定義,而并沒有存放視圖中的數(shù)據(jù)。這些數(shù)據(jù)存放在原來的表中。
- 使用視圖查詢數(shù)據(jù)時(shí),數(shù)據(jù)庫系統(tǒng)會(huì)從原來的表中取出對應(yīng)的數(shù)據(jù)。
- 一旦表中的數(shù)據(jù)發(fā)生改變,顯示在視圖中的數(shù)據(jù)也會(huì)發(fā)生改變。
使用視圖的原因
- 安全原因,視圖可以隱藏一些數(shù)據(jù),例如,員工信息表,可以用視圖只顯示姓名、工齡、地址,而不顯示社會(huì)保險(xiǎn)號和工資數(shù)等
- 另一個(gè)原因是可使復(fù)雜的查詢易于理解和使用。
創(chuàng)建視圖
語法格式
CREATE [OR REPLACE] [ALGORITHM={UNDEFINED|MERGE|TEMPTABLE}]
VIEW 視圖名[(屬性清單)]
AS SELECT語句
[WITH [CASCADED|LOCAL] CHECK OPTION];
- REPLACE:替換現(xiàn)有視圖
- ALGORITHM:可選項(xiàng),表示視圖選擇的算法。
- 屬性清單:可選項(xiàng),指定視圖中各個(gè)屬性的名詞,默認(rèn)情況下與SELECT語句中的查詢的屬性相同。
- SELECT語句:表示一個(gè)完整的查詢語句,將查詢記錄導(dǎo)入視圖中。
- WITH CHECK OPTION:可選項(xiàng),表示更新視圖時(shí)要保證在該視圖的權(quán)限范圍之內(nèi)。
視圖示例
- 創(chuàng)建包含員工名、email和部門名的視圖
mysql> use test2021;
mysql> create view emp_view
-> as
-> select name, email, dept_name
-> from employees as e
-> inner join departments as d
-> on e.dept_id=d.dept_id;
Query OK, 0 rows affected (0.01 sec)
# 查詢視圖中數(shù)據(jù)
mysql> select * from emp_view;
mysql> select * from emp_view where dept_name='運(yùn)維部';
+-----------+--------------------+-----------+
| name | email | dept_name |
+-----------+--------------------+-----------+
| 廖娜 | liaona@guodong.com | 運(yùn)維部 |
| 竇紅梅 | douhongmei@guodong.com | 運(yùn)維部 |
| 聶想 | niexiang@guodong.com | 運(yùn)維部 |
| 陳陽 | chenyang@guodong.com | 運(yùn)維部 |
| 戴璐 | dailu@guodong.com | 運(yùn)維部 |
| 陳斌 | chenbin@guodong.com | 運(yùn)維部 |
+-----------+--------------------+-----------+
6 rows in set (0.00 sec)
mysql> create view emp_sal_view
-> as
-> select name, date, basic+bonus as total
-> from employees as e
-> inner join salary as s
-> on e.employee_id=s.employee_id;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from emp_sal_view where year(date)=2020 and month(date)=12;
修改視圖
語法格式
方式一:
CREATE [OR REPLACE] [ALGORITHM={UNDEFINED|MERGE|TEMPTABLE}]
VIEW 視圖名[(屬性清單)]
AS SELECT語句
[WITH [CASCADED|LOCAL] CHECK OPTION];
mysql> create or replace view emp_view
-> as
-> select name, email, d.dept_id, dept_name
-> from employees as e
-> inner join departments as d
-> on e.dept_id=d.dept_id;
mysql> select * from emp_view;
方式二
ALTER VIEW 視圖名 AS 查詢語句
mysql> alter view emp_sal_view
-> as
-> select name, date, basic, bonus, basic+bonus as total
-> from employees as e
-> inner join salary as s
-> on e.employee_id=s.employee_id;
Query OK, 0 rows affected (0.01 sec)
mysql> select * from emp_sal_view where year(date)=2020 and month(date)=12;
查看視圖
SHOW TABLES
DESC 視圖
刪除視圖
DROP VIEW 視圖1, 視圖2, ...
mysql> drop view emp_view, emp_sal_view;
Query OK, 0 rows affected (0.00 sec)
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。