JIYIK CN >

Current Location:Home > Learning > DATABASE > MySQL >

Copying rows in a MySQL database

Author:JIYIK Last Updated:2025/04/11 Views:

Today's topic is about duplicating rows in MySQL database. We will learn how to copy a row and paste it into the same table with auto increment ID and custom ID.

We will also see how to copy multiple fields from multiple rows of one table and paste them into another table.


Copying rows in a MySQL database

Copying rows is useful when you need two or more exact columns from another table. We can copy a row (a record) from one table to another instead of inserting them manually.

We can use different methods to copy rows in a MySQL database. First, to learn these methods, let's create a database with two tables tb_studentsand .tb_attendancetest

Sample code:

# create a database
CREATE SCHEMA `test`;

# create students table
CREATE TABLE `test`.`tb_students` (
  `ID` INT NOT NULL AUTO_INCREMENT,
  `FIRSTNAME` VARCHAR(45) NOT NULL,
  `LASTNAME` VARCHAR(45) NOT NULL,
  `DEPARTMENT` VARCHAR(45) NOT NULL,
  `GENDER` VARCHAR(10) NOT NULL,
  `PHONE` BIGINT NOT NULL,
  `CITY` VARCHAR(45) NOT NULL,
  PRIMARY KEY (`ID`));

# create attendance table
CREATE TABLE `test`.`tb_attendance` (
  `ID` INT NOT NULL,
  `FIRSTNAME` VARCHAR(45) NOT NULL,
  `LASTNAME` VARCHAR(45) NOT NULL,
  `GENDER` VARCHAR(45) NOT NULL,
  `DEPT` VARCHAR(45) NOT NULL,
  `ATTENDANCE` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`ID`));

INSERTUse the and statements in MySQL database SELECTto copy rows from one table to another

We must insert data into one table before we can copy rows from that table into another. To do this, we populate tb_studentsa table named as follows.

Sample code:

#insert data into the table
INSERT INTO `test`.`tb_students`
(FIRSTNAME, LASTNAME, DEPARTMENT, GENDER, PHONE, CITY)
VALUE
('Mehvish', 'Ashiq', 'Computer Science', 'Female', '1234567890', 'Lahore'),
('Thomas', 'Christopher', 'Physics', 'Male', '2546317908', 'Miami'),
('Daniel', 'James', 'Business Administration', 'Male', '7854123690', 'Texas'),
('Saira', 'Kethy', 'History', 'Female', '3254169870', 'Michigan');

#display table data
SELECT * FROM test.tb_students;

Output:

Now, tb_studentscopy the data from the table and insert it tb_attendaceinto the table. Note that we are not copying all the data, but specific fields to populate tb_attendancethe table.

#insert data into the table
INSERT INTO `test`.`tb_attendance`
(ID, FIRSTNAME, LASTNAME, GENDER, DEPT)
SELECT ID, FIRSTNAME, LASTNAME, GENDER, DEPARTMENT
FROM `test`.`tb_students`;

#display table data
SELECT * FROM test.tb_attendance;

Output:


Use the INSERTand SELECTstatements to copy rows in the same table with auto-increment IDs

In this section, we tb_studentscopy a row from the table containing four records, insert it with an auto-increment ID, and update the values ​​of the PHONEand CITYattributes.

Sample code:

INSERT INTO `test`.`tb_students`
(FIRSTNAME, LASTNAME, DEPARTMENT, GENDER, PHONE, CITY)
SELECT FIRSTNAME, LASTNAME, DEPARTMENT, GENDER, '2564138790', 'Dubai'
FROM `test`.`tb_students`
WHERE `test`.`tb_students`.ID = 1;

SELECT * FROM `test`.`tb_students`;

Output:


Use the INSERTand SELECTstatements to copy rows from the same table with a custom ID

If we do not want tb_studentsto use auto-increment ID in the table but customize the ID, we can INSERTwrite the column name in the statement and SELECTits value in the statement as shown below.

Sample code:

INSERT INTO `test`.`tb_students`
(ID, FIRSTNAME, LASTNAME, DEPARTMENT, GENDER, PHONE, CITY)
SELECT 10,FIRSTNAME, LASTNAME, DEPARTMENT, GENDER, '2564138790', 'Dubai'
FROM `test`.`tb_students`
WHERE `test`.`tb_students`.ID = 1;

SELECT * FROM `test`.`tb_students`;

Output:

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.

Article URL:

Related Articles

If ELSE in MySQL

Publish Date:2025/04/11 Views:85 Category:MySQL

In this tutorial, we aim to explore how to use IF ELSE the statement in MySQL. One of the key roles of a data analyst is to gather insights from the data and produce meaningful results. It can be done with the help of several data filtering

DATETIME vs. TIMESTAMP in MySQL

Publish Date:2025/04/11 Views:117 Category:MySQL

DATETIME and TIMESTAMP are two different data types that can be used to store values ​​that must contain both a date and a time portion. In this article, we will understand how it is stored in the database and the memory required for ea

Execute multiple joins in one query in MYSQL

Publish Date:2025/04/11 Views:94 Category:MySQL

Have you ever wondered how to include multiple joins in one query in MySQL? You have come to the right place. Remember that joins allow us to access information from other tables. This information is included separately to avoid redundancy.

Joining 3 tables in MySQL

Publish Date:2025/04/11 Views:187 Category:MySQL

In this tutorial, we will learn how to join three tables in MySQL. Businesses and organizations may have to visualize three tables simultaneously based on certain matching columns common to all three tables. This operation is allowed in MyS

Use of UPDATE JOIN in MySQL

Publish Date:2025/04/11 Views:85 Category:MySQL

This tutorial will explain how to use the statement in MySQL database UPDATE JOIN . We generally use joins to iterate over the rows in a particular table which may or may not have similar rows in other tables. We can UPDATE use JOIN the cla

How to use the Row_Number() function in MySQL

Publish Date:2025/04/11 Views:142 Category:MySQL

In this tutorial, we will explain how to use the VALUES function in MySQL ROW_NUMBER() . This is a sorting method that assigns consecutive numbers within a partition starting from 1. It is important to note that no two rows within a partiti

Multiple primary keys in MySQL

Publish Date:2025/04/11 Views:66 Category:MySQL

In this tutorial, our goal is to explore the concept of multiple primary keys for a table in MySQL. Many times, businesses and organizations have to assign certain columns as primary keys. This primary key has multiple uses and reasons to b

Displaying foreign keys in MySQL

Publish Date:2025/04/11 Views:55 Category:MySQL

In this tutorial, we aim to explore how to display foreign keys for tables and columns in MySQL. The type of key that references a primary key, also known as the primary key of another table, is called a foreign key. Understanding the forei

Select first N rows in MySQL

Publish Date:2025/04/11 Views:85 Category:MySQL

Sometimes, you have to select first N rows of MySQL database according to your project requirements. n The value of varies according to the requirement; it can be TOP 1 row or TOP 30 rows. We will learn how to select top N rows using the cl

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial