標籤:io on 資料 問題 ad ef as 資料庫 sql
視圖,把基本表的某些資料群組合起來構成一個虛擬表的一種形式,之所以叫虛擬,是因為只有當視圖用於一條語句中的時候,它才能存在。同時如果對視圖中的資料進行修改,會同時修改到基本表中的資料。建立視圖:create [or replace] view view_name [column1,column2..]as 表的運算式as:建立一個視圖towns,1.包含了 players 表中的所有城市名,2.接下來並查詢這個虛擬表的內容1. create view towns as select town from players2. select * from towns以上是先建立視圖towns,再查詢檢視,其中視圖的列名稱沒有顯式寫出來,所以預設用的是後面查詢的欄位作了列的名稱欄位,as之後的查詢結果作為視圖的資料,可以顯式指定視圖列名as:create view cplays (uid,name,job) asselect id,name,prefessionfrom works從視圖 cplays 中刪除 id = 100的人delete from cplayswhere uid = 100此時基本表中的 id = 100的人同時會被刪除 允許select 中的一個運算式為函數或者計算as:create or replace view result asselect grand, count(*)from scoregroup by grand查詢以上視圖時:select * from result返回的欄位有 grand,count(*)or replace 是當視圖存在時,則會更新這個視圖更新視圖如果沒有約束的話,如下:create view t_view asselect id,name,jobfrom test.workwhere id < 4;update t_view set name = ‘xxx‘where id = 4;以上語句會使 id = 4 的name 變為‘xxx‘,沒有問題,但當加上約束條件時,如果不滿足條件,則不允許相應的操作as:create view t_view asselect id,name,jobfrom test.workwhere id < 4with check option;update t_view set name = ‘xxx‘where id = 4;以上更新會失敗,因為視圖用了約束,後面的所查詢的 id不在建立的視圖中,所以無法更新相關資料,又因為視圖是可以嵌套的,所以如果只當對前的視圖的操作作檢測,用with local check option約束條件即可,此時只會對當前的視圖條件作檢測,如果是對其相關的嵌套的視圖也作檢查,則約束條件用 with[cascaded] check option,預設是 cascaded 的刪除視圖drop view [if exitsts] view_name
資料庫之mysql 視圖