Quickly create an empty table in MYSQL

Today I was asked how we can create an empty table?
There are two ways in MYSQL.
1, the SELECT ... the Create the Table
2, the Create the Table like ...
The first lot of people know, but very few people use the second.
The first has a drawback :
cancel some of the original definition of the table.

The manual is say so:
. S Some of the Data Conversion types Might Occur the For Example, at The AUTO_INCREMENT attribute not Preserved IS, CAN Become CHAR and VARCHAR the Columns the Columns.
But I tested, will only cancel increment property! (May be a different version of it. Other versions are not tested!)



The second will not.
Let's look at an example:
MySQL> T_old the Create the Table (the above mentioned id Serial, Content VARCHAR (8000) not null, `desc` VARCHAR (100) not null) Engine InnoDB;
Query the OK, 0 rows affected (0.01 sec)

MySQL> Show the Create table t_old;
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                                                                                 |
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| t_old | CREATE TABLE `t_old` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  content` VARCHAR `(8000) the NOT NULL,
  ` desc` VARCHAR (100) the NOT NULL,
  UNIQUE KEY `id` (` id`)
) ENGINE = InnoDB the DEFAULT CHARSET = utf8 |
+ ------- + - -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- + ----
. 1 in Row SET (0.00 sec)

MySQL> Create Table t_select SELECT * WHERE from T_old. 1 = 0;
Query the OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0

the PS: To to maintain the same engine, they add.
Write: Create Table t_select Engine InnoDB SELECT * WHERE from T_old. 1 = 0;
MySQL> Create Show 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=utf8 |
+----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
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=utf8 |
+--------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

This article comes from " God, let there or be square! " Blog, reproduced please contact the author!

Reproduced in: https: //my.oschina.net/u/585111/blog/219489

Guess you like

Origin blog.csdn.net/weixin_33800463/article/details/92008304