MySql視圖

MySql視圖mysql

1、爲何使用視圖

在數據庫中關於數據的查詢有時候很是麻煩,例如錶鏈接、子查詢等,這種查詢會很是痛苦,由於邏輯比較複雜、編寫語句比較多。當這種查詢須要重複使用時,則不會每次都能編寫正確,從而下降了數據庫的實用性。sql

在具體操做表前,有時候要求只能操做部分字段,而不是所有字段。數據庫

爲了提升複雜sql語句的複用性和表操做的安全性,mysql數據庫管理系統提供 視圖特性。所謂的視圖,本質上是一種虛擬表,其內容與真實的表類似,包含一系列帶有名稱的列和行數據。可是,視圖並不在數據庫中以存儲的數據值形式存在。行和列的數據來自自定義視圖的查詢所引用的基本表,而且在具體引用視圖時動態生成。安全

視圖主要關注某些特定數據和他們所查詢的數據內容,這樣只能看到視圖中所定義的數據,而不是視圖所引用表中的數據,從而提升了數據庫中數據的安全性。微信

視圖的特色以下:ide

l  視圖的列能夠來自不一樣的表,是表的抽象和在邏輯意義上創建的新關係函數

l  視圖是由基本表(實表)產生的表(虛表)微信支付

l  視圖的創建和刪除不影響基本表ui

l  對視圖內容的更新(添加、刪除和修改)直接影響基本表this

l  當視圖來自多個基本表時,不容許添加和刪除數據

2、建立視圖

視圖的操做包括建立視圖,查看視圖、刪除視圖和修改視圖。在建立視圖時,首先要確保擁有CREATE VIEW的權限,而且同時確保對建立視圖所引用的表也具備相應的權限。

2.1、建立視圖的語法形式

         雖然視圖能夠被當作是一種虛擬表,可是在其物理上是不存在的,即數據庫管理系統沒有專門的位置爲視圖存儲數據。根據視圖的概念能夠發現其數據來源於查詢語句,所以建立視圖的語法爲:

         CREATE VIEW VIEW_NAME AS 查詢語句

         和建立表同樣,視圖名不能和表名、也不能和其餘的視圖名稱重名。根據上述語法能夠發現,視圖的功能實際上就是封裝了複雜的查詢語句。

注:在SQL語句命名規範中,視圖通常以view_XXX或者v_XXX的樣式來命名。例子以下:

         【列子1】建立students表中年齡小於20的視圖名字爲s

                  MySQL [yuancheng]> create view s as select * from students where Age < 20;

Query OK, 0 rows affected (0.09 sec)

 

MySQL [yuancheng]>

         查看全部表是否有s

         MySQL [yuancheng]> show tables;

+---------------------+

| Tables_in_yuancheng |

+---------------------+

| classes             |

| coc                 |

| courses             |

| s                   |

| scores              |

| students            |

| teachers            |

| toc                 |

+---------------------+

8 rows in set (0.03 sec)

 

MySQL [yuancheng]>

查看s表中的數據是否知足條件

         MySQL [yuancheng]> select * from s;

+-------+--------------+-----+--------+---------+-----------+

| StuID | Name         | Age | Gender | ClassID | TeacherID |

+-------+--------------+-----+--------+---------+-----------+

|     7 | Xi Ren       |  19 | F      |       3 |         6 |

|     8 | Lin Daiyu    |  17 | F      |       7 |         6 |

|    10 | Yue Lingshan |  19 | F      |       3 |         6 |

|    12 | Wen Qingqing |  19 | F      |       1 |         2 |

|    14 | Lu Wushuang  |  17 | F      |       3 |         6 |

|    15 | Duan Yu      |  19 | M      |       4 |         6 |

|    19 | Xue Baochai  |  18 | F      |       6 |         6 |

|    20 | Diao Chan    |  19 | F      |       7 |         6 |

+-------+--------------+-----+--------+---------+-----------+

8 rows in set (0.02 sec)

 

MySQL [yuancheng]>

在上面的代碼語句中建立了一個名爲s的視圖,經過對代碼的觀察能夠發現,實際上sql語句裏寫的就是一個表查詢語句,只不過是把這個查詢語句封裝起來從新起了一個別名,以便之後能夠重複使用。

2.2、建立各類視圖

         因爲視圖的功能其實是封裝查詢語句,那麼是否是任何形式的查詢語句均可以封裝在視圖裏面呢,經過實例介紹各類形式的視圖。

