How to copy MySQL data tables and table structure _ MySQL

Source: Internet
Author: User
How to copy MySQL data tables and table structures bitsCN.com

How to copy MySQL data tables and table structures

This section describes how to copy MySQL, table structure, and fields. You can also copy data from one table to another.

1. copy the table structure (syntax: creata table old table select * from NEW table)

create table t1(  id int unsigned auto_increment primary key,  name varchar(32) not null default '',  pass int not null default 0 );

Desc view table structure

Create table t2 copy table t1 table structure create table t2 select * from t1;

View table structure in desc t2

Note: The fields in the two tables have the same structure, but the primary key and auto-increment auto_increment are absent. Therefore, this method is not recommended, so how can we create two identical tables? there are certainly some solutions, as shown in the following statement.

create  table  t2 like t1; 

This allows you to create a table that is exactly the same as t2 and t1.

2. specify the field to copy the table structure

Syntax: create table new table select field 1, field 2... From old table

3. Copy table data

Suppose you want to copy all the data in table t1 to insert into t2 select * from t1 in table t2. if you only want to copy a field insert into t2 (Field 1, field 2) select field 1, field 2 from t1;


BitsCN.com
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.