How to create a table from view in MySQL?


To create a table from the view below is the syntax −

create table yourTableName select *from yourViewName;

Let us first create a table −

mysql> create table DemoTable830(Name varchar(100));
Query OK, 0 rows affected (0.91 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable830 values('Chris');
Query OK, 1 row affected (0.41 sec)
mysql> insert into DemoTable830 values('Robert');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable830 values('David');
Query OK, 1 row affected (0.09 sec)
mysql> insert into DemoTable830 values('Mike');
Query OK, 1 row affected (0.16 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable830;

This will produce the following output −

+--------+
| Name   |
+--------+
| Chris  |
| Robert |
| David  |
| Mike   |
+--------+
4 rows in set (0.00 sec)

Following is the query to create a view −

mysql> create view table_view AS select *from DemoTable830;
Query OK, 0 rows affected (0.39 sec)

Now, we will create a table from view. Following is the query −

mysql> create table DemoTable831 select *from table_view;
Query OK, 4 rows affected (1.64 sec)
Records: 4 Duplicates: 0 Warnings: 0

Let us check the records of the table which has been created from the view −

mysql> select *from DemoTable831;

This will produce the following output −

+--------+
| Name   |
+--------+
| Chris  |
| Robert |
| David  |
| Mike   |
+--------+
4 rows in set (0.00 sec)

Updated on: 03-Sep-2019

401 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements