MySQL檢視簡介與操作的介紹(附程式碼)

不言
發布: 2019-04-15 11:26:47
轉載
3115 人瀏覽過

這篇文章帶給大家的內容是關於MySQL視圖簡介與操作的介紹(附程式碼),有一定的參考價值,有需要的朋友可以參考一下,希望對你有幫助。

1、準備工作

在MySQL資料庫中建立兩個表格balance(餘額表)和customer(客戶表)並插入資料。

create table customer(
    id int(10) primary key,
    name char(20) not null,
    role char(20) not null,
    phone char(20) not null,
    sex char(10) not null,
    address char(50) not null
)ENGINE=InnoDB DEFAULT CHARSET=utf8;

#外键为customerId
create table balance(
    id int(10) primary key,
    customerId int(10) not null,
    balance DECIMAL(10,2),
    foreign key(customerId) references customer(id) 

)ENGINE=InnoDB DEFAULT CHARSET=utf8;
登入後複製

向客戶表和餘額表中各插入3條資料。

insert into customer values(0001,"xiaoming",'vip1','12566666','male','江宁区888号');
insert into customer values(0002,"xiaohong",'vip10','15209336760','male','建邺区888号');
insert into customer values(0003,"xiaocui",'vip11','15309336760','female','新街口888号');

insert into balance values(1,0001,900.55);
insert into balance values(2,0002,900.55);
insert into balance values(3,0003,10000);
登入後複製

2、視圖簡介

視圖可以簡單理解成虛擬表,它和資料庫中真實存在資料表不同,視圖中的資料是基於真實表查詢得到的。視圖和真實表一樣具備相似的結構。真實表的更新,查詢,刪除等操作,視圖也支援。那為什麼需要視圖呢?

a、提升真實表的安全性:視圖是虛擬的,可以只授予使用者視圖的權限而不授予真實表的權限,起到保護真實表的作用。
b、客製化展示資料:基於相同的實際表,可以透過不同的視圖來向不同需求的使用者客製化展示資料。
c、簡化資料操作:適用於查詢語句比較複雜使用頻率較高的場景,可以透過視圖來實現。
......

要說明一點的是:視圖相關的操作需要使用者俱備對應的權限。以下操作使用root用戶,預設用戶具備操作權限。

建立視圖語法

create view <视图名称>  as <select语句>;
登入後複製

修改視圖語法

修改視圖名稱可以先刪除,再用相同的語句建立。

#更新视图结构
alter view <视图名称>  as <select语句>;
#更新视图数据相当于更新实际表,不适用基于多表创建的视图
update ....
登入後複製

注意:部分視圖的資料是無法更新,也就是無法使用update,insert等語句更新,例如:

a、select語句包含多個表
b、視圖中包含having子句
c、試圖中包含distinct關鍵字
......

刪除視圖語法

drop view <视图名称>
登入後複製

3、視圖的動作

#基於單一表格建立檢視

mysql> create view  bal_view 
    -> as
    -> select * from  balance;
Query OK, 0 rows affected (0.22 sec)
登入後複製

建立完成後,查看bal_view的結構和記錄。可以發現透過視圖查詢到資料和透過真實表查詢得到的結果完全一樣。

#查询bal_view的结构
mysql> desc bal_view;
+------------+---------------+------+-----+---------+-------+
| Field      | Type          | Null | Key | Default | Extra |
+------------+---------------+------+-----+---------+-------+
| id         | int(10)       | NO   |     | NULL    |       |
| customerId | int(10)       | NO   |     | NULL    |       |
| balance    | decimal(10,2) | YES  |     | NULL    |       |
+------------+---------------+------+-----+---------+-------+
3 rows in set (0.07 sec)
#查询bal_view中的记录
mysql> select  * from bal_view;
+----+------------+----------+
| id | customerId | balance  |
+----+------------+----------+
|  1 |          1 |   900.55 |
|  2 |          2 |   900.55 |
|  3 |          3 | 10000.00 |
+----+------------+----------+
3 rows in set (0.01 sec)
登入後複製

透過建立檢視的語句不難下結論:當真實表中的資料改變時,檢視中的資料也會隨之改變。那麼當視圖中的資料改變時,真實表中的資料會改變嗎?來實驗一下,修改id=1的客戶balance為2000。

mysql> update bal_view set balance=2000 where id=1;
Query OK, 1 row affected (0.05 sec)
Rows matched: 1  Changed: 1  Warnings: 0
登入後複製

來看看真實表balance中的資料。

mysql> select  * from bal_view where id=1;
+----+------------+---------+
| id | customerId | balance |
+----+------------+---------+
|  1 |          1 | 2000.00 |
+----+------------+---------+
1 row in set (0.03 sec)
登入後複製

結論:視圖表中的資料變更時,真實表中的資料也會隨之改變。

基於多表建立視圖

建立視圖cus_bal,共兩個欄位客戶名稱和餘額。

mysql> create view cus_bal
    -> (cname,bal)
    -> as
    -> select customer.name,balance.balance from customer ,balance
    -> where customer.id=balance.customerId;
Query OK, 0 rows affected (0.05 sec)
#查看cus_bal中的数据
mysql> select *  from  cus_bal;
+----------+----------+
| cname    | bal      |
+----------+----------+
| xiaoming |  2000.00 |
| xiaohong |   900.55 |
| xiaocui  | 10000.00 |
+----------+----------+
3 rows in set (0.28 sec)
登入後複製

修改視圖

將cus_bal視圖中的cname改成cusname。

mysql> alter view  cus_bal
    -> (cusname,bal)
    -> as
    -> select customer.name,balance.balance from customer ,balance
    -> where customer.id=balance.customerId;
Query OK, 0 rows affected (0.06 sec)
#查看修改后视图结构。
mysql> desc  cus_bal;
+---------+---------------+------+-----+---------+-------+
| Field   | Type          | Null | Key | Default | Extra |
+---------+---------------+------+-----+---------+-------+
| cusname | char(20)      | NO   |     | NULL    |       |
| bal     | decimal(10,2) | YES  |     | NULL    |       |
+---------+---------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
登入後複製

修改基於多表建立的視圖

mysql> insert into cus_bal(cusname,bal) values ("ee",11);
ERROR 1393 (HY000): Can not modify more than one base table through a join view &#39;rms.cus_bal&#39;
登入後複製

刪除視圖

#刪除視圖cus_bal

drop view cus_bal;
mysql> drop view cus_bal;
Query OK, 0 rows affected (0.00 sec)
登入後複製

【相關推薦:MySQL教學

以上是MySQL檢視簡介與操作的介紹(附程式碼)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:cnblogs.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!