Skip to content
Related Articles

Related Articles

Improve Article

How to Copy a Table in MySQL Using Python?

  • Last Updated : 16 Nov, 2020
Geek Week

 In this article, we will create a table in MySQL and will create a copy of that table using Python. We will copy the entire table, including all the columns and the definition of the columns, as well as all rows of data in the table.

To connect to MySQL database using python, we need PyMySql module. The cursor class allows python to execute SQL commands. Cursors are created by connection_name.cursor() method, where connection_name is the link made to SQL Database. Once the connection is established, cursor.execute() is used to run the SQL statements.

Let us understand the above by taking an example. Suppose, in MySQL we create a database test and it contains a table named geeksfoegeeks and has the below schema and following data:

SQL Database

To Copy a Table in MySQL we use the below query:
 

CREATE TABLE table-name SELECT * FROM table-name;

Now, below is the program to copy the entire table using python:



Python3




# import required modules
import pymysql
  
  
# establish connection to SQL database
connection = pymysql.connect(
    
    # specify hostname
    host="localhost",
      
    # specify user of mysql database
    user="root",
      
    # specify password for above user
    password="1234",
      
    # default port number for mysql is 3306
    port=3306,
      
    # specify database name on which you want to work
    db="test"
)
  
  
# make a cursor
mycursor = connection.cursor()
  
# create a new table geeksforgeekscopy and copy all 
# records from geeksfoegeeks into the newly created table
mycursor.execute("create table geeksforgeekscopy select * from geeksfoegeeks")
  
# list all the tables
mycursor.execute("Show tables")
  
# fetchall() will store all the names 
# of tables into query1
query1 = mycursor.fetchall()
  
# print name of tables
for i in query1:
    print(i)
  
# read all records from copy table
mycursor.execute("Select * from geeksforgeekscopy")
  
# fetchall() will store all the records 
# of copy table into query2
query2 = mycursor.fetchall()
  
# print all records
for i in query2:
    print(i)

Output:

Python output

In the above figure, we could see the list of tables followed by all records from the geeksforgeekscopy table. The above output has also been confirmed by providing the output of MySQL database.

MySQL Output

Here is another example which depicts how to create a new table from the data and schema of a previous table. Below is the previously existing table:

Now, using the below script to create a copy of the above table in the database:

Python3




# import required modules
import pymysql
  
# connect python with mysql with your hostname, 
# username, password and database
connection= pymysql.connect("localhost", "root", "", "geek")
  
# make a cursor
mycursor = connection.cursor()
  
# create a new table and copy all records from 
# previous table into the newly created table
mycursor.execute("create table geeksdemocopy select * from geeksdemo")
  
# list all the tables
mycursor.execute("Show tables")
  
# fetchall() will store all the names of tables into query1
query1 = mycursor.fetchall()
  
# print name of tables
for i in query1:
    print(i)
  
# read all records from copy table
mycursor.execute("Select * from geeksdemocopy")
  
# fetchall() will store all the records of copy table into query2
query2 = mycursor.fetchall()
  
# print all records
for i in query2:
    print(i)

Output:

Below is the new table whose data and schema are copied from the previous table:

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :