MySQL之視圖

來源:互聯網
上載者:User

標籤:修改   組成   sele   play   The   暫存資料表   error   teacher   style   

一、視圖的定義
  視圖是虛擬表或邏輯表,它被定義為具有串連的SQL SELECT查詢語句。因為資料庫檢視與資料庫表類似,它由行和列組成,因此可以根據資料庫表查詢資料。其內容由查詢定義。
但是,視圖並不在資料庫中以儲存的資料值集形式存在,行和列資料來自由定義視圖的查詢所引用的表,並且在引用視圖時動態產生。簡單的來說視圖是由其定義結果組成的表;
二、視圖的優缺點

優點:

  1,資料庫檢視允許簡化複雜查詢,通過資料庫檢視,您只需使用簡單的SQL語句,而不是使用具有多個串連的複雜的SQL語句。

       2,安全性。一般是這樣做的:建立一個視圖,定義好該視圖所操作的資料。之後將使用者權限與視圖綁定。這樣的方式是使用到了一個特性:grant語句可以針對視圖進行授予許可權。

缺點: 

  1、效能:從資料庫檢視查詢資料可能會很慢,特別是如果視圖是基於其他視圖建立的。
 2、表依賴關係:將根據資料庫的基礎資料表建立一個視圖。每當更改與其相關聯的表的結構時,都必須更改視圖。
三、建立視圖

文法:

CREATE VIEW 視圖名稱 AS  SQL語句

ex:

#兩張有關係的表mysql> select * from course;+-----+--------+------------+| cid | cname  | teacher_id |+-----+--------+------------+|   1 | 生物   |          1 ||   2 | 物理   |          2 ||   3 | 體育   |          3 ||   4 | 美術   |          2 |+-----+--------+------------+rows in set (0.00 sec)mysql> select * from teacher;+-----+-----------------+| tid | tname           |+-----+-----------------+|   1 | 張磊老師        ||   2 | 李平老師        ||   3 | 劉海燕老師      ||   4 | 朱雲海老師      ||   5 | 李傑老師        |+-----+-----------------+rows in set (0.00 sec)#查詢李平老師教授的課程名mysql> select cname from course where teacher_id = (select tid from teacher where tname=‘李平老師‘);+--------+| cname  |+--------+| 物理   || 美術   |+--------+rows in set (0.00 sec)#子查詢出暫存資料表,作為teacher_id等判斷依據select tid from teacher where tname=‘李平老師‘
暫存資料表
create view teacher_view as select tid from teacher where tname=‘李平老師‘;#於是查詢李平老師教授的課程名的sql可以改寫為mysql> select cname from course where teacher_id = (select tid from teacher_view);+--------+| cname  |+--------+| 物理   || 美術   |+--------+rows in set (0.00 sec)
暫存資料表建立為試圖四、使用視圖
mysql> create view tt as select * from course left join teacher on teacher.tid = course.teacher_id;Query OK, 0 rows affected (0.02 sec)mysql> select * from tt;+-----+--------+------------+------+-----------------+| cid | cname  | teacher_id | tid  | tname           |+-----+--------+------------+------+-----------------+|   1 | 王五   |          1 |    1 | 張磊老師        ||   2 | 王五   |          2 |    2 | 麗萍老師        ||   4 | 王五   |          2 |    2 | 麗萍老師        ||   5 | 王五   |          2 |    2 | 麗萍老師        ||   6 | 王五   |          2 |    2 | 麗萍老師        ||   3 | 王五   |          3 |    3 | 王海燕老師      |+-----+--------+------------+------+-----------------+rows in set (0.01 sec)不能修改視圖的資料,驗證為例:mysql> insert into tt values(7,‘哈哈‘,2,4,‘張三丰老師‘);ERROR 1471 (HY000): The target table tt of the INSERT is not insertable-into
View Code五、修改視圖
# 文法:ALTER VIEW 視圖名稱 AS SQL語句mysql> alter view teacher_view as select * from course where cid>3;Query OK, 0 rows affected (0.04 sec)mysql> select * from teacher_view;+-----+-------+------------+| cid | cname | teacher_id |+-----+-------+------------+|   4 | xxx   |          2 ||   5 | yyy   |          2 |+-----+-------+------------+rows in set (0.00 sec)
View Code七、刪除視圖
# 文法:DROP VIEW 視圖名稱DROP VIEW teacher_view

 

 
 
 

MySQL之視圖

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.