如何在 MySQL 中輕鬆使用“從檢視建立表”語法?


您可以使用 create table select 語法從檢視建立表。語法如下:

CREATE TABLE yourTableName AS SELECT
yourColumnName1,yourColumnName2,yourColumnName3,........N from yourViewName;

要執行以上查詢,您需要先建立一個表,然後在該表上建立一個檢視。之後再執行查詢。

首先,您需要建立一個表。建立表的查詢如下:

mysql> create table StuedntInformation
   -> (
   -> Id int,
   -> Name varchar(100)
   -> );
Query OK, 0 rows affected (0.54 sec)

在上面,我們建立了一個表。之後,您需要建立一個檢視。建立檢視的查詢如下:

mysql> CREATE VIEW view_Student AS SELECT Id,Name from StuedntInformation;
Query OK, 0 rows affected (0.11 sec)

現在,我建立了一個名為“view_Student”的檢視。使用 show 命令檢查檢視。

查詢如下:

mysql> SHOW CREATE VIEW view_Student;

輸出

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

我們將使用上述檢視名稱“view_Student”來建立一個表。以下是使用檢視建立表的查詢:

mysql> CREATE TABLE CreatingTableUsingViewStudent AS
   -> select Id,Name from view_Student;

Query OK, 0 rows affected (0.50 sec)
Records: 0 Duplicates: 0 Warnings: 0

現在,您可以使用 show 命令檢查表的 DDL。查詢如下:

mysql> show create table CreatingTableUsingViewStudent;

輸出

+-------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table                         | Create Table                                                                                                                                                                    |
+-------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| CreatingTableUsingViewStudent | CREATE TABLE `creatingtableusingviewstudent` ( `Id` int(11) DEFAULT NULL, `Name` varchar(100) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+-------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

更新於: 2019-07-30

2K+ 瀏覽量

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告