2.2.1、建立查詢常量語句的視圖,即常量視圖

         mysql> create view view_test1 as select 3;

Query OK, 0 rows affected (0.02 sec)

 

mysql> select * from view_test1;

+---+

| 3 |

+---+

| 3 |

+---+

1 row in set (0.02 sec)

 

mysql>

2.2.2、建立聚合函數視圖

         好比 sum maxmincount

mysql> create view  view_test2  as select sum(Age) from students;

Query OK, 0 rows affected (0.03 sec)

 

mysql> select * from view_test2;

+----------+

| sum(Age) |

+----------+

|      670 |

+----------+

1 row in set (0.01 sec)

 

mysql>

2.2.3、建立排序功能(order by)的查詢語句功能

         mysql> create view view_test4 as select * from students where Age<=19 order by Age desc;

Query OK, 0 rows affected (0.02 sec)

 

mysql>

mysql> select * from view_test4;

+-------+--------------+-----+--------+---------+-----------+

| StuID | Name         | Age | Gender | ClassID | TeacherID |

+-------+--------------+-----+--------+---------+-----------+

|     7 | Xi Ren       |  19 | F      |       3 |         6 |

|    10 | Yue Lingshan |  19 | F      |       3 |         6 |

|    12 | Wen Qingqing |  19 | F      |       1 |         2 |

|    15 | Duan Yu      |  19 | M      |       4 |         6 |

|    20 | Diao Chan    |  19 | F      |       7 |         6 |

|    19 | Xue Baochai  |  18 | F      |       6 |         6 |

|     8 | Lin Daiyu    |  17 | F      |       7 |         6 |

|    14 | Lu Wushuang  |  17 | F      |       3 |         6 |

+-------+--------------+-----+--------+---------+-----------+

8 rows in set (0.01 sec)

 

mysql>

2.2.4、建立內鏈接查詢語句視圖

         mysql> create view view_deal as SELECT farmer_deal_record.order_id ,order_info.create_time ,vendor.`name` ,(CASE vendor.type WHEN 1 THEN '' WHEN 2 THEN '' END),vendor.real_name,vendor.phone ,operator.`name` ,operator.phone,farmer_deal_record.farmer_id ,(CASE farmer.vendor WHEN 0 THEN '' WHEN 1 THEN '' END) ,farmer.`name` ,farmer.phone , order_info.contact_name ,order_info.contact_phone ,order_info.crop ,order_info.fee_real ,order_info.area_real,farmer_deal_record.pay_type ,farmer_deal_record.deal_type ,farmer_deal_record.deal_time ,farmer_deal_record.amount ,farmer_deal_record.fee_deal, farmer_deal_record.deduction  FROM farmer_deal_record,order_info,vendor,farmer,operator_order,operator WHERE NOT deal_type=102 AND farmer_deal_record.order_id=order_info.id AND order_info.vendor_id=vendor.id AND farmer_deal_record.farmer_id=farmer.id AND order_info.id=operator_order.order_id AND operator_order.operator_id=operator.id ORDER BY farmer_deal_record.deal_time,vendor.`name` DESC ;

Query OK, 0 rows affected (0.00 sec)

 

mysql>

         mysql> select * from view_deal;

+--------------------+---------------------+--------------------------------------------+--------------------+--------------------+--------------------------+--------------+--------------------+----------+--------------------+--------------+--------------+-----------------+-----------------------+-----------+--------------+--------------+--------------+--------------+---------------------+-----------+--------------+--------------------+

| 訂單ID             | 下單時間            | 嗡服中心                                   | 嗡服中心性質       | 嗡服中心姓名       | 嗡服中心聯繫電話         | 飛手姓名     | 飛手聯繫電話       | 農戶ID   | 是否爲×××商       | 農戶姓名     | 農戶電話     | 訂單聯繫人      | 訂單聯繫人電話        | 農做物    | 實際費用     | 實際面積     | 支付類型     | 交易類型     | 支付時間            | 總金額    | 支付金額     | 錢包抵扣金額       |

+--------------------+---------------------+--------------------------------------------+--------------------+--------------------+--------------------------+--------------+--------------------+----------+--------------------+--------------+--------------+-----------------+-----------------------+-----------+--------------+--------------+--------------+--------------+---------------------+-----------+--------------+--------------------+

