MySQL視圖簡(jiǎn)介及基本操作教程
視圖是數(shù)據(jù)庫(kù)系統(tǒng)中一種非常有用的數(shù)據(jù)庫(kù)對(duì)象。MySQL 5.0 之后的版本添加了對(duì)視圖的支持。
認(rèn)識(shí)視圖
視圖是一個(gè)虛擬表,其內(nèi)容由查詢(xún)定義。同真實(shí)表一樣,視圖包含一系列帶有名稱(chēng)的列和行數(shù)據(jù),但視圖并不是數(shù)據(jù)庫(kù)真實(shí)存儲(chǔ)的數(shù)據(jù)表。
視圖是從一個(gè)、多個(gè)表或者視圖中導(dǎo)出的表,包含一系列帶有名稱(chēng)的數(shù)據(jù)列和若干條數(shù)據(jù)行。
視圖并不同于數(shù)據(jù)表,它們的區(qū)別在于以下幾點(diǎn):
- 視圖不是數(shù)據(jù)庫(kù)中真實(shí)的表,而是一張?zhí)摂M表,其結(jié)構(gòu)和數(shù)據(jù)是建立在對(duì)數(shù)據(jù)中真實(shí)表的查詢(xún)基礎(chǔ)上的。
- 存儲(chǔ)在數(shù)據(jù)庫(kù)中的查詢(xún)操作 SQL 語(yǔ)句定義了視圖的內(nèi)容,列數(shù)據(jù)和行數(shù)據(jù)來(lái)自于視圖查詢(xún)所引用的實(shí)際表,引用視圖時(shí)動(dòng)態(tài)生成這些數(shù)據(jù)。
- 視圖沒(méi)有實(shí)際的物理記錄,不是以數(shù)據(jù)集的形式存儲(chǔ)在數(shù)據(jù)庫(kù)中的,它所對(duì)應(yīng)的數(shù)據(jù)實(shí)際上是存儲(chǔ)在視圖所引用的真實(shí)表中的。
- 視圖是數(shù)據(jù)的窗口,而表是內(nèi)容。表是實(shí)際數(shù)據(jù)的存放單位,而視圖只是以不同的顯示方式展示數(shù)據(jù),其數(shù)據(jù)來(lái)源還是實(shí)際表。
- 視圖是查看數(shù)據(jù)表的一種方法,可以查詢(xún)數(shù)據(jù)表中某些字段構(gòu)成的數(shù)據(jù),只是一些 SQL 語(yǔ)句的集合。從安全的角度來(lái)看,視圖的數(shù)據(jù)安全性更高,使用視圖的用戶(hù)不接觸數(shù)據(jù)表,不知道表結(jié)構(gòu)。
- 視圖的建立和刪除只影響視圖本身,不影響對(duì)應(yīng)的基本表。
視圖與表在本質(zhì)上雖然不相同,但視圖經(jīng)過(guò)定義以后,結(jié)構(gòu)形式和表一樣,可以進(jìn)行查詢(xún)、修改、更新和刪除等操作。
1、準(zhǔn)備工作
在MySQL數(shù)據(jù)庫(kù)中創(chuàng)建兩張表balance(余額表)和customer(客戶(hù)表)并插入數(shù)據(jù)。
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;
向客戶(hù)表和余額表中各插入3條數(shù)據(jù)。
insert into customer values(0001,"xiaoming",'vip1','12566666','male','江寧區(qū)888號(hào)'); insert into customer values(0002,"xiaohong",'vip10','15209336760','male','建鄴區(qū)888號(hào)'); insert into customer values(0003,"xiaocui",'vip11','15309336760','female','新街口888號(hào)'); insert into balance values(1,0001,900.55); insert into balance values(2,0002,900.55); insert into balance values(3,0003,10000);
2、視圖簡(jiǎn)介
視圖可以簡(jiǎn)單理解成虛擬表,它和數(shù)據(jù)庫(kù)中真實(shí)存在數(shù)據(jù)表不同,視圖中的數(shù)據(jù)是基于真實(shí)表查詢(xún)得到的。視圖和真實(shí)表一樣具備相似的結(jié)構(gòu)。真實(shí)表的更新,查詢(xún),刪除等操作,視圖也支持。那么為什么需要視圖呢?
a、提升真實(shí)表的安全性:視圖是虛擬的,可以只授予用戶(hù)視圖的權(quán)限而不授予真實(shí)表的權(quán)限,起到保護(hù)真實(shí)表的作用。
b、定制化展示數(shù)據(jù):基于同樣的實(shí)際表,可以通過(guò)不同的視圖來(lái)向不同需求的用戶(hù)定制化展示數(shù)據(jù)。
c、簡(jiǎn)化數(shù)據(jù)操作:適用于查詢(xún)語(yǔ)句比較復(fù)雜使用頻率較高的場(chǎng)景,可以通過(guò)視圖來(lái)實(shí)現(xiàn)。
......
需要說(shuō)明一點(diǎn)的是:視圖相關(guān)的操作需要用戶(hù)具備相應(yīng)的權(quán)限。以下操作使用root用戶(hù),默認(rèn)用戶(hù)具備操作權(quán)限。
創(chuàng)建視圖語(yǔ)法
create view <視圖名稱(chēng)> as <select語(yǔ)句>;
修改視圖語(yǔ)法
修改視圖名稱(chēng)可以先刪除,再用相同的語(yǔ)句創(chuàng)建。
#更新視圖結(jié)構(gòu) alter view <視圖名稱(chēng)> as <select語(yǔ)句>; #更新視圖數(shù)據(jù)相當(dāng)于更新實(shí)際表,不適用基于多表創(chuàng)建的視圖 update ....
注意:部分視圖的數(shù)據(jù)是無(wú)法更新,也就是無(wú)法使用update,insert等語(yǔ)句更新,比如:
a、select語(yǔ)句包含多個(gè)表
b、視圖中包含having子句
c、試圖中包含distinct關(guān)鍵字
......
刪除視圖語(yǔ)法
drop view <視圖名稱(chēng)>
3、視圖的操作
基于單表創(chuàng)建視圖
mysql> create view bal_view -> as -> select * from balance; Query OK, 0 rows affected (0.22 sec)
創(chuàng)建完成后,查看bal_view的結(jié)構(gòu)和記錄??梢园l(fā)現(xiàn)通過(guò)視圖查詢(xún)到數(shù)據(jù)和通過(guò)真實(shí)表查詢(xún)得到的結(jié)果完全一樣。
#查詢(xún)bal_view的結(jié)構(gòu) 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) #查詢(xún)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)
通過(guò)創(chuàng)建視圖的語(yǔ)句不難得出結(jié)論:當(dāng)真實(shí)表中的數(shù)據(jù)發(fā)生改變時(shí),視圖中的數(shù)據(jù)也會(huì)隨之改變。那么當(dāng)視圖中的數(shù)據(jù)發(fā)生改變時(shí),真實(shí)表中的數(shù)據(jù)會(huì)變化嗎?來(lái)實(shí)驗(yàn)一下,修改id=1的客戶(hù)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
來(lái)看一下真實(shí)表balance中的數(shù)據(jù)。
mysql> select * from bal_view where id=1; +----+------------+---------+ | id | customerId | balance | +----+------------+---------+ | 1 | 1 | 2000.00 | +----+------------+---------+ 1 row in set (0.03 sec)
結(jié)論:視圖表中的數(shù)據(jù)發(fā)生變化時(shí),真實(shí)表中的數(shù)據(jù)也會(huì)隨之改變。
基于多表創(chuàng)建視圖
創(chuàng)建視圖cus_bal,共兩個(gè)字段客戶(hù)名稱(chēng)和余額。
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中的數(shù)據(jù) 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) #查看修改后視圖結(jié)構(gòu)。 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)
修改基于多表創(chuàng)建的視圖
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 'rms.cus_bal'
刪除視圖
刪除視圖cus_bal
drop view cus_bal; mysql> drop view cus_bal; Query OK, 0 rows affected (0.00 sec)
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)本站的支持。
版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。