MySQL: Create Table Using Another Table

Profile picture for user arilio666

Today in this article we are gonna talk about creating tables from another existing table in our database using the AS clause which will recreate the existing table when used in this manner and create a backup or some can say clone of the existing table.

Syntax

create table TableName 
as
select * from existing_table;

So enough talk lets us see some of the ways we can create based on the existing one.

Creating Table Based On Existing Table

Same as the syntax the query follows the exact thing of creating the exact table structure of the existing table we provide in the select query statement.

CREATE TABLE Crypto 
as
SELECT * from crypto_price_list;

So we simply created a table based on the existing one called crypto_price_list and made the exact copy of it.

Creating Table Based On Two or More Existing Table Out Of Joins

CREATE TABLE CUSTORDER
as
SELECT order_id, o.customer_id, first_name, last_name from orders o JOIN customers c ON o.customer_id = c.customer_id;

  • So here we created a table based on the joins of two different tables orders and customers and created a table based on that joins output.
  • Here, in the same way, we can also customize our column name using the 'as' clause and create a table based on that output to get our very own customized column name-based table.
Tags