mysql快速建表的方法

来源:互联网 发布:如何在淘宝买到好衣服 编辑:程序博客网 时间:2024/05/18 16:55

mysql快速建表的语句写法并不复杂,下面就为您详细介绍两种最常用的mysql快速建表的语句:

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

但是第一种mysql快速建表的语句有缺陷,他能取消原来表的有些定义。(手册上说Some conversion of data types might occur. For example, the AUTO_INCREMENT attribute is not preserved, and VARCHAR columns can become CHAR columns. )
可以看看下面的例子

  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.  

这样 自增字段跟表引擎都变了
如果想要保持一样的引擎,就加上:engine innodb
如:

  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.  

这样引擎跟自增字段都没有变

看下面一个一个例子,就知道有什么变化了

  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.  

【编辑推荐】

MySQL大表备份的简单方法

MySQL中文建表问题解析

MySQL添加字段和修改字段的方法

MySQL授权表使用示例

MySQL内存表的弊端



转载于:http://database.51cto.com/art/201011/234765.htm




0 0
原创粉丝点击