Home > Database > Mysql Tutorial > body text

How do we make a MySQL clone table?

WBOY
Release: 2023-09-15 10:49:02
forward
1356 people have browsed it

我们如何制作 MySQL 克隆表?

Sometimes there may be a situation where we need an exact copy of a table and CREATE TABLE ... SELECT is not suitable for us purpose, since the copy must contain the same indexes, default values, etc.

You can follow the steps below to handle this situation and create a clone of the table as shown below-

  • Use SHOW CREATE TABLE to get the CREATE TABLE statement, which specifies the structure of the source table , index, etc.
  • Modify the statement, change the table name to the table name of the clone table, and then execute the statement. This way we will get an exact clone table.
  • Alternatively, if we also need to copy the table contents, we can also issue an INSERT INTO ... SELECT statement.

Example

In the following example, we will create a clone table for tutorials_tbl.

Step 1

Get the complete structure of the table.

mysql> SHOW CREATE TABLE tutorials_tbl \G;

*************************** 1. row ***************************
Table: tutorials_tbl
Create Table: CREATE TABLE `tutorials_tbl` (
   `tutorial_id` int(11) NOT NULL auto_increment,
   `tutorial_title` varchar(100) NOT NULL default '',
   `tutorial_author` varchar(40) NOT NULL default '',
   `submission_date` date default NULL,
   PRIMARY KEY (`tutorial_id`),
   UNIQUE KEY `AUTHOR_INDEX` (`tutorial_author`)
) TYPE = MyISAM
1 row in set (0.00 sec)
ERROR:
No query specified
Copy after login

Step 2

Rename the table and create another table.

mysql> CREATE TABLE clone_tbl (
    -> tutorial_id int(11) NOT NULL auto_increment,
    -> tutorial_title varchar(100) NOT NULL default '',
    -> tutorial_author varchar(40) NOT NULL default '',
    -> submission_date date default NULL,
    -> PRIMARY KEY (tutorial_id),
    -> UNIQUE KEY AUTHOR_INDEX (tutorial_author)
    -> ) TYPE = MyISAM;
Query OK, 0 rows affected (1.80 sec)
Copy after login

Step 3

After performing step 2, you will create a clone table in the database. If you want to copy data from an old table, you can do so using the INSERT INTO...SELECT statement.

mysql> INSERT INTO clone_tbl (tutorial_id, tutorial_title, tutorial_author, submission_date)
    -> SELECT tutorial_id,tutorial_title,
    ->    tutorial_author,submission_date
    -> FROM tutorials_tbl;
Query OK, 3 rows affected (0.07 sec)
Records: 3 Duplicates: 0 Warnings: 0
Copy after login

In the end, we will have a clone table that is exactly what you want.

The above is the detailed content of How do we make a MySQL clone table?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!