1、什麼是視圖
視圖(view)是一種虛擬存在的表,是一個邏輯表,自己並不包含數據。做爲一個select語句保存在數據字典中的。
經過視圖,能夠展示基表的部分數據;視圖數據來自定義視圖的查詢中使用的表,使用視圖動態生成。
基表:用來建立視圖的表叫作基表base tablemysql
2、建立視圖算法
CREATE [OR REPLACE] [ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE}] [DEFINER = { user | CURRENT_USER }] [SQL SECURITY { DEFINER | INVOKER }] VIEW view_name [(column_list)] AS select_statement [WITH [CASCADED | LOCAL] CHECK OPTION]
(1) OR REPLACE:表示替換已有視圖
(2) ALGORITHM:表示視圖選擇算法,默認算法是UNDEFINED(未定義的):MySQL自動選擇要使用的算法 ;merge合併;temptable臨時表
(3) DEFINER選項:指出誰是視圖的建立者或定義者
(4) SQL SECURITY選項:要查詢一個視圖,首先必需要具備對視圖的select權限
(5) select_statement:表示select語句
(6) [WITH [CASCADED | LOCAL] CHECK OPTION]:表示視圖在更新時保證在視圖的權限範圍以內
cascade是默認值,表示更新視圖的時候,要知足視圖和表的相關條件
local表示更新視圖的時候,要知足該視圖定義的一個條件便可
推薦使用WHIT [CASCADED|LOCAL] CHECK OPTION選項,能夠保證數據的安全性 示例:
在單表上建立視圖sql
mysql> create view v_F_players(編號,名字,性別,電話) -> as -> select PLAYERNO,NAME,SEX,PHONENO from PLAYERS -> where SEX='F' -> with check option; Query OK, 0 rows affected (0.00 sec) mysql> desc v_F_players; +--------+----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+----------+------+-----+---------+-------+ | 編號 | int(11) | NO | | NULL | | | 名字 | char(15) | NO | | NULL | | | 性別 | char(1) | NO | | NULL | | | 電話 | char(13) | YES | | NULL | | +--------+----------+------+-----+---------+-------+ mysql> select * from v_F_players; +--------+-----------+--------+------------+ | 編號 | 名字 | 性別 | 電話 | +--------+-----------+--------+------------+ | 8 | Newcastle | F | 070-458458 | | 27 | Collins | F | 079-234857 | | 28 | Collins | F | 010-659599 | | 104 | Moorman | F | 079-987571 | | 112 | Bailey | F | 010-548745 | +--------+-----------+--------+------------+
在多表上建立視圖數據庫
mysql> create view v_match -> as -> select a.PLAYERNO,a.NAME,MATCHNO,WON,LOST,c.TEAMNO,c.DIVISION -> from -> PLAYERS a,MATCHES b,TEAMS c -> where a.PLAYERNO=b.PLAYERNO and b.TEAMNO=c.TEAMNO; Query OK, 0 rows affected (0.03 sec) mysql> select * from v_match; +----------+-----------+---------+-----+------+--------+----------+ | PLAYERNO | NAME | MATCHNO | WON | LOST | TEAMNO | DIVISION | +----------+-----------+---------+-----+------+--------+----------+ | 6 | Parmenter | 1 | 3 | 1 | 1 | first | | 44 | Baker | 4 | 3 | 2 | 1 | first | | 83 | Hope | 5 | 0 | 3 | 1 | first | | 112 | Bailey | 12 | 1 | 3 | 2 | second | | 8 | Newcastle | 13 | 0 | 3 | 2 | second | +----------+-----------+---------+-----+------+--------+----------+
若是建立視圖時不明確指定視圖的列名,那麼列名就和定義視圖的select子句中的列名徹底相同;
若是顯式的指定視圖的列名就按照指定的列名。
注意:顯示指定視圖列名,要求視圖名後面的列的數量必須匹配select子句中的列的數量。安全
3、查看視圖函數
(1) 使用show create view語句查看視圖信息spa
mysql> show create view v_F_players\G; *************************** 1. row *************************** View: v_F_players Create View: CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v_F_players` AS select `PLAYERS`.`PLAYERNO` AS `編號`,`PLAYERS`.`NAME` AS `名字`,`PLAYERS`.`SEX` AS `性別`,`PLAYERS`.`PHONENO` AS `電話` from `PLAYERS` where (`PLAYERS`.`SEX` = 'F') WITH CASCADED CHECK OPTION character_set_client: utf8 collation_connection: utf8_general_ci 1 row in set (0.00 sec)
(2) 視圖一旦建立完畢,就能夠像一個普通表那樣使用,視圖主要用來查詢
mysql> select * from view_name;code
4、更新視圖orm
(1) CREATE OR REPLACE VIEW語句修改視圖
在視圖存在的狀況下可對視圖進行修改,視圖不在的狀況下可建立視圖ci
(2) ALTER語句修改視圖
ALTER [ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE}] [DEFINER = { user | CURRENT_USER }] [SQL SECURITY { DEFINER | INVOKER }] VIEW view_name [(column_list)] AS select_statement [WITH [CASCADED | LOCAL] CHECK OPTION]
(3) DML操做更新視圖
(1)由於視圖自己沒有數據,所以對視圖進行的dml操做最終都體如今基表中
mysql> create view v_student as select * from student; mysql> select * from v_student; +--------+--------+------+ | 學號 | name | sex | +--------+--------+------+ | 1 | 張三 | M | | 2 | 李四 | F | | 5 | 王五 | NULL | +--------+--------+------+ mysql> update v_student set name='錢六' where 學號='1'; mysql> select * from student; +--------+--------+------+ | 學號 | name | sex | +--------+--------+------+ | 1 | 錢六 | M | | 2 | 李四 | F | | 5 | 王五 | NULL | +--------+--------+------+
不是全部的視圖均可以作DML操做,有下列內容之一,視圖不能作DML操做:
①select子句中包含distinct
②select子句中包含組函數
③select語句中包含group by子句
④select語句中包含order by子句
⑤select語句中包含union 、union all等集合運算符
⑥where子句中包含相關子查詢
⑦from子句中包含多個表
⑧若是視圖中有計算列,則不能更新
⑨若是基表中有某個具備非空約束的列未出如今視圖定義中,則不能作insert操做
(2) drop刪除視圖
刪除視圖是指刪除數據庫中已存在的視圖,刪除視圖時,只能刪除視圖的定義,不會刪除數據,也就是說不動基表
mysql> drop view v_student;
若是視圖不存在,則拋出異常;使用IF EXISTS選項使得刪除不存在的視圖時不拋出異常。
5、使用WITH CHECK OPTION約束
對於能夠執行DML操做的視圖,定義時能夠帶上WITH CHECK OPTION約束,
做用:
對視圖所作的DML操做的結果,不能違反視圖的WHERE條件的限制示例:建立視圖,包含1960年以前出生的全部球員(老兵)
mysql> create view v_veterans -> as -> select * from PLAYERS -> where birth_date < '1960-01-01' -> with check option; Query OK, 0 rows affected (0.01 sec) mysql> select * from v_veterans; +----------+---------+----------+------------+-----+--------+----------------+---------+----------+-----------+------------+----------+ | PLAYERNO | NAME | INITIALS | BIRTH_DATE | SEX | JOINED | STREET | HOUSENO | POSTCODE | TOWN | PHONENO | LEAGUENO | +----------+---------+----------+------------+-----+--------+----------------+---------+----------+-----------+------------+----------+ | 2 | Everett | R | 1948-09-01 | M | 1975 | Stoney Road | 43 | 3575NH | Stratford | 070-237893 | 2411 | | 39 | Bishop | D | 1956-10-29 | M | 1980 | Eaton Square | 78 | 9629CD | Stratford | 070-393435 | NULL | | 83 | Hope | PK | 1956-11-11 | M | 1982 | Magdalene Road | 16A | 1812UP | Stratford | 070-353548 | 1608 | +----------+---------+----------+------------+-----+--------+----------------+---------+----------+-----------+------------+----------+
此時,使用update對視圖進行修改:
mysql> update v_veterans -> set BIRTH_DATE='1970-09-01' -> where PLAYERNO=39; ERROR 1369 (HY000): CHECK OPTION failed 'TENNIS.v_veterans'
由於違反了視圖中的WHERE birth_date < '1960-01-01'子句,因此拋出異常; 使用WITH CHECK OPTION約束時,(不指定選項則默認是CASCADED): ①WITH CASCADED CHECK OPTION:檢查全部的視圖 ②WITH LOCAL CHECK OPTION:只檢查將要更新的視圖自己