MySQL Replication Table Structure

Source: Internet
Author: User

Introduction

Sometimes we need to copy the table structure of a table intact to generate a new table, and MySQL provides two convenient methods.

Cases:

 create  table   Tb_base (id  int  not  null  primary  key  ,name  varchar  (10   key   Ix_name (name) ENGINE  =  Span style= "color: #ff0000;" > '  myisam   ' , Charset; 
Insert  into Values (1,'a'), (2,'b');

First, like method

The like method can replicate the results of a table in exactly the same way as a new table, including the notes, indexes, primary key foreign keys, storage engines, etc. of the copied table.

CREATE [temporary] TABLE [IF not EXISTS] tbl_name      like | ( like Old_tbl_name)}

1. Copying a table

CREATE  TABLE IF  not EXISTS tb_base_like ( like

2. View the table

You can see that the newly copied table is exactly the same as the original table.

Second, select method

CREATE [temporary] TABLE [IF not EXISTS] Tbl_name SELECT ...   (SomeSelectorUnion statement)

1. Copying a table

CREATE  TABLE IF  not EXISTS SELECT *  from

2. View the table

Like method values copy field properties, other primary keys, indexes, table notes, storage engines are not replicated

Three, the difference

Like method: The Like method is actually a method that specifically replicates the table structure, but it replicates only the structure and related properties of the table and does not replicate the data

Select method: Strictly speaking, the Select method does not understand the method of copying table structure, in fact, it just executes a SELECT query statement, so the result of the copy contains only the fields and data of select, the other table properties have the configuration file of the system, including the storage engine, The default character set, and so on, is determined by the system's default configuration.

Summary

So the real table structure replication method is the like method, if you do not need to consider the table's original properties including the storage engine, notes, primary key, index, etc. then the Select Copy method is a good method and can also be copied with the data.

Note:

pursuer.chen

Blog:http://www.cnblogs.com/chenmh

This site all the essays are original, welcome to reprint, but reprint must indicate the source of the article, and at the beginning of the article clearly give the link.

Welcome to the exchange of discussions

MySQL Replication Table Structure

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.