当前位置: 首页 > 图文教程 > 数据库 > MYSQL > MySQL中两种快速创建空表的方式的区别

MYSQL
SQL Server与Oracle数据库在查询优化上的差异
轻松掌握怎样从Windows命令行启动MySQL
教你轻松掌握MaxDB和MySQL之间的协同性
教你轻松了解MySQL数据库中的结果字符串
解析:轻松了解 MySQL中损坏的MyISAM表
解析:MySQL 数据库搜索中大小写敏感性
实例解析:MySQL 实例管理器识别的命令
快速掌握 Mysql数据库对文件操作的封装
帮助你分析MySQL的数据类型以及建库策略
从MySQL导大量数据的程序实现方法
MySQL数据库中设列的默认值为Now()的介绍
如何将txt文本中的数据轻松导入MySQL表中
带你深入了解MySQL数据库系统参数的优化
初学MySql5 所应了解的知识和常见问题
MYSQL数据库实用学习资料之常用命令集合
MySQL数据库配置技巧
Mysql数据库常用命令
计划备份mysql数据库
一次MySQL性能优化实战
MySQL乱码问题深层分析

MYSQL 中的 MySQL中两种快速创建空表的方式的区别


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-17   浏览: 56 ::
收藏到网摘: n/a

在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>