MySQL視圖介紹

來源:互聯網
上載者:User

標籤:mysql

 

資料準備

建立資料庫、匯入資料

 

[[email protected] ~]# mysql -uroot -p -e"create database viewdb;"

Enter password:

[[email protected] ~]# mysql -uroot -p viewdb< viewdb.sql

Enter password:

 

mysql> show databases;

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

| Database           |

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

| information_schema |

| db2                |

| liujing            |

| mysql              |

| performance_schema |

| sys                |

| viewdb             |

| worlddb            |

| xiaowei            |

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

9 rows in set (0.13 sec)

 

mysql> show tables;

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

| Tables_in_viewdb |

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

| user             |

| userinfo         |

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

2 rows in set (0.00 sec)

 

 

視圖概述

視圖是一個虛擬表,是從資料庫中一個或多個表中匯出來的表。視圖還可以從已經存在的視圖的基礎上定義。

 

視圖的作用

1、簡化工作

2、提高安全性

3、邏輯獨立

 

建立視圖的文法

 

CREATE [OR REPLACE] [ALGORITHM] ={undefined|MERGE|TEMPTABLE}]

VIEW view_name [(column_list)]

AS SELECT_statement

[WITH[CASCADED|LOCAL] CHECK OPTION]

ALGORITHM用於設定視圖的演算法

CASCADED|LOCAL為限定條件。

CASCADED:預設值,更新時必須滿足所有相關視圖和表的條件。

LOCAL:更新時僅滿足該視圖本身定義的條件即可。

 

 

建立單表視圖

mysql> create view view_user as selectname,age from user;

Query OK, 0 rows affected (0.01 sec)

 

查看

mysql> select name,age from user;

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

| name   | age  |

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

| TubeLiu |   10 |

| Kevin  |   20 |

| Mark   |   30 |

| July   |   40 |

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

4 rows in set (0.00 sec)

 

mysql> select * from view_user;

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

| name   | age  |

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

| TubeLiu |   10 |

| Kevin  |   20 |

| Mark   |   30 |

| July   |   40 |

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

4 rows in set (0.00 sec)

 

 

更新了視圖中的資料

mysql> update view_user set age = 20where name=‘Tubeliu‘;

Query OK, 1 row affected (0.10 sec)

Rows matched: 1  Changed: 1 Warnings: 0

 

 

 

查看

mysql> select * from view_user;

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

| name   | age  |

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

| TubeLiu |   20 |

| Kevin  |   20 |

| Mark   |   30 |

| July   |   40 |

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

4 rows in set (0.00 sec)

 

mysql> select name,age from user;

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

| name   | age  |

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

| TubeLiu |   20 |

| Kevin  |   20 |

| Mark   |   30 |

| July   |   40 |

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

4 rows in set (0.00 sec)

 

對視圖的操作也會影響真實表中的內容。

 

 

建立多表視圖

 

mysql> create viewview_userinfo(name1,phone1) as select user.name,userinfo.phone fromuser,userinfo where id=fid;

Query OK, 0 rows affected (0.00 sec)

 

 

查看

mysql> select * from view_userinfo;

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

| name1  | phone1 |

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

| TubeLiu | 12345 |

| Kevin  |  54321 |

| Mark   |  32145 |

| July   |  34521 |

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

4 rows in set (0.00 sec)

 

mysql> select user.name,userinfo.phonefrom user,userinfo where id=fid;

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

| name   | phone |

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

| TubeLiu | 12345 |

| Kevin  | 54321 |

| Mark   | 32145 |

| July   | 34521 |

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

4 rows in set (0.00 sec)

 

 

 

 


本文出自 “陽光的蝸牛” 部落格,謝絕轉載!

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.