詳解MySQL中的視圖
介紹
視圖(view)是一個虛擬表,非真實存在,其本質(zhì)是根據(jù)SQL語句獲取動態(tài)的數(shù)據(jù)集,并為其命名,用戶使用時只需使用視圖名稱即可獲取結(jié)果集,并可以將其當作表來使用。 數(shù)據(jù)庫中只存放了視圖的定義,而并沒有存放視圖中的數(shù)據(jù)。這些數(shù)據(jù)存放在原來的表中。 使用視圖查詢數(shù)據(jù)時,數(shù)據(jù)庫系統(tǒng)會從原來的表中取出對應的數(shù)據(jù)。因此,視圖中的數(shù)據(jù)是依賴于原來的表中的數(shù)據(jù)的。一旦表中的數(shù)據(jù)發(fā)生改變,顯示在視圖中的數(shù)據(jù)也會發(fā)生改變。
作用
簡化代碼,可以把重復使用的查詢封裝成視圖重復使用,同時可以使復雜的查詢易于理解和使用。
安全原因,如果一張表中有很多數(shù)據(jù),很多信息不希望讓所有人看到,此時可以使用視圖視,如:
社會保險基金表,可以用視圖只顯示姓名,地址,而不顯示社會保險號和工資數(shù)等,可以對不同的用戶,設(shè)定不同的視圖。
視圖的創(chuàng)建
create [or replace] [algorithm = {undefined | merge | temptable}] view view_name [(column_list)] as select_statement [with [cascaded | local] check option] 參數(shù)說明: (1)algorithm:可選項,表示視圖選擇的算法。 (2)view_name :表示要創(chuàng)建的視圖名稱。 (3)column_list:可選項,指定視圖中各個屬性的名詞,默認情況下與SELECT語句中的查詢的屬性相同。 (4)select_statement :表示一個完整的查詢語句,將查詢記錄導入視圖中。 (5)[with [cascaded | local] check option]:可選項,表示更新視圖時要保證在該視圖的權(quán)限范圍之內(nèi)。
創(chuàng)建 數(shù)據(jù)庫mydb6_view,然后在該數(shù)據(jù)庫下執(zhí)行sql腳本view_data.sql 導入數(shù)據(jù)
create database mydb6_view;
create or replace view view1_emp as select ename,job from emp; -- 查看表和視圖 show full tables;
修改視圖
修改視圖是指修改數(shù)據(jù)庫中已存在的表的定義。當基本表的某些字段發(fā)生改變時,可以通過修改視
圖來保持視圖和基本表之間一致。MySQL中通過CREATE OR REPLACE VIEW語句和ALTER VIEW語句來修改視圖。
alter view 視圖名 as select語句
alter view view1_emp as select a.deptno,a.dname,a.loc,b.ename,b.sal from dept a, emp b where a.deptno = b.deptno;
更新視圖
某些視圖是可更新的。也就是說,可以在UPDATE、DELETE或INSERT等語句中使用它們,以更
新基表的內(nèi)容。對于可更新的視圖,在視圖中的行和基表中的行之間必須具有一對一的關(guān)系。如果
視圖包含下述結(jié)構(gòu)中的任何一種,那么它就是不可更新的: 聚合函數(shù)(SUM(), MIN(), MAX(),
COUNT()等) DISTINCT GROUP BY HAVING UNION或UNION ALL 位于選擇列表中的子查詢
JOIN FROM子句中的不可更新視圖 WHERE子句中的子查詢,引用FROM子句中的表。 僅引用文
字值(在該情況下,沒有要更新的基本表)
視圖中雖然可以更新數(shù)據(jù),但是有很多的限制。一般情況下,最好將視圖作為查詢數(shù)據(jù)的虛擬表,
而不要通過視圖更新數(shù)據(jù)。因為,使用視圖更新數(shù)據(jù)時,如果沒有全面考慮在視圖中更新數(shù)據(jù)的限
制,就可能會造成數(shù)據(jù)更新失敗。
-- ---------更新視圖------- create or replace view view1_emp as select ename,job from emp; update view1_emp set ename = '周瑜' where ename = '魯肅'; -- 可以修改 insert into view1_emp values('孫權(quán)','文員'); -- 不可以插入 -- ----------視圖包含聚合函數(shù)不可更新-------------- create or replace view view2_emp as select count(*) cnt from emp; insert into view2_emp values(100); update view2_emp set cnt = 100;
-- ----------視圖包含distinct不可更新--------- create or replace view view3_emp as select distinct job from emp; insert into view3_emp values('財務'); -- ----------視圖包含goup by 、having不可更新------------------ create or replace view view4_emp as select deptno ,count(*) cnt from emp group by deptno having cnt > 2; insert into view4_emp values(30,100);
-- ----------------視圖包含union或者union all不可更新---------------- create or replace view view5_emp as select empno,ename from emp where empno <= 1005 union select empno,ename from emp where empno > 1005; insert into view5_emp values(1015,'韋小寶'); -- -------------------視圖包含子查詢不可更新-------------------- create or replace view view6_emp as select empno,ename,sal from emp where sal = (select max(sal) from emp); insert into view6_emp values(1015,'韋小寶',30000);
-- ----------------------視圖包含join不可更新----------------- create or replace view view7_emp as select dname,ename,sal from emp a join dept b on a.deptno = b.deptno; insert into view7_emp(dname,ename,sal) values('行政部','韋小寶',30000); -- --------------------視圖包含常量文字值不可更新------------------- create or replace view view8_emp as select '行政部' dname,'楊過' ename; insert into view8_emp values('行政部','韋小寶');
其他操作
重命名視圖
-- rename table 視圖名 to 新視圖名; rename table view1_emp to my_view1
刪除視圖
-- drop view 視圖名[,視圖名…]; drop view if exists view_student;
刪除視圖時,只能刪除視圖的定義,不會刪除數(shù)據(jù)。
練習
-- 1:查詢部門平均薪水最高的部門名稱 select dname from dept a ,(select deptno,avg(sal) from emp group by deptno order by avg(sal) desc limit 1) b where a.deptno = b.deptno; -- 2:查詢員工比所屬領(lǐng)導薪資高的部門名、員工名、員工領(lǐng)導編號 select * from dept x, (select a.ename aname ,a.sal asal,b.ename bname,b.sal bsal,a.deptno from emp a, emp b where a.mgr = b.empno and a.sal > b.sal) y where x.deptno = y.deptno;
-- 3:查詢工資等級為4級,2000年以后入職的工作地點為北京的員工編號、姓名和工資,并查詢出薪資在前三名的員工信息 create view xxx as SELECT e.empno,e.ename,e.sal,e.hiredate FROM emp e,dept d,salgrade s WHERE (e.sal BETWEEN losal AND hisal) AND s.GRADE = 4 AND year(e.hiredate) > '2000' AND d.loc = '北京'; select * from ( select *, dense_rank() over(order by sal desc ) rn from xxx ) t where t.rn <=3;
到此這篇關(guān)于MySQL的視圖的文章就介紹到這了,更多相關(guān)MySQL視圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MySQL 查詢結(jié)果以百分比顯示簡單實現(xiàn)
用到了MySQL字符串處理中的兩個函數(shù)concat()和left()實現(xiàn)查詢結(jié)果以百分比顯示,具體示例代碼如下,感興趣的朋友可以學習下2013-07-07win10下mysql 5.7.23 winx64安裝配置方法圖文教程
這篇文章主要為大家詳細介紹了win10下mysql 5.7.23 winx64安裝配置方法圖文教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-09-09MySQL5.6 數(shù)據(jù)庫主從同步安裝與配置詳解(Master/Slave)
本篇文章主要介紹了MySQL5.6 數(shù)據(jù)庫主從同步安裝與配置詳解,具有一定的參考價值,有興趣的可以了解一下。2017-01-01