Quick table creation in mysql

Source: Internet
Author: User

The statement writing for mysql quick table creation is not complex. The following describes the two most commonly used statements for mysql quick table creation:

 
 
  1. 1:create table t_select select * from t_old where 1 = 0;  
  2. 2:create table t_select1 like t_old;  

However, the first mysql quick table creation statement has a defect and can cancel some definitions of the original table. Some conversion of data types might occur. For example, the AUTO_INCREMENT attribute is not preserved, and VARCHAR columns can become CHAR columns .)
Let's take a look at the example below.

 
 
  1. create table t_old (id serial, content varchar(8000) not null,`desc` varchar(100) not null) engine innodb;  
  2. show CREATE table t_old;  
  3. | Table | Create Table                                                                        
  4.  
  5.  | t_old | CREATE TABLE `t_old` (  
  6. `id` bigint(20) unsigned NOT NULL auto_increment,  
  7. `content` varchar(8000) NOT NULL,  
  8. `desc` varchar(100) NOT NULL,  
  9. UNIQUE KEY `id` (`id`)  
  10. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 
  11.  
  12. create table t_select select * from t_old where 1 = 0;   
  13. CREATE TABLE `t_select` (  
  14. `id` bigint(20) unsigned NOT NULL default '0',  
  15. `content` varchar(8000) NOT NULL,  
  16. `desc` varchar(100) NOT NULL  
  17. ) ENGINE=MyISAM DEFAULT CHARSET=utf8   
  18.  

In this way, the auto-increment field and table engine have changed.
If you want to maintain the same engine, add: engine innodb
For example:

 
 
  1. create table t_select engine innodb select * from t_old where 1 = 0; create table t_like like t_old;  
  2. show CREATE table t_like;  
  3. Table                                                    | t_like | CREATE TABLE `t_like` (  
  4. `id` bigint(20) unsigned NOT NULL auto_increment,  
  5. `content` varchar(8000) NOT NULL,  
  6. `desc` varchar(100) NOT NULL,  
  7. UNIQUE KEY `id` (`id`)  
  8. ) ENGINE=InnoDB DEFAULT CHARSET=utf8   
  9.  

In this way, the engine and auto-increment fields are not changed.

Let's look at one of the following examples to see what has changed.

 
 
  1. CREATE TABLE `t4_innodb` (                 
  2. `id` int(11) NOT NULL AUTO_INCREMENT,    
  3. `a1` int(11) NOT NULL,                   
  4. `a2` int(11) DEFAULT NULL,               
  5. `remark` varchar(200) NOT NULL,          
  6. PRIMARY KEY (`id`),                      
  7. KEY `a1_2_idx` (`a1`)                    
  8. ) ENGINE=InnoDB DEFAULT CHARSET=utf8     
  9.  
  10. create table t6_innodb select * from t4_innodb where 1=2;  
  11. CREATE TABLE `t6_innodb` (              
  12. `id` int(11) NOT NULL DEFAULT '0',    
  13. `a1` int(11) NOT NULL,                
  14. `a2` int(11) DEFAULT NULL,            
  15. `remark` varchar(200) NOT NULL        
  16. ) ENGINE=InnoDB DEFAULT CHARSET=utf8   
  17.  
  18. create table t8_innodb like t4_innodb;  
  19.  
  20. CREATE TABLE `t8_innodb` (                 
  21. `id` int(11) NOT NULL AUTO_INCREMENT,    
  22. `a1` int(11) NOT NULL,                   
  23. `a2` int(11) DEFAULT NULL,               
  24. `remark` varchar(200) NOT NULL,          
  25. PRIMARY KEY (`id`),                      
  26. KEY `a1_2_idx` (`a1`)                    
  27. ) ENGINE=InnoDB DEFAULT CHARSET=utf8    
  28.  

Simple MySQL large table backup method

Analysis of MySQL Chinese Table creation Problems

How to add and modify fields in MySQL

MySQL authorization table usage example

Disadvantages of MySQL memory tables

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.