Mysql replication table structure, table data methods _mysql

Source: Internet
Author: User
Tags db2 create database

This article has shared several kinds of replication table structure, table data examples, details, please see below.

1. Copy table structure and data to new table

CREATE Table New Table select * from old table

This method copies all the contents of the oldtable, and of course we can delete them with the delete from newtable.

But one of the worst aspects of this approach is that the new table has no attributes such as the primary key, Extra (auto_increment) of the old table. You need to add "alter" yourself, and it's easy to get it wrong.

2. Copy table structure to new table only

CREATE Table New Table select * from old table where 1=2

or CREATE table new table like old table

3, copy the old table data to the new table (assuming two table structure)

INSERT into new Table select * from old table

4, copy the old table data to the new table (assuming that two table structure is not the same)

INSERT into new Table (Field 1, Field 2,.......) SELECT Field 1, Field 2,...... From old table

5, you can copy the table 1 structure to table 2

SELECT * into table 2 from table 1 WHERE 1=2

6. You can copy table 1 to table 2

SELECT * into table 2 from table 1

7, show create table old tables;

This lists the creation commands for the old table. We just need to copy the command, change the name of the table, and we can create a table that's exactly the same.

8, Mysqldump

Dump the table with mysqldump, change the name, and then guide it back or run directly on the command line

9. Copy old database to new database (copy all table structure and copy all table data)

#mysql-U Root-ppassword
>create DATABASE new_db;
#mysqldump old_db-u Root-ppassword--skip-extended-insert--add-drop-table | MySQL new_db-u Root-ppassword


10, the table is not in the same database (for example, DB1 table1, DB2 table2)
Sql:insert into Db1.table1 select * from Db2.table2 (full copy)
INSERT INTO DB1.TABLE1 SELECT DISTINCT * from Db2.table2 (do not replicate duplicate records)
Insert INTO TDB1.ABLE1 select Top 5 * DB2.TABLE2 (first five records) the above content is the entire description of this article, I hope you like.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.