公司主營業(yè)務(wù):網(wǎng)站建設(shè)、成都網(wǎng)站建設(shè)、移動網(wǎng)站開發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實(shí)現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競爭能力。創(chuàng)新互聯(lián)是一支青春激揚(yáng)、勤奮敬業(yè)、活力青春激揚(yáng)、勤奮敬業(yè)、活力澎湃、和諧高效的團(tuán)隊(duì)。公司秉承以“開放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對我們的高要求,感謝他們從不同領(lǐng)域給我們帶來的挑戰(zhàn),讓我們激情的團(tuán)隊(duì)有機(jī)會用頭腦與智慧不斷的給客戶帶來驚喜。創(chuàng)新互聯(lián)推出淶源免費(fèi)做網(wǎng)站回饋大家。
本文主要給大家介紹操作MySQL的DDL語句視圖方法,文章內(nèi)容都是筆者用心摘選和編輯的,具有一定的針對性,對大家的參考意義還是比較大的,下面跟筆者一起了解下操作MySQL的DDL語句視圖方法吧。
1. 背景
* 視圖是數(shù)據(jù)庫中的虛擬表。包含一系列帶有名稱的行和列數(shù)據(jù)。視圖是從一個或者多個表中導(dǎo)出的,視圖的行為與表非常相似,用戶可以使用SELECT語句查詢數(shù)據(jù),以及使用INSERT、UPDATE和DELETE修改記錄,視圖使用戶操作更加方便而且保障數(shù)據(jù)庫系統(tǒng)的安全。
* 視圖一旦定義便存儲在數(shù)據(jù)庫中,與其對應(yīng)的數(shù)據(jù)并沒有像表那樣在數(shù)據(jù)庫中再存儲一份,通過視圖看到的數(shù)據(jù)只是存放在基本表中的數(shù)據(jù)。對視圖的操作與對表的操作一樣,可以對其進(jìn)行查詢、修改和刪除。當(dāng)對通過視圖看到的數(shù)據(jù)進(jìn)行修改時,相應(yīng)的基本表的數(shù)據(jù)也要發(fā)生變化,同時若基本表的數(shù)據(jù)放生變化,則這種變化也會自動地反應(yīng)到視圖中。
2. 視圖作用
* 使查詢變得很清晰,視圖中存放的數(shù)據(jù)就是我們所要的數(shù)據(jù),并且可以簡化用戶操作。
* 讓數(shù)據(jù)更安全,視圖中的數(shù)據(jù),不存在視圖中,還是在基本表里面,通過視圖這層關(guān)系,我們可以有效的保護(hù)我們的重要數(shù)據(jù)
* 提高表的邏輯獨(dú)立性,視圖可以屏蔽原有表結(jié)構(gòu)變化帶來的影響
3. 視圖類型
* MERGE: 將引用視圖的語句的文本與視圖定義合并起來,使得視圖定義的某一部分取代語句的對應(yīng)部分。
* TEMPTABLE: 將視圖的結(jié)果將被置于臨時表中,然后使用它執(zhí)行語句。
* UNDEFINED:默認(rèn)所使用的算法。MySQL更傾向于MERGE而不是TEMPTABLE,這是因?yàn)镸ERGE通常更有效。
4. 視圖類型例子
* 創(chuàng)建基表users
mysql> CREATE TABLE users( -> id BIGINT PRIMARY KEY NOT NULL AUTO_INCREMENT, -> name VARCHAR(64) NOT NULL, -> sex ENUM('M', 'F') NOT NULL, -> age INT NOT NULL -> )ENGINE=INNODB CHARSET=utf8mb4; Query OK, 0 rows affected (0.15 sec)
* 對基表 users 插入數(shù)據(jù)
mysql> INSERT INTO users VALUES(NULL, 'tom', 'M', 23), (NULL, 'jak', 'F', 32), (NULL, 'jus', 'M', 35); Query OK, 3 rows affected (0.04 sec) Records: 3 Duplicates: 0 Warnings: 0
* 查看基表數(shù)據(jù)
mysql> SELECT * FROM users; +----+------+-----+-----+ | id | name | sex | age | +----+------+-----+-----+ | 1 | tom | M | 23 | | 2 | jak | F | 32 | | 3 | jus | M | 35 | +----+------+-----+-----+ 3 rows in set (0.00 sec)
* 創(chuàng)建用戶id和name vusers1視圖,類型為UNDEFINED
mysql> CREATE ALGORITHM=UNDEFINED VIEW vusers1 AS SELECT id, name FROM users; Query OK, 0 rows affected (0.01 sec)
* 分析查看 vusers1 視圖執(zhí)行計劃
[ 未使用臨時表 ]
mysql> EXPLAIN SELECT * FROM vusers1; +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+ | 1 | SIMPLE | users | NULL | ALL | NULL | NULL | NULL | NULL | 3 | 100.00 | NULL | +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+ 1 row in set, 1 warning (0.00 sec)
* 創(chuàng)建用戶id和name vusers2視圖,類型為merge
mysql> CREATE ALGORITHM=MERGE VIEW vuser2 AS SELECT id, name FROM users; Query OK, 0 rows affected (0.03 sec)
* 分析查看 vusers2 視力執(zhí)行計劃
[ 未使用臨時表 ]
mysql> EXPLAIN SELECT * FROM vuser2; +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+ | 1 | SIMPLE | users | NULL | ALL | NULL | NULL | NULL | NULL | 3 | 100.00 | NULL | +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+ 1 row in set, 1 warning (0.01 sec)
* 創(chuàng)建用戶id和name vusers3視圖,類型為TEMPTABLE
mysql> CREATE ALGORITHM=TEMPTABLE VIEW vuser3 AS SELECT id, name FROM users; Query OK, 0 rows affected (0.19 sec)
* 分析查看 vusers3 視力執(zhí)行計劃
[ 使用了臨時表 ]
mysql> EXPLAIN SELECT * FROM vuser3; +----+-------------+------------+------------+------+---------------+------+---------+------+------+----------+-------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+------------+------------+------+---------------+------+---------+------+------+----------+-------+ | 1 | PRIMARY || NULL | ALL | NULL | NULL | NULL | NULL | 3 | 100.00 | NULL | | 2 | DERIVED | users | NULL | ALL | NULL | NULL | NULL | NULL | 3 | 100.00 | NULL | +----+-------------+------------+------------+------+---------------+------+---------+------+------+----------+-------+ 2 rows in set, 1 warning (0.00 sec)
5. 查看視圖信息
* desc 查看視圖基本信息
mysql> desc vusers1; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | id | bigint(20) | NO | | 0 | | | name | varchar(64) | NO | | NULL | | +-------+-------------+------+-----+---------+-------+ 2 rows in set (0.00 sec)
* show table status 查看視圖基本信息
mysql> show table status like 'vusers1'; +--------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+-------------+-------------+------------+-----------+----------+----------------+---------+ | Name | Engine | Version | Row_format | Rows | Avg_row_length | Data_length | Max_data_length | Index_length | Data_free | Auto_increment | Create_time | Update_time | Check_time | Collation | Checksum | Create_options | Comment | +--------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+-------------+-------------+------------+-----------+----------+----------------+---------+ | vuser1 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | VIEW | +--------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+-------------+-------------+------------+-----------+----------+----------------+---------+ 1 row in set (0.00 sec)
* show create view 查看視圖信息
mysql> show create view vuser1; +--------+---------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+ | View | Create View | character_set_client | collation_connection | +--------+---------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+ | vuser1 | CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `vuser1` AS select `users`.`id` AS `id`,`users`.`name` AS `name` from `users` | utf8 | utf8_general_ci | +--------+---------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+ 1 row in set (0.00 sec)
6. 總結(jié)
以需求驅(qū)動技術(shù),技術(shù)本身沒有優(yōu)略之分,只有業(yè)務(wù)之分。
看完以上關(guān)于操作MySQL的DDL語句視圖方法,很多讀者朋友肯定多少有一定的了解,如需獲取更多的行業(yè)知識信息 ,可以持續(xù)關(guān)注我們的行業(yè)資訊欄目的。