The difference between two quick ways to create empty tables in MySQL

Source: Internet
Author: User
Tags mysql create database

There are two ways to do this in MySQL

1, CREATE TABLE T_name Select ...

2, CREATE table t_name like ...

The first one cancels out some definitions of the original table, and the engine is the system default engine.

This is what the manual says: Some Conversion of data types might occur. For example, the auto_increment are not preserved, and VARchar columns can become char columns.

The second is a complete copy of the original table.

First set up the test table:

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)

The first way:

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)

The second way:

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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.