MySQL視圖介紹

{"type":"doc","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"前言:"},{"type":"text","text":" "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在MySQL中,視圖可能是我們最常用的數據庫對象之一了。那麼你知道視圖和表的區別嗎?你知道創建及使用視圖要注意哪些點嗎?可能很多人對視圖只是一知半解,想詳細瞭解視圖的同學看過來喲,本篇文章會詳細介紹視圖的概念、創建及使用方法。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"1.視圖定義及簡單介紹"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"視圖是基於 SQL 語句的結果集的可視化的表,即視圖是一個虛擬存在的表,可以包含表的全部或者部分記錄,也可以由一個表或者多個表來創建。使用視圖就可以不用看到數據表中的所有數據,而是隻想得到所需的數據。當我們創建一個視圖的時候,實際上是在數據庫裏執行了SELECT語句,SELECT語句包含了字段名稱、函數、運算符,來給用戶顯示數據。 "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"視圖的數據是依賴原來表中的數據的,所以原來的表的數據發生了改變,那麼顯示的視圖的數據也會跟着改變,例如向數據表中插入數據,那麼在查看視圖的時候,會發現視圖中也被插入了同樣的數據。視圖實際上是由預定義的查詢形式的表所組成的。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"2.視圖創建及使用方法"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"創建視圖標準語法:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"sql"},"content":[{"type":"text","text":"CREATE\n [OR REPLACE]\n [ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE}]\n [DEFINER = user]\n [SQL SECURITY { DEFINER | INVOKER }]\n VIEW view_name [(column_list)]\n AS select_statement\n [WITH [CASCADED | LOCAL] CHECK OPTION]"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"語法解讀:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"1)OR REPLACE:表示替換已有視圖,如果該視圖不存在,則CREATE OR REPLACE VIEW與CREATE VIEW相同。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"2)ALGORITHM:表示視圖選擇算法,默認算法是UNDEFINED(未定義的):MySQL自動選擇要使用的算法 ;merge合併;temptable臨時表,一般該參數不顯式指定。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"3)DEFINER:指出誰是視圖的創建者或定義者,如果不指定該選項,則創建視圖的用戶就是定義者。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"4)SQL SECURITY:SQL安全性,默認爲DEFINER。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"5)select_statement:表示select語句,可以從基表或其他視圖中進行選擇。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"6)WITH CHECK OPTION:表示視圖在更新時保證約束,默認是CASCADED。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"其實我們日常創建視圖時,無需指定每個參數,一般情況下,建議這樣創建視圖:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"sql"},"content":[{"type":"text","text":"create view [(column_list)]\nas select語句\nwith check option;"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"下面給出幾個具體創建示例:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"sql"},"content":[{"type":"text","text":"# 單表視圖\nmysql> create view v_F_players(編號,名字,性別,電話)\n -> as\n -> select PLAYERNO,NAME,SEX,PHONENO from PLAYERS\n -> where SEX='F'\n -> with check option;\nQuery OK, 0 rows affected (0.00 sec)\n\nmysql> desc v_F_players;\n+--------+----------+------+-----+---------+-------+\n| Field | Type | Null | Key | Default | Extra |\n+--------+----------+------+-----+---------+-------+\n| 編號 | int(11) | NO | | NULL | |\n| 名字 | char(15) | NO | | NULL | |\n| 性別 | char(1) | NO | | NULL | |\n| 電話 | char(13) | YES | | NULL | |\n+--------+----------+------+-----+---------+-------+\n4 rows in set (0.00 sec)\n\nmysql> select * from v_F_players;\n+--------+-----------+--------+------------+\n| 編號 | 名字 | 性別 | 電話 |\n+--------+-----------+--------+------------+\n| 8 | Newcastle | F | 070-458458 |\n| 27 | Collins | F | 079-234857 |\n| 28 | Collins | F | 010-659599 |\n| 104 | Moorman | F | 079-987571 |\n| 112 | Bailey | F | 010-548745 |\n+--------+-----------+--------+------------+\n5 rows in set (0.02 sec)\n\n# 多表視圖\nmysql> create view v_match\n -> as \n -> select a.PLAYERNO,a.NAME,MATCHNO,WON,LOST,c.TEAMNO,c.DIVISION\n -> from \n -> PLAYERS a,MATCHES b,TEAMS c\n -> where a.PLAYERNO=b.PLAYERNO and b.TEAMNO=c.TEAMNO;\nQuery OK, 0 rows affected (0.03 sec)\n\nmysql> select * from v_match;\n+----------+-----------+---------+-----+------+--------+----------+\n| PLAYERNO | NAME | MATCHNO | WON | LOST | TEAMNO | DIVISION |\n+----------+-----------+---------+-----+------+--------+----------+\n| 6 | Parmenter | 1 | 3 | 1 | 1 | first |\n| 44 | Baker | 4 | 3 | 2 | 1 | first |\n| 83 | Hope | 5 | 0 | 3 | 1 | first |\n| 112 | Bailey | 12 | 1 | 3 | 2 | second |\n| 8 | Newcastle | 13 | 0 | 3 | 2 | second |\n+----------+-----------+---------+-----+------+--------+----------+\n5 rows in set (0.04 sec)"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"視圖在使用時和基礎表一樣,比如我們可以使用 select "},{"type":"text","marks":[{"type":"italic"}],"text":" from view_name 或 select "},{"type":"text","text":" from view_name where ... ,視圖可以將我們不需要的數據過濾掉,將相關的列名用我們自定義的列名替換。視圖作爲一個訪問接口,不管基表的表結構和表名有多複雜。一般情況下視圖只用於查詢,視圖本身沒有數據,因此對視圖進行的dml操作最終都體現在基表中,對視圖進行delete、update、insert操作,原表同樣會更新,drop視圖原表不會變,視圖不可以truncate。但是一般情況下我們要避免更新視圖,dml操作可以直接對原表進行更新。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"3.視圖相關最佳實踐"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"下面簡單介紹下視圖的優點,通過這些優點我們很容易總結出視圖的適用場景。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"1)簡單:使用視圖的用戶完全不需要關心後面對應的表的結構、關聯條件和篩選條件,對用戶來說已經是過濾好的複合條件的結果集。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"2)安全:使用視圖的用戶只能訪問他們被允許查詢的結果集,對錶的權限管理並不能限制到某個行某個列,但是通過視圖就可以簡單的實現。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"3)數據獨立:一旦視圖的結構確定了,可以屏蔽表結構變化對用戶的影響,源表增加列對視圖沒有影響;源表修改列名,則可以通過修改視圖來解決,不會造成對訪問者的影響。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"總而言之,使用視圖的大部分情況是爲了保障數據安全性,提高查詢效率。比如說我們經常用到幾個表的關聯結果,那麼我們就可以使用視圖來處理,或者說第三方程序需要調用我們的業務庫,可以按需創建視圖給第三方程序查詢。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"對於日常使用及維護視圖的過程中,個人總結出以下幾點實踐,可供參考:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"視圖命名建議統一前綴,比如以v"},{"type":"text","marks":[{"type":"italic"}],"text":"或view"},{"type":"text","text":"開頭,便於識別。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"SQL SECURITY使用默認的DEFINER,表示已視圖定義者的權限去查詢視圖。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"視圖定義者建議使用相關程序用戶。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"視圖不要關聯太多的表,造成數據冗餘。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"查詢視圖時要附帶條件,不建議每次都查詢出所有數據。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"視圖遷移要注意在新環境有該視圖的定義者用戶。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"不要直接更新視圖中的數據,視圖只作查詢。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"總結:"},{"type":"text","text":" "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"視圖在MySQL中經常會用到,本篇文章介紹了視圖的概念以及創建方法,延伸而來,後續又講述了視圖的使用場景及優點。可能在使用時感覺不出視圖和表的區別,其實這裏面的門道還有很多,在這裏建議視圖只作查詢使用,按照規範來,視圖會帶來很大的便捷。希望這篇文章對你有幫助。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章