| 152247404135 | 2018-03-31 13:29:26 | 中心                 | 直營               |               | 1827              |          | 1686        |       94 |                  | NULL         | 676  |               | 13312345678           |       |         0.01 |         1.00 |            1 |           99 | 2018-03-31 15:58:02 |      0.01 |         0.01 |               0.00 |

| 152248060 | 2018-03-31 16:00:42 |                  | 直營               |                | 1527              |          | 1386        |       94 |                  | NULL         | 1776  |               | 17310376676           |       |         0.01 |         0.96 |            2 |           99 | 2018-03-31 16:10:55 |      0.01 |         0.01 |               0.00 |

| 1559 | 2018-04-20 22:08:52 |                  |                |              | 145              |          | 151345        |       98 |                  | NULL         | 145  |             | 15072371345           |       |      2420.00 |       242.00 |            1 |           99 | 2018-04-20 22:28:41 |   2420.00 |      2420.00 |               0.00 |

+-----------------------------------------------------------------------------------------------------------------------------+

2.2.5、建立外連接(LEFT JOIN RIGHT JOIN)查詢語句視圖

         mysql> create view view_test5 as SELECT StuID,teachers.Name  FROM students LEFT JOIN teachers ON students.TeacherID=teachers.TID;

Query OK, 0 rows affected (0.02 sec)

 

mysql>

2.2.6、建立聯合查詢(UNION UNION ALL)語句的視圖

         mysql> create view view_test6 as select Name from students union all select Name from teachers;;

Query OK, 0 rows affected (0.03 sec)

 

mysql>

         mysql> select * from view_test6;

+----------------+

| Name           |

+----------------+

| Shi Zhongyu    |

| Shi Potian     |

| Xie Yanke      |

| Ding Dian      |

| Yu Yutong      |

| Shi Qing       |

| Xi Ren         |

| Lin Daiyu      |

| Ren Yingying   |

| Yue Lingshan   |

| Yuan Chengzhi  |

| Wen Qingqing   |

| Tian Boguang   |

| Lu Wushuang    |

| Duan Yu        |

| Xu Zhu         |

| Lin Chong      |

| Hua Rong       |

| Xue Baochai    |

| Diao Chan      |

| Huang Yueying  |

| Xiao Qiao      |

| Ma Chao        |

| Xu Xian        |

| Sun Dasheng    |

| Shi Jin        |

| Liu Bei        |

| Lu Zhi         |

| Zhu Ge Liang   |

| Huang Yue Ying |

| Zhao Yun       |

| Pang Tong      |

+----------------+

32 rows in set (0.03 sec)

 

mysql>

2.3、查看視圖

         建立完視圖後,常常會查詢視圖信息,在mysql中可使用 SHOW TABLES SHOW TABLE STATUSSHOW CTRATE VIEW等語句查詢數據,若是使用這些語句來查詢,首先要確保擁有SHOW VIEW的權限。

2.3.1、使用SHOW TABLES語句查看視圖名

         經過SHOW TABLES語句查看數據庫中的表和視圖的表

         mysql> show tables;

+---------------------+

| Tables_in_yuancheng |

+---------------------+

| classes             |

| coc                 |

| courses             |

| s                   |

| scores              |

| students            |

| teachers            |

| toc                 |

| view_test1          |

| view_test2          |

| view_test3          |

| view_test4          |

| view_test5          |

| view_test6          |

+---------------------+

14 rows in set (0.02 sec)

 

mysql>

2.3.2、使用SHOW TABLE STATUS語句查看視圖或表的詳細信息

         SHOW TABLE STATUS  [LIKE TABLE_NAME];

         mysql> show table status like  'wx_order' \G;

*************************** 1. row ***************************

           Name: wx_order

         Engine: InnoDB

        Version: 10

     Row_format: Compact

           Rows: 43

 Avg_row_length: 381

    Data_length: 16384

Max_data_length: 0

   Index_length: 0

      Data_free: 0

 Auto_increment: 52

    Create_time: 2018-05-16 17:02:05

    Update_time: NULL

     Check_time: NULL

      Collation: utf8mb4_general_ci

       Checksum: NULL

 Create_options:

        Comment: 微信支付訂單

1 row in set (0.00 sec)

 

ERROR:

No query specified

 

mysql>

注:其中各個字段的含義以下:

