阳光网驿-企业信息化交流平台【DTC零售连锁全渠道解决方案】

 找回密码
 注册

QQ登录

只需一步,快速开始

扫描二维码登录本站

手机号码,快捷登录

老司机
查看: 1554|回复: 2

[转帖] MySQL中两种快速创建空表的方式的区别

[复制链接]
  • TA的每日心情
    开心
    2012-3-7 10:15
  • 签到天数: 11 天

    [LV.3]偶尔看看II

    发表于 2012-1-31 10:43:32 | 显示全部楼层 |阅读模式

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


    楼主热帖
    启用邀请码注册,提高发帖质量,建设交流社区
  • TA的每日心情
    擦汗
    2020-4-5 13:10
  • 签到天数: 1112 天

    [LV.10]以坛为家III

    发表于 2013-5-18 16:36:32 | 显示全部楼层
    学习学习了,支持一下////
    启用邀请码注册,提高发帖质量,建设交流社区
  • TA的每日心情
    无聊
    2021-8-10 17:31
  • 签到天数: 950 天

    [LV.10]以坛为家III

    发表于 2013-6-5 10:04:31 | 显示全部楼层
    看不懂,也支持一下{:soso_e128:}…………
    启用邀请码注册,提高发帖质量,建设交流社区
    您需要登录后才可以回帖 登录 | 注册

    本版积分规则

    快速回复 返回顶部 返回列表