Copy a MySQL Table

MySQL provides different ways to copy a table depending on whether you need only the structure, only selected data, or both.

Copy MySQL table structure and data

Copy structure and data

CREATE TABLE student2 AS SELECT * FROM student;

This copies selected columns and rows into a new table, but it does not automatically preserve every index or attribute from the source table.

Copy selected rows

CREATE TABLE student2 AS SELECT *
FROM student
WHERE class = 'Four';

Copy selected columns

CREATE TABLE student2 AS SELECT id, name, mark
FROM student;

IF NOT EXISTS

CREATE TABLE IF NOT EXISTS student5 AS SELECT *
FROM student
WHERE class = 'Four';

IF NOT EXISTS prevents an error when the destination name already exists; it does not verify that the existing structure matches the source.

Re-create a disposable copy

DROP TABLE IF EXISTS student5;
CREATE TABLE student5 AS SELECT * FROM student;
DROP TABLE permanently removes the destination table. Use it only when that replacement behavior is intentional.

Preserve structure and then copy data

When keys and table attributes matter, first copy the structure with LIKE, then insert the rows:

CREATE TABLE student2 LIKE student;
INSERT INTO student2
SELECT * FROM student;

Copy only the structure

CREATE TABLE student2 LIKE student;

CREATE TABLE ... LIKE copies the table definition, including indexes, without copying rows.

Show the original CREATE statement

SHOW CREATE TABLE student;

PHP PDO example

<?php
require 'config.php';
$tableName = 'student';
// Use a trusted/whitelisted table name; identifiers cannot be bound like values.
$stmt = $dbo->query("SHOW CREATE TABLE `student`");
$row = $stmt->fetch(PDO::FETCH_ASSOC);
echo $row['Create Table'];
?>



Subscribe to our YouTube Channel here



plus2net.com
dfgf

19-02-2009

This is a very useful information. Thank you very much.... Everywhere else i found info to do this in 2 steps, but this method saves a lot of work.
demonsmile

15-03-2009

wa bng clear discussion about sa copying of data in the table to table
Hiromitsu

05-06-2009

Thanks for this tutorial. It works great !!
siddhartha singh

10-06-2009

this is very clear cut way to explain the things,one can easily learn the points by just reading the txts
Rudy Warjri

09-07-2009

please giv me a more detailed explanation to calculate the marks from one table and then calculate the total sum in another table by using some php code
Willy

06-08-2009

Will this work in ACCESS 2007?
matthew

08-10-2009

It doesn't work very well. Particular fields of newly created table has to be altered if there were extra properties in the fields of the source.
Fadi

10-12-2009

Greate work thanks. It really helped me
Abid

18-12-2009

Thanks for this info.. but if what if i've just to copy only the structure.....??
Dave

22-02-2010

always double-check that the new table has the same indexes as the source table - some versions don't copy the indexes when you do a CREATE TABLE student2 SELECT * FROM student with or without the LIMIT 0 :)
seenu

02-03-2010

Thanks for providing the valuable information, this helps a lot to learn the concept's.
Vilart

08-03-2010

Thank so much for your best information. God bless you.
Anand

09-03-2010

Hi, this query will work... select * into A from B where A- new table name and B - old table name.. the table with the same column, data and etc,etc are created... this worked in SQL 2005
manoj kumar bardhan

07-04-2010

Its very help full..
JAISHI RAM

22-05-2010

I want to copy table1 into table2 with structure and data in the same database. I used the cammand CREATE TABLE student2 SELECT * FROM student on button click event and also on sql moblie query but can not make copy. So u r requested to pl. kindly solve my this problem with example. Thanking u.
Raaj

04-06-2010

i want to copy table structure only in SQL2005.. can you please help me?
Scot King

10-06-2010

How do I copy data from table into tablebackup that is external to the current database?
Satish

05-08-2010

Thanks for the info, anyone have tired to create a Multiple Tables with the Automatic Names given to New Table Created , Queried from a Master Table in same database Example: "mstr_Student_tbl". Here I want a Table generated as "tbl_stundentID" automatically where the structure is same as in a Model Table Model_Stundent_tbl If that is a SQL It will be like ..?? just 2 bit to start... CREATE TABLE mstr_Student_tbl.ID LIKE Model_Student_tbl Thanks in advance for help regards Satish
Pankaj Kumar GUpta

29-09-2010

i try to copy only structure and create a new table and use this Query "create table t1 like student" when i use this Query it not work any one give me suggestion
sam

08-11-2010

how to merge two tables in php mysql database? Same field name records not deleted.All records save in new table.
Adil

13-01-2011

@Raaj -- copy table structure only no data; CREATE TABLE Table_NAME SELECT * FROM Table_NAME_copy where 1 = 2;
el-ahmed mahmood

09-02-2011

i would like a SQL statement that define the structure and content of a table containing student profile
Narendra Kumar

16-06-2011

I would like to told you that how to copy the one table data into another. INSERT INTO Table1 (Column1, ..., ColumnN) SELECT Column1, ..., ColumnN FROM Table2
sunny

09-09-2011

With INSERT ... SELECT, you can quickly insert many rows into a table from one or many tables. For example: INSERT INTO tbl_temp2 (fld_id) SELECT tbl_temp1.fld_order_id FROM tbl_temp1 WHERE tbl_temp1.fld_order_id > 100;
ashish shukla

01-11-2012

its very benificial for fresher,.....
Saeed

07-11-2014

Thank you so much .... This website very helpfull ...*****




SQL Video Tutorials










✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer