MySQL中两种快速创建空表的方式的区别
|
源代码网整理以下在MySQL中有两种方法 源代码网整理以下1、create table t_name select ... 源代码网整理以下2、create table t_name like ... 源代码网整理以下第一种会取消掉原来表的有些定义,且引擎是系统默认引擎。 源代码网整理以下手册上是这么讲的:Some conversion of data types might occur. For example, the AUTO_INCREMENT attribute is not preserved, and VARCHAR columns can become CHAR columns. 源代码网整理以下第二种就完全复制原表。 源代码网整理以下先建立测试表: 源代码网整理以下mysql> create database dbtest; 源代码网整理以下Query OK, 1 row affected (0.03 sec) 源代码网整理以下mysql> use dbtest; 源代码网整理以下Database changed 源代码网整理以下mysql> create table t_old 源代码网整理以下-> ( 源代码网整理以下-> id serial, 源代码网整理以下-> content varchar(8000) not null, 源代码网整理以下-> `desc` varchar(100) not null) 源代码网整理以下-> engine innodb; 源代码网整理以下Query OK, 0 rows affected (0.04 sec) 源代码网整理以下mysql> show create table t_old; 源代码网整理以下+-------+-------------------------------------------------+ 源代码网整理以下| Table | Create Table | 源代码网整理以下+-------+------------------------------------------------+ 源代码网整理以下| t_old | CREATE TABLE `t_old` ( 源代码网整理以下`id` bigint(20) unsigned NOT NULL auto_increment, 源代码网整理以下`content` varchar(8000) NOT NULL, 源代码网整理以下`desc` varchar(100) NOT NULL, 源代码网整理以下UNIQUE KEY `id` (`id`) 源代码网整理以下) ENGINE=InnoDB DEFAULT CHARSET=latin1 | 源代码网整理以下+-------+----------------------------------------------------+ 源代码网整理以下1 row in set (0.00 sec) 源代码网整理以下第一种方式: 源代码网整理以下mysql> create table t_select select * from t_old where 1 = 0; 源代码网整理以下Query OK, 0 rows affected (0.04 sec) 源代码网整理以下Records: 0 Duplicates: 0 Warnings: 0 源代码网整理以下mysql> show create table t_select; 源代码网整理以下+----------+--------------------------------------------+ 源代码网整理以下| Table | Create Table +----------+---------------------------------------------+ 源代码网整理以下| t_select | CREATE TABLE `t_select` ( 源代码网整理以下`id` bigint(20) unsigned NOT NULL default "0", 源代码网整理以下`content` varchar(8000) NOT NULL, 源代码网整理以下`desc` varchar(100) NOT NULL 源代码网整理以下) ENGINE=MyISAM DEFAULT CHARSET=latin1 | 源代码网整理以下+----------+-------------------------------------------+ 源代码网整理以下1 row in set (0.00 sec) 源代码网整理以下第二种方式: 源代码网整理以下mysql> create table t_like like t_old; 源代码网整理以下Query OK, 0 rows affected (0.02 sec) 源代码网整理以下mysql> show create table t_like; 源代码网整理以下+--------+-------------------------------------------------+ 源代码网整理以下| Table | Create Table | 源代码网整理以下+--------+-------------------------------------------------+ 源代码网整理以下| t_like | CREATE TABLE `t_like` ( 源代码网整理以下`id` bigint(20) unsigned NOT NULL auto_increment, 源代码网整理以下`content` varchar(8000) NOT NULL, 源代码网整理以下`desc` varchar(100) NOT NULL, 源代码网整理以下UNIQUE KEY `id` (`id`) 源代码网整理以下) ENGINE=InnoDB DEFAULT CHARSET=latin1 | 源代码网整理以下+--------+-------------------------------------------------+ 源代码网整理以下1 row in set (0.00 sec) 源代码网整理以下mysql> 源代码网供稿. |
