Computer >> Máy Tính >  >> Lập trình >> MySQL

Làm thế nào để dễ dàng 'tạo bảng từ chế độ xem' trong MySQL?

Bạn có thể tạo một bảng từ dạng xem bằng cú pháp tạo bảng. Cú pháp như sau -

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

Để chạy truy vấn trên, trước tiên bạn cần tạo một bảng và sau đó, bạn cần tạo một dạng xem trên bảng đó. Sau đó, hãy chạy truy vấn.

Đầu tiên, bạn cần tạo một bảng. Truy vấn để tạo bảng như sau -

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

Ở trên, chúng ta đã tạo một bảng. Sau đó, bạn cần tạo một khung nhìn. Truy vấn để tạo một dạng xem như sau -

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

Bây giờ tôi đã tạo một khung nhìn với tên ‘view_Student’. Kiểm tra chế độ xem bằng lệnh show.

Truy vấn như sau -

mysql> SHOW CREATE VIEW view_Student;

Đầu ra

+--------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+
| 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)

Chúng tôi sẽ sử dụng tên chế độ xem ở trên ‘view_Student’ để tạo bảng. Sau đây là truy vấn để tạo bảng bằng chế độ xem -

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

Bây giờ bạn có thể kiểm tra DDL của một bảng bằng lệnh show. Truy vấn như sau -

mysql> show create table CreatingTableUsingViewStudent;

Đầu ra

+-------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| 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)