2.3.3、使用SHOW CREATE VIEW 語句查看視圖定義信息

         若是想查看關於建立視圖的定義信息,能夠經過SHOW CREATE VIEW 語句來實現,語法爲:

         SHOW CREATE VIEW view_name

         上述參數中view_name參數爲表示要查看定義信息的視圖名稱。

【例子1】查看view_test1的定義信息

         mysql> show create view view_test3;

+------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+

| View       | Create View                                                                                                                                                                                                                                                                                                                                                              | character_set_client | collation_connection |

+------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+

| view_test3 | CREATE ALGORITHM=UNDEFINED DEFINER=`zhang`@`%` SQL SECURITY DEFINER VIEW `view_test3` AS select `students`.`StuID` AS `StuID`,`students`.`Name` AS `Name`,`students`.`Age` AS `Age`,`students`.`Gender` AS `Gender`,`students`.`ClassID` AS `ClassID`,`students`.`TeacherID` AS `TeacherID` from `students` where (`students`.`Age` < 15) order by `students`.`Age` desc | utf8                 | utf8_general_ci      |

+------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+

1 row in set (0.02 sec)

 

mysql> show create view view_test3\G;

*************************** 1. row ***************************

                View: view_test3

         Create View: CREATE ALGORITHM=UNDEFINED DEFINER=`zhang`@`%` SQL SECURITY DEFINER VIEW `view_test3` AS select `students`.`StuID` AS `StuID`,`students`.`Name` AS `Name`,`students`.`Age` AS `Age`,`students`.`Gender` AS `Gender`,`students`.`ClassID` AS `ClassID`,`students`.`TeacherID` AS `TeacherID` from `students` where (`students`.`Age` < 15) order by `students`.`Age` desc

character_set_client: utf8

collation_connection: utf8_general_ci

1 row in set (0.02 sec)

 

ERROR:

No query specified

 

mysql>

2.3.4、使用DESC語句查看錶結構

         若是想查看關於視圖的表結構能夠經過DESC語句來查看。

mysql> desc view_test3;

+-----------+---------------------+------+-----+---------+-------+

| Field     | Type                | Null | Key | Default | Extra |

+-----------+---------------------+------+-----+---------+-------+

| StuID     | int(10) unsigned    | NO   |     | 0       |       |

| Name      | varchar(50)         | NO   |     | NULL    |       |

| Age       | tinyint(3) unsigned | NO   |     | NULL    |       |

| Gender    | enum('F','M')       | NO   |     | NULL    |       |

| ClassID   | tinyint(3) unsigned | YES  |     | NULL    |       |

| TeacherID | int(10) unsigned    | YES  |     | NULL    |       |

+-----------+---------------------+------+-----+---------+-------+

6 rows in set (0.02 sec)

 

mysql>

2.3.5、經過系統表查看視圖信息

         mysql數據庫安裝成功後,會自動建立系統數據庫information_schema,在該數據庫中存在一個包含視圖信息的表views,能夠經過查看錶views來查看相關信息。以下:

         mysql> use information_schema;

Reading table information for completion of table and column names

You can turn off this feature to get a quicker startup with -A

 

Database changed

mysql>

mysql> select * from views where table_name='view_test3'\G;

*************************** 1. row ***************************

       TABLE_CATALOG: def

        TABLE_SCHEMA: yuancheng

          TABLE_NAME: view_test3

     VIEW_DEFINITION: select `yuancheng`.`students`.`StuID` AS `StuID`,`yuancheng`.`students`.`Name` AS `Name`,`yuancheng`.`students`.`Age` AS `Age`,`yuancheng`.`students`.`Gender` AS `Gender`,`yuancheng`.`students`.`ClassID` AS `ClassID`,`yuancheng`.`students`.`TeacherID` AS `TeacherID` from `yuancheng`.`students` where (`yuancheng`.`students`.`Age` < 15) order by `yuancheng`.`students`.`Age` desc

        CHECK_OPTION: NONE

        IS_UPDATABLE: YES

             DEFINER: zhang@%

       SECURITY_TYPE: DEFINER

CHARACTER_SET_CLIENT: utf8

COLLATION_CONNECTION: utf8_general_ci

1 row in set (0.03 sec)

 

ERROR:

No query specified

 

mysql>

2.4、刪除視圖

         視圖的操做包括建立視圖、查看視圖、刪除視圖和修改視圖。在刪除視圖時,要確保擁有DROP VIEW 的權限。

