如何瞭解 MySQL 檢視的構成?


以下為語法 −

show create view yourViewName;

讓我們首先建立一個表 −

mysql> create table DemoTable
-> (
-> StudentName varchar(20)
-> );
Query OK, 0 rows affected (0.56 sec)

使用插入命令在表中插入一些記錄 −

mysql> insert into DemoTable values('Chris');
Query OK, 1 row affected (0.14 sec)

mysql> insert into DemoTable values('Robert');
Query OK, 1 row affected (0.18 sec)

mysql> insert into DemoTable values('David');
Query OK, 1 row affected (0.13 sec)

使用 select 語句顯示錶中的所有記錄 −

mysql> select *from DemoTable;

輸出

這將生成以下輸出 −

+-------------+
| StudentName |
+-------------+
| Chris       |
| Robert      |
| David       |
+-------------+
3 rows in set (0.00 sec)

以下是為表建立檢視的查詢 −

mysql> CREATE VIEW view_DemoTable AS SELECT StudentName from DemoTable;
Query OK, 0 rows affected (0.12 sec)

以下是檢視 MySQL 檢視的構成的查詢 −

mysql> show create view view_DemoTable;

輸出

這將生成以下輸出 −

+-------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+
| View              | Create View | character_set_client | collation_connection |
+-------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+
| view_DemoTable     | CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`%` SQL
SECURITY DEFINER VIEW `view_DemoTable` AS select `DemoTable`.`StudentName` AS
`StudentName` from `DemoTable` | utf8 | utf8_general_ci |
+-------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+
1 row in set (0.00 sec)

讓我們檢查檢視記錄 −

mysql> select *from view_DemoTable;

輸出

這將生成以下輸出 −

+-------------+
| StudentName |
+-------------+
| Chris       |
| Robert      |
| David       |
+-------------+
3 rows in set (0.05 sec)

更新於: 2020 年 6 月 30 日

119 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始
廣告