資料庫:mysql內建功能-視圖

來源:互聯網
上載者:User

標籤:hang   warnings   尋找   sql語句   複雜   highlight   dba   語文   microsoft   

一 視圖

視圖是一個虛擬表(非真實存在),其本質是【根據SQL語句擷取動態資料集,並為其命名】,使用者使用時只需使用【名稱】即可擷取結果集,可以將該結果集當做表來使用。

使用視圖我們可以把查詢過程中的暫存資料表摘出來,用視圖去實現,這樣以後再想操作該暫存資料表的資料時就無需重寫複雜的sql了,直接去視圖中尋找即可,但視圖有明顯地效率問題,並且視圖是存放在資料庫中的,如果我們程式中使用的sql過分依賴資料庫中的視圖,即強耦合,那就意味著擴充sql極為不便,因此並不推薦使用

#兩張有關係的表mysql> select * from course;+-----+--------+------------+| cid | cname  | teacher_id |+-----+--------+------------+|   1 | 生物 |          1 ||   2 | 體育 |          1 ||   3 | 物理 |          2 ||   4 | 語文 |          3 ||   5 | 數學 |          4 ||   6 | 英語 |          5 ||   7 | 地理 |          2 |+-----+--------+------------+7 rows in set (0.00 sec)mysql> select * from teacher;+-----+-----------+| tid | tname     |+-----+-----------+|   1 | 張三    ||   2 | 李四    ||   3 | 王五    ||   4 | egon      ||   5 | 張無忌 |+-----+-----------+5 rows in set (0.07 sec)# 查詢張三教授的課程名mysql> select cname from course where teacher_id =(select tid from teacher where tname=‘張三‘);+--------+| cname  |+--------+| 生物 || 體育 |+--------+2 rows in set (0.00 sec)# 子查詢出暫存資料表,作為teacher_id等判斷依據select tid from teacher where tname=‘張三‘

  

一 建立視圖

# 文法:create view 視圖名稱 AS sql語句mysql> create view teacher_view as select tid from teacher where tname = ‘張三‘;Query OK, 0 rows affected (0.15 sec)# 於是查詢張三教授的課程名的sql可以改寫為mysql> select cname from course where teacher_id = (select tid from teacher_view);+--------+| cname  |+--------+| 生物 || 體育 |+--------+注意注意注意:#1. 使用視圖以後就無需每次都重寫子查詢的sql,但是這麼效率並不高,還不如我們寫子查詢的效率高#2. 而且有一個致命的問題:視圖是存放到資料庫裡的,如果我們程式中的sql過分依賴於資料庫中存放的視圖,那麼意味著,一旦sql需要修改且涉及到視圖的部分,則必須去資料庫中進行修改,而通常在公司中資料庫有專門的DBA負責,你要想完成修改,必須付出大量的溝通成本DBA可能才會幫你完成修改,極其地不方便

  

二 使用視圖

mysql> select * from course;
+-----+--------+------------+
| cid | cname | teacher_id |
+-----+--------+------------+
| 1 | 生物 | 1 |
| 2 | 體育 | 1 |
| 3 | 物理 | 2 |
| 4 | 語文 | 3 |
| 5 | 數學 | 4 |
| 6 | 英語 | 5 |
| 7 | 地理 | 2 |
+-----+--------+------------+

mysql> create view course_view  as select * from course;  Query OK, 0 rows affected (0.05 sec)mysql> select * from course_view;+-----+--------+------------+| cid | cname  | teacher_id |+-----+--------+------------+|   1 | 生物 |          1 ||   2 | 體育 |          1 ||   3 | 物理 |          2 ||   4 | 語文 |          3 ||   5 | 數學 |          4 ||   6 | 英語 |          5 ||   7 | 地理 |          2 |+-----+--------+------------+7 rows in set (0.00 sec)mysql> update course_view set cname = ‘化學‘ where cname = ‘生物‘;Query OK, 1 row affected (0.08 sec)Rows matched: 1  Changed: 1  Warnings: 0mysql> insert into course_view values(8,‘曆史‘,3);  # 往視圖裡面插入資料Query OK, 1 row affected (0.02 sec)mysql> select * from course;   # 發現原始表的記錄也跟著修改了+-----+--------+------------+| cid | cname  | teacher_id |+-----+--------+------------+|   1 | 化學 |          1 ||   2 | 體育 |          1 ||   3 | 物理 |          2 ||   4 | 語文 |          3 ||   5 | 數學 |          4 ||   6 | 英語 |          5 ||   7 | 地理 |          2 ||   8 | 曆史 |          3 |+-----+--------+------------+8 rows in set (0.00 sec)

  

三 修改視圖
文法: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)

  

四 刪除視圖
文法: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.