2.4.1、刪除視圖

         在刪除視圖時課堂經過DROP VIEW語句刪除一個或者多個視圖,語法格式以下:

         DROP VIEW View_name [,View_name]….

         在上述語句中,view_name參數是要刪除視圖的名稱

【列子1】刪除view_test1視圖

         mysql> show tables;

+---------------------+

| Tables_in_yuancheng |

+---------------------+

| classes             |

| coc                 |

| courses             |

| s                   |

| scores              |

| students            |

| teachers            |

| toc                 |

| view_test1          |

| view_test2          |

| view_test3          |

| view_test4          |

| view_test5          |

| view_test6          |

+---------------------+

14 rows in set (0.02 sec)

 

mysql> drop view view_test1;

Query OK, 0 rows affected (0.02 sec)

 

mysql> show tables;

+---------------------+

| Tables_in_yuancheng |

+---------------------+

| classes             |

| coc                 |

| courses             |

| s                   |

| scores              |

| students            |

| teachers            |

| toc                 |

| view_test2          |

| view_test3          |

| view_test4          |

| view_test5          |

| view_test6          |

+---------------------+

13 rows in set (0.02 sec)

 

mysql>

【例子2】刪除多個視圖

         mysql> drop view s,view_test4,view_test3;

Query OK, 0 rows affected (0.02 sec)

 

mysql>

         mysql> show tables;

+---------------------+

| Tables_in_yuancheng |

+---------------------+

| classes             |

| coc                 |

| courses             |

| scores              |

| students            |

| teachers            |

| toc                 |

| view_test2          |

| view_test5          |

| view_test6          |

+---------------------+

10 rows in set (0.02 sec)

 

mysql>

2.5、修改視圖

         對於已經已經建立好的視圖,在使用過一段時間以後,若是須要進行一些表結構上的修改,即視圖的修改,除了可使用ALTER語句實現修改視圖以外還能夠經過CREATE OR REPLACE VIEW語句來修改視圖。

         對於已經建立好的表,尤爲是已經有大量數據的表。經過先刪除,而後按照新的表定義重建表的方式來修改表時,須要作許多額外的工做,好比數據的從新加載等,對於視圖來講,自己就是一張虛表,並無實質性存儲數據,全部能夠經過該方式來修改視圖。

2.5.1CREATE OR REPLACE VIEW 語句修改視圖

l  建立視圖view_1

mysql> create view view_1 as select students.StuID,students.Name,students.Age from students,teachers where students.TeacherID=teachers.TID and students.Age<20;

Query OK, 0 rows affected (0.01 sec)

mysql>

l  查看視圖內容

mysql> select * from view_1;

+-------+--------------+-----+

| StuID | Name         | Age |

+-------+--------------+-----+

|    12 | Wen Qingqing |  19 |

|     7 | Xi Ren       |  19 |

|     8 | Lin Daiyu    |  17 |

|    10 | Yue Lingshan |  19 |

|    14 | Lu Wushuang  |  17 |

|    15 | Duan Yu      |  19 |

|    19 | Xue Baochai  |  18 |

|    20 | Diao Chan    |  19 |

+-------+--------------+-----+

8 rows in set (0.01 sec)

 

mysql>

l  修改視圖

mysql> create  or replace  view view_1 as select students.StuID,students.Name,students.Age,students.ClassID,teachers.name as Tname  from students,teachers where students.TeacherID=teachers.TID and students.Age<<20;

Query OK, 0 rows affected (0.02 sec)

mysql>

l  查看修改後的視圖內容

mysql> select * from view_1;                                                                                                                                                                                    

+-------+--------------+-----+---------+-----------+

| StuID | Name         | Age | ClassID | Tname     |

+-------+--------------+-----+---------+-----------+

|    12 | Wen Qingqing |  19 |       1 | Lu Zhi    |

|     7 | Xi Ren       |  19 |       3 | Pang Tong |

|     8 | Lin Daiyu    |  17 |       7 | Pang Tong |

|    10 | Yue Lingshan |  19 |       3 | Pang Tong |

|    14 | Lu Wushuang  |  17 |       3 | Pang Tong |

|    15 | Duan Yu      |  19 |       4 | Pang Tong |

|    19 | Xue Baochai  |  18 |       6 | Pang Tong |

|    20 | Diao Chan    |  19 |       7 | Pang Tong |

+-------+--------------+-----+---------+-----------+

