Site icon TestingDocs.com

MySQL Replicate Table using Existing Table

Overview

In this tutorial, we will learn to replicate table using existing table in MySQL. A new database table can be created using values from an existing table. This helps in saving the time in creating a new table from scratch, when the data needed already exists.

Syntax

CREATE TABLE <new_table_name>
SELECT query

It is good practice to familiarize with the existing table before replicating it. We can use the SHOW CREATE TABLE statement, which is explicitly for reviewing the CREATE TABLE statement used for building the table.

 

For example, to create a new table R_Actor from an existing table Actor from the sakila database. The statement below creates a new table called R_Actor using the output from the SELECT query
and populates it with the data from the existing Actor table.

mysql> CREATE TABLE R_Actor
              -> SELECT * FROM
              -> Actor;

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

We can verify the table creation using the following statements:
The SHOW TABLES, DESCRIBE and SELECT statements confirm the creation of the new table and the table contents.

mysql>SHOW TABLES;

mysql>DESCRIBE <table_name>;

mysql>SELECT * FROM R_Actor;

The table created with the CREATE TABLE.. SELECT is created based on the output
of the SELECT query. In this method table options will not be copied.

MySQL Tutorials

MySQL Tutorials on this website:

https://www.testingdocs.com/mysql-tutorials-for-beginners/

For more information on MySQL Database:

https://www.mysql.com/

Exit mobile version