您可以使用建立表格選擇語法從檢視建立表格。語法如下 -
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)
以上是如何在 MySQL 中輕鬆地「從視圖建立表格」語法?的詳細內容。更多資訊請關注PHP中文網其他相關文章!