8 rows in set (0.01 sec)

 

mysql>

2.5.2ALTER語句修改視圖

使用ALTER語句來修改視圖,其語法格式以下:

                  ALTER VIEW View_Name  AS 查詢語句

【例子1】經過2.5.1上面的視圖添加查詢字段名稱

                  mysql> alter view view_1 as select students.StuID,students.Name,students.Age,students.ClassID,teachers.name as Tname,teachers.Age as TAge,teachers.Gender as TGender  from students,teachers where students.TeachherID=teachers.TID and students.Age<20;

Query OK, 0 rows affected (0.02 sec)

 

mysql> select * from view_1;                                                                                                                                                                                    

+-------+--------------+-----+---------+-----------+------+---------+

| StuID | Name         | Age | ClassID | Tname     | TAge | TGender |

+-------+--------------+-----+---------+-----------+------+---------+

|    12 | Wen Qingqing |  19 |       1 | Lu Zhi    |   30 | F       |

|     7 | Xi Ren       |  19 |       3 | Pang Tong |   44 | F       |

|     8 | Lin Daiyu    |  17 |       7 | Pang Tong |   44 | F       |

|    10 | Yue Lingshan |  19 |       3 | Pang Tong |   44 | F       |

|    14 | Lu Wushuang  |  17 |       3 | Pang Tong |   44 | F       |

|    15 | Duan Yu      |  19 |       4 | Pang Tong |   44 | F       |

|    19 | Xue Baochai  |  18 |       6 | Pang Tong |   44 | F       |

|    20 | Diao Chan    |  19 |       7 | Pang Tong |   44 | F       |

+-------+--------------+-----+---------+-----------+------+---------+

8 rows in set (0.01 sec)

 

mysql>

2.6、利用視圖操做基本表

         mysql中能夠經過視圖檢索(查詢)基本表數據,除此以外還能夠經過視圖修改基本表中的數據。

2.6.1、查詢數據

         經過視圖查詢數據,與經過表查詢數據徹底相同,經過視圖查詢比表更安全,簡單和實用。只需在查詢時把表名換成視圖名稱便可

         【例子1】查詢視圖信息

                  mysql> select * from view_1;                                                                  

+-------+--------------+-----+---------+-----------+------+---------+

| StuID | Name         | Age | ClassID | Tname     | TAge | TGender |

+-------+--------------+-----+---------+-----------+------+---------+

|    12 | Wen Qingqing |  19 |       1 | Lu Zhi    |   30 | F       |

|     7 | Xi Ren       |  19 |       3 | Pang Tong |   44 | F       |

|     8 | Lin Daiyu    |  17 |       7 | Pang Tong |   44 | F       |

|    10 | Yue Lingshan |  19 |       3 | Pang Tong |   44 | F       |

|    14 | Lu Wushuang  |  17 |       3 | Pang Tong |   44 | F       |

|    15 | Duan Yu      |  19 |       4 | Pang Tong |   44 | F       |

|    19 | Xue Baochai  |  18 |       6 | Pang Tong |   44 | F       |

|    20 | Diao Chan    |  19 |       7 | Pang Tong |   44 | F       |

+-------+--------------+-----+---------+-----------+------+---------+

8 rows in set (0.01 sec)

 

mysql>

2.6.2、利用視圖操做基本表數據

         經過建立的視圖不只能夠對視圖進行查詢數據,並且還能夠對視圖進行更新(增長、刪除和更新)數據操做。因爲視圖是虛表,因此對視圖數據進行的更新操做,其實是對其基本表數據進行更新操做。在具體更新視圖數據時,須要注意如下兩點:

l  對視圖數據進行添加、刪除和更新操做直接影響基本表。

l  視圖來自多個基本表時,不容許添加和刪除數據。

2.6.2.1、添加數據

         mysql> create view view_1 as select * from students where Age < 20;

Query OK, 0 rows affected (0.02 sec)

 

mysql> select * from view_1;

+-------+--------------+-----+--------+---------+-----------+

| StuID | Name         | Age | Gender | ClassID | TeacherID |

+-------+--------------+-----+--------+---------+-----------+

|     7 | Xi Ren       |  19 | F      |       3 |         6 |

|     8 | Lin Daiyu    |  17 | F      |       7 |         6 |

|    10 | Yue Lingshan |  19 | F      |       3 |         6 |

|    12 | Wen Qingqing |  19 | F      |       1 |         2 |

