MySQL replication table structure, mysql replication Structure

Source: Internet
Author: User

MySQL replication table structure, mysql replication Structure
Introduction

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

 

Example:

CREATE TABLE tb_base(id INT NOT NULL PRIMARY KEY,name VARCHAR(10),KEY ix_name (name))ENGINE='MyISAM',CHARSET=utf8,COMMENT 'a' ;
insert into tb_base() values(1,'a'),(2,'b');

I. LIKE Method

The like method can replicate the results of a table to generate a new table, including the remarks, indexes, primary key foreign keys, and storage engine of the copied table.

CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name    { LIKE old_tbl_name | (LIKE old_tbl_name) }

1. Copy a table

CREATE  TABLE IF NOT EXISTS tb_base_like (LIKE tb_base); 

2. view the table

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

Ii. SELECT Method

CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_nameSELECT ...   (Some valid select or union statement)

1. Copy a table

CREATE  TABLE IF NOT EXISTS tb_base_select SELECT * FROM tb_base; 

2. view the table

Like method value copying field attribute, other primary key, index, table remarks, storage engine are not copied

Iii. Differences

Like method: The like method is actually a special method for copying table structures, but it only copies the structure and related properties of the table and does not copy data.

Select method: strictly speaking, the select method cannot be understood as a method for copying the table structure. In fact, it only executes a select query statement. Therefore, the copied result only contains select fields and data, other table attributes are determined by system configuration files, including storage engines and default character sets.

Summary

Therefore, the real table structure replication method is the LIKE method, if you do not need to consider the original attributes of a table, such as storage engine, remarks, primary keys, and indexes, the select COPY method is a good method and can be copied together with data.

 

 

 

Note:

Author: pursuer. chen

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

All essays on this site are original. You are welcome to repost them. However, you must indicate the source of the article and clearly give the link at the beginning of the article.

Welcome to discussion

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.