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

MYSQL
MySQL 5.0默认100连接数的修改
如何使用"MySQL-Proxy"实现读写分离
phpMyAdmin下载、安装和使用入门
安装phpMyAdmin数据库管理软件
关于Mysql数据库导致CPU很高的问题解决
Mysql数据库的导入导出 和 Liunx的权限
MySQL InnoDB存储引擎的一些参数
MySQL InnoDB存储引擎的事务隔离级别
MySQL中InnoDB和MyISAM类型的差别
如何在.NET中访问MySQL数据库
关于 mysql5 改密码后不能登录问题的解答
初学者必读 MySQL 数据库常见问题汇总
MySQL字符集:怎样才能保证不发生乱码
详细讲解优化MySQL数据库性能的十个参数
教你使用MySQL触发器自动更新memcache
SQL存储过程和触发不能使用USE的应对方法
MySQL怎样处理一个溢出的磁盘
MySQL出错代码含义列表解释一表通
服务器安装MySQL教程及注意事项
完美解决mysql中文乱码的问题

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-17   浏览: 47 ::
收藏到网摘: 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>