|    14 | Lu Wushuang  |  17 | F      |       3 |         6 |

|    15 | Duan Yu      |  19 | M      |       4 |         6 |

|    19 | Xue Baochai  |  18 | F      |       6 |         6 |

|    20 | Diao Chan    |  19 | F      |       7 |         6 |

+-------+--------------+-----+--------+---------+-----------+

8 rows in set (0.02 sec)

 

mysql>

         mysql> insert into view_1 (StuID,Name,Age,Gender,ClassID,TeacherID) values(27,'Zhang Wang',10,''F',3,2);

Query OK, 1 row affected (0.01 sec)

 

mysql> select * from view_1;

+-------+--------------+-----+--------+---------+-----------+

| StuID | Name         | Age | Gender | ClassID | TeacherID |

+-------+--------------+-----+--------+---------+-----------+

|     7 | Xi Ren       |  19 | F      |       3 |         6 |

|     8 | Lin Daiyu    |  17 | F      |       7 |         6 |

|    10 | Yue Lingshan |  19 | F      |       3 |         6 |

|    12 | Wen Qingqing |  19 | F      |       1 |         2 |

|    14 | Lu Wushuang  |  17 | F      |       3 |         6 |

|    15 | Duan Yu      |  19 | M      |       4 |         6 |

|    19 | Xue Baochai  |  18 | F      |       6 |         6 |

|    20 | Diao Chan    |  19 | F      |       7 |         6 |

|    27 | Zhang Wang   |  10 | F      |       3 |         2 |

+-------+--------------+-----+--------+---------+-----------+

9 rows in set (0.02 sec)

 

mysql>

注意:VALUSE後的數據常量與視圖中的列要一一對應

2.6.2.2、刪除數據操做

         mysql> select * from view_1;

+-------+--------------+-----+--------+---------+-----------+

| StuID | Name         | Age | Gender | ClassID | TeacherID |

+-------+--------------+-----+--------+---------+-----------+

|     7 | Xi Ren       |  19 | F      |       3 |         6 |

|     8 | Lin Daiyu    |  17 | F      |       7 |         6 |

|    10 | Yue Lingshan |  19 | F      |       3 |         6 |

|    12 | Wen Qingqing |  19 | F      |       1 |         2 |

|    14 | Lu Wushuang  |  17 | F      |       3 |         6 |

|    15 | Duan Yu      |  19 | M      |       4 |         6 |

|    19 | Xue Baochai  |  18 | F      |       6 |         6 |

+-------+--------------+-----+--------+---------+-----------+

7 rows in set (0.01 sec)

 

mysql> delete from view_1 where StuID=19 or StuID=12;

Query OK, 2 rows affected (0.02 sec)

 

mysql> select * from view_1;

+-------+--------------+-----+--------+---------+-----------+

| StuID | Name         | Age | Gender | ClassID | TeacherID |

+-------+--------------+-----+--------+---------+-----------+

|     7 | Xi Ren       |  19 | F      |       3 |         6 |

|     8 | Lin Daiyu    |  17 | F      |       7 |         6 |

|    10 | Yue Lingshan |  19 | F      |       3 |         6 |

|    14 | Lu Wushuang  |  17 | F      |       3 |         6 |

|    15 | Duan Yu      |  19 | M      |       4 |         6 |

+-------+--------------+-----+--------+---------+-----------+

5 rows in set (0.01 sec)

 

mysql>

2.6.2.3、更新數據

         mysql> update view_1 set Age='15' where Name='Duan Yu' and Age=19;

Query OK, 1 row affected (0.01 sec)

Rows matched: 1  Changed: 1  Warnings: 0

 

mysql> select * from view_1;

+-------+--------------+-----+--------+---------+-----------+

| StuID | Name         | Age | Gender | ClassID | TeacherID |

+-------+--------------+-----+--------+---------+-----------+

|     7 | Xi Ren       |  19 | F      |       3 |         6 |

|     8 | Lin Daiyu    |  17 | F      |       7 |         6 |

|    10 | Yue Lingshan |  19 | F      |       3 |         6 |

|    14 | Lu Wushuang  |  17 | F      |       3 |         6 |

|    15 | Duan Yu      |  15 | M      |       4 |         6 |

+-------+--------------+-----+--------+---------+-----------+

5 rows in set (0.01 sec)

 

mysql>

相關文章
相關標籤/搜索