The difference between CREATE table TB as select and create table TB like

Source: Internet
Author: User
Purpose: To test the difference between create table A as SELECT * from B and create table a like B

MySQL under test:

SOURCE table: Ti

Table structure is as follows

Root:test> Show CREATE TABLE Ti\g
1. Row ***************************
Table:ti
Create table:create Table ' ti ' (
' id ' int (one) DEFAULT NULL,
' Amount ' decimal (7,2) DEFAULT NULL,
' m_photo_big ' varchar DEFAULT NULL,
' Tr_date ' Date DEFAULT NULL,
' New_msg_flag ' tinyint (4) not NULL DEFAULT ' 0 ',
' Love_listreq ' int (3) DEFAULT ' 1 ',
' Love_listconfig ' int (3) DEFAULT ' 1 ',
KEY ' New_msg_flag ' (' New_msg_flag ')
) Engine=innodb DEFAULT charset=latin1
1 row in Set (0.00 sec)

A table is created using the Create as SELECT statement

Root:test> CREATE TABLE Ti2 as select * from TI limit 0;
Query OK, 0 rows Affected (0.00 sec)

sroot:test> how create table Ti2;
----------------------------
CREATE TABLE ' Ti2 ' (
' id ' int (one) DEFAULT NULL,
' Amount ' decimal (7,2) DEFAULT NULL,
' m_photo_big ' varchar DEFAULT NULL,
' Tr_date ' Date DEFAULT NULL,
' New_msg_flag ' tinyint (4) not NULL DEFAULT ' 0 ',
' Love_listreq ' int (3) DEFAULT ' 1 ',
' Love_listconfig ' int (3) DEFAULT ' 1 '
) Engine=innodb DEFAULT charset=latin1

Comparing the table structure of the source table, we found that the key ' New_msg_flag ' (' New_msg_flag ') was not created


b Create a table using the LIKE clause

Root:test> CREATE table ti1 like ti;
Query OK, 0 rows affected (0.06 sec)

Root:test> Show CREATE TABLE ti1;
----------------------------------------
CREATE TABLE ' Ti1 ' (
' id ' int (one) DEFAULT NULL,
' Amount ' decimal (7,2) DEFAULT NULL,
' m_photo_big ' varchar DEFAULT NULL,
' Tr_date ' Date DEFAULT NULL,
' New_msg_flag ' tinyint (4) not NULL DEFAULT ' 0 ',
' Love_listreq ' int (3) DEFAULT ' 1 ',
' Love_listconfig ' int (3) DEFAULT ' 1 ',
KEY ' New_msg_flag ' (' New_msg_flag ')
) Engine=innodb DEFAULT charset=latin1

Compare the table structure of the source table, the two are exactly the same, complete with table structure and index

Conclusion: The table created in MySQL under CREATE Table A as SELECT * from B does not contain index information, like clause form contains complete table structure and index information

So the AS SELECT clause generally applies to Yu Jianquo and copies the source table data, and the LIKE clause applies to situations where only the table structure is replicated

Risk of misuse: lack of index is fatal to the performance of the business, needless to say.

Oracle under:

A create as select also does not create an index

b Oracle does not support like clauses

How to achieve the full creation of the table structure and indexing method remains to be explored!

GAME over

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.