Copying a table in MySQL
The purpose of this article is to explore different ways to create a copy of a table in MySQL.
The source table is also called the table to be copied, and the target table is called the clone table, which can be from the same or a different database in the MySQL server.
We will explore the following methods of copying tables in MySQL:
-
Use
CREATE TABLE ... AS SELECT
the statement to clone a table. -
Use
WHERE
the clause to clone a table containing partial data.
Before we begin, we'll create a dummy dataset to work with. Here, we create a table, student_details
, and a few rows in it.
-- create the table student_details
CREATE TABLE student_details(
stu_id int,
stu_firstName varchar(255) DEFAULT NULL,
stu_lastName varchar(255) DEFAULT NULL,
primary key(stu_id)
);
-- insert rows to the table student_details
INSERT INTO student_details(stu_id,stu_firstName,stu_lastName)
VALUES(1,"Preet","Sanghavi"),
(2,"Rich","John"),
(3,"Veron","Brow"),
(4,"Geo","Jos"),
(5,"Hash","Shah"),
(6,"Sachin","Parker"),
(7,"David","Miller");
The above query creates a table and rows containing the first and last names of students. To view the entries in the data, we use the following code:
SELECT * FROM student_details;
The above code will give the following result:
stu_id stu_firstName stu_lastName
1 Preet Sanghavi
2 Rich John
3 Veron Brow
4 Geo Jos
5 Hash Shah
6 Sachin Parker
7 David Miller
Now, let's create a students_data_backup
copy of the above table called .
Use CREATE TABLE ... AS SELECT
the statement to clone a table
One of the most basic ways to create a copy of a table is to use CREATE TABLE ... AS SELECT
the SELECT statement. We can do this with the following code:
-- Basic copy table creation
create table students_data_backup as select * from student_details;
student_details
The above code creates a copy table named
from the table that acts as the source table students_data_backup
. We can visualize this new table using the following query students_data_backup
:
select * from students_data_backup;
The output of the above code is a temporary table as follows:
stu_id stu_firstName stu_lastName
1 Preet Sanghavi
2 Rich John
3 Veron Brow
4 Geo Jos
5 Hash Shah
6 Sachin Parker
7 David Miller
Use WHERE
the clause to clone a table with partial data
Sometimes, only certain specific parts need to be cloned. We can use WHERE
the clause and CREATE TABLE ... AS SELECT
statement to do this.
Let's try to partially clone student_details
the table. We choose stu_id
to clone the table using only the first five .
We can do this with the following code:
-- Cloning the student_details table with where clause
create table students_data_backup as select * from student_details WHERE stu_id <= 5;
The given code example will produce the following output:
stu_id stu_firstName stu_lastName
1 Preet Sanghavi
2 Rich John
3 Veron Brow
4 Geo Jos
5 Hash Shah
student_details
As we can see, copies of the tables
have been created , but only the first five stu_id
.
Using this technique, we can clone part of the data from a specific table as per our requirement. With the help of above two methods, we can easily create a copy of the source table.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
How to select from multiple tables in MySQL
Publish Date:2025/04/25 Views:57 Category:MySQL
-
This article explains how to use MySQL to query from multiple tables in one script SELECT . Let's demonstrate a situation: SELECT name , price, details, type , FROM food, food_order WHERE breakfast.id = 'breakfast_id' Now, let's imagine FRO
Creating a table from CSV in MySQL
Publish Date:2025/04/25 Views:114 Category:MySQL
-
In this article, we aim to understand how to create a table from CSV in MySQL database. Businesses and organizations must quickly generate tables from large amounts of data. These organizations typically have large CSV files with large amou
Creating a Temporary Table in MySQL
Publish Date:2025/04/25 Views:183 Category:MySQL
-
In this article, we aim to explore different ways to create temporary tables in MySQL. One of the main features of temporary tables is that it helps in storing temporary data. This feature is enabled in MySQL 3.23 and later versions. These
Truncate all tables in Mysql
Publish Date:2025/04/25 Views:89 Category:MySQL
-
Today I will show you how to truncate all tables in Mysql. It is used when you want to delete the entire table TRUNCATE TABLE . TRUNCATE It is a type of DML statement, which means it cannot be rolled back once it is committed. There are two
Different ways to check if a row exists in a MySQL table
Publish Date:2025/04/25 Views:163 Category:MySQL
-
This article highlights different ways to check if a row exists in a MySQL table. We will use the EXISTS and NOT EXISTS operators. We can also use these two operators with IF() the function to get a meaningful message if a row (a record) is
Check if table exists in MySQL
Publish Date:2025/04/25 Views:194 Category:MySQL
-
This article provides several options to check if a table exists in MySQL. Before discussing it, let us first see what a table is in MySQL and when you need to check its existence. What is a table in MySQL? A table is a database object that
Rename columns in MySQL database
Publish Date:2025/04/25 Views:80 Category:MySQL
-
In this article, we aim to explore different ways to rename columns in MySQL. ALTER TABLE The command is mainly used to change the format of a given MySQL table. It can be used to add columns, change the data type within a column, delete co
Get column names in MySQL
Publish Date:2025/04/25 Views:109 Category:MySQL
-
In this article, we aim to explore how to get the column names of a specific table in MySQL database. Often, when working with data in MySQL, we tend to forget the column names of a particular table in the database and the data types of dif
Get the version in MySQL
Publish Date:2025/04/24 Views:170 Category:MySQL
-
In this article, we will learn how to get the current version of MySQL on Windows systems. It is necessary for businesses to maintain versions of different tools and software used to run their business in order to keep systems compatible. T