mysql快速建表的方法

 2022-10-27    301  

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

1:createtablet_selectselect*fromt_oldwhere1=0; 
2:createtablet_select1liket_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. ) 可以看看下面的例子

mysql快速建表的方法

createtablet_old(idserial,contentvarchar(8000)notnull,`desc`varchar(100)notnull)engineinnodb; 
showCREATEtablet_old; 
|Table|CreateTable 

|t_old|CREATETABLE`t_old`( 
`id`bigint(20)unsignedNOTNULLauto_increment, 
`content`varchar(8000)NOTNULL, 
`desc`varchar(100)NOTNULL, 
UNIQUEKEY`id`(`id`) 
)ENGINE=InnoDBDEFAULTCHARSET=utf8

createtablet_selectselect*fromt_oldwhere1=0; 
CREATETABLE`t_select`( 
`id`bigint(20)unsignedNOTNULLdefault'0', 
`content`varchar(8000)NOTNULL, 
`desc`varchar(100)NOTNULL 
)ENGINE=MyISAMDEFAULTCHARSET=utf8 

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

createtablet_selectengineinnodbselect*fromt_oldwhere1=0;createtablet_likeliket_old; 
showCREATEtablet_like; 
Table|t_like|CREATETABLE`t_like`( 
`id`bigint(20)unsignedNOTNULLauto_increment, 
`content`varchar(8000)NOTNULL, 
`desc`varchar(100)NOTNULL, 
UNIQUEKEY`id`(`id`) 
)ENGINE=InnoDBDEFAULTCHARSET=utf8 

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

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

CREATETABLE`t4_innodb`( 
`id`int(11)NOTNULLAUTO_INCREMENT, 
`a1`int(11)NOTNULL, 
`a2`int(11)DEFAULTNULL, 
`remark`varchar(200)NOTNULL, 
PRIMARYKEY(`id`), 
KEY`a1_2_idx`(`a1`) 
)ENGINE=InnoDBDEFAULTCHARSET=utf8 

createtablet6_innodbselect*fromt4_innodbwhere1=2; 
CREATETABLE`t6_innodb`( 
`id`int(11)NOTNULLDEFAULT'0', 
`a1`int(11)NOTNULL, 
`a2`int(11)DEFAULTNULL, 
`remark`varchar(200)NOTNULL 
)ENGINE=InnoDBDEFAULTCHARSET=utf8 

createtablet8_innodbliket4_innodb; 

CREATETABLE`t8_innodb`( 
`id`int(11)NOTNULLAUTO_INCREMENT, 
`a1`int(11)NOTNULL, 
`a2`int(11)DEFAULTNULL, 
`remark`varchar(200)NOTNULL, 
PRIMARYKEY(`id`), 
KEY`a1_2_idx`(`a1`) 
)ENGINE=InnoDBDEFAULTCHARSET=utf8 

  •  标签:  
  • MySQL
  •  

原文链接:https://77isp.com/post/4519.html

=========================================

https://77isp.com/ 为 “云服务器技术网” 唯一官方服务平台,请勿相信其他任何渠道。