概念
??視圖是一種虛擬存在的表,對于視圖的用戶來說,基本上跟使用正常的表一樣。視圖在數(shù)據(jù)庫中是不存在的,視圖中的數(shù)據(jù)是動態(tài)生成的。
??視圖相對于普通表的優(yōu)勢:
-
簡單:使用視圖不需要關(guān)心后面的表的對應(yīng)結(jié)構(gòu)條件,對于使用者來說,視圖是過濾好的結(jié)果集。
-
數(shù)據(jù)獨(dú)立:一旦視圖結(jié)構(gòu)確定,對實際表的改變對視圖使用者是沒有影響的。
視圖操作
視圖操作分為創(chuàng)建視圖、修改視圖、視圖視圖、查看視圖定義。
創(chuàng)建視圖,修改視圖
#創(chuàng)建視圖 create?[or?replace]?[algorithm={undefined|merge|temptable}] view?view_name[(column_list)] as?select_statement [with[cascaded|local]?check?option] #?修改視圖 alter?[algorithm={undefined|merge|temptable}] view?view_name[(column_list)] as?select_statement [with[cascaded|local]?check?option]
舉例:
create?view?view_test?as select?t1.sid,t1.username,t2.department from?test1?t1?left?join?test2?t2?on?t1.sid=t2.sid;
創(chuàng)建視圖
說明:
-
from關(guān)鍵字后面不能包含子查詢;
-
不能更新的視圖:包含聚合函數(shù)/group/distinct/having/union,常量視圖,select中包含子查詢,jion,from一個不能更新的視圖,where中子查詢引用from子句中的表。
-
with[cascaded|local] check option 絕對是否允許更新數(shù)據(jù)使記錄不再滿足視圖條件。其中l(wèi)ocal-只需滿足本視圖條件就可以更新、cascaded-必須滿足所有針對該視圖的所有視圖的條件才可以更新。默認(rèn)為cascaded。
查看視圖數(shù)據(jù)
同普通表
select?*?from?view_test;
查看視圖數(shù)據(jù)
刪除視圖
#刪除視圖 drop?view?[if?exists]?view_name?[,view_name2]...[restrict|cascade] #舉例 drop?view?view_test;
查看視圖狀態(tài)
從MySQL5.1開始,使用show tables命令的時候不僅顯示表名還顯示視圖名稱。
【相關(guān)推薦】
1.?視圖
2.?視圖
3.?視圖
? 版權(quán)聲明
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載。
THE END