It turns out that you can also create a table like this

Zhang Gong is a java programmer. Recently, Mr. Zhang went to an Internet company to interview for a Java development position. The interviewer looked at Zhang Gong's resume and was proficient in databases, so he asked him.

What is the difference between create table as and create table like?

Mr. Zhang couldn't answer, because Mr. Zhang always used the method of create table to build a table. The interviewer mentioned the method of building a table that Mr. Zhang did not touch, let alone use it. Naturally, I can't tell why.

Interviewer: I don't have a good grasp of MySql grammar. How do you write your resume and be proficient in databases. When the interviewer said so, Mr. Zhang felt a little embarrassed.

Let's look at the difference between create table, create table as and create table like in the three table building methods.

For example, we want to create a short message template table

1、create table

CREATE TABLE `sms_template` (
  `sms_template_id` bigint(20) NOT NULL COMMENT '短信模板ID',
  `tenant_id` bigint(20) NOT NULL COMMENT '分销商ID',
  `title` varchar(20) NOT NULL COMMENT '短信模板标题',
  `content` varchar(255) NOT NULL COMMENT '短信模板内容',
  `sms_type_id` bigint(20) NOT NULL COMMENT '触发类型ID',
  `sms_type` tinyint(4) NOT NULL COMMENT '短信模板类型:1-系统短息;2-自建短息',
  `company` varchar(50) NOT NULL COMMENT '公司名称',
  `is_deleted` tinyint(4) NOT NULL DEFAULT '0' COMMENT '是否逻辑删除:0-未删除,1-已删除',
  `is_receiver` tinyint(4) NOT NULL DEFAULT '0' COMMENT '是否允许添加手机号:0-不允许;1-允许',
  `is_charge` tinyint(4) NOT NULL DEFAULT '1' COMMENT '是否收费:1-收费;4-不收费',
  `create_time` datetime NOT NULL COMMENT '创建时间',
  `update_time` datetime NOT NULL COMMENT '修改时间',
  PRIMARY KEY (`sms_template_id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='短信模板主表';

Use create table to build the SMS template table. At work, we sometimes need to back up a table. Generally, create table as select (abbreviated: CTAS) is used. This method of table building is simple and convenient. It is necessary to pay attention to the problems caused by CTAS table building, because CTAS table building does not necessarily save the original table style.

2. Create table...as select.. (CTAS) lazy mode

Benefits of using CTAS to create tables

  • The results of the query are used in the CTAS statement to create and populate the table. Since the table created by CTAS is atomic, this means that we will not see the table until all query results are filled. Therefore, we see either the data with the complete query to the table, or the data in the table cannot be queried at all.

  • There are two parts in CTAS. The select part can be any select statement supported by HiveQL. The create part of CTAS obtains the result mode from the select part, and can use other table attributes such as SerDe and storage format to create the specified target table. For example, specify attributes such as row and column segmentation format.

In particular, it should be noted that there will be changes in the table structure when using CTAS to build tables, as well as the defects and limitations of using CTAS to build tables.

Next, let's take a look at the create table like method.

3. Create table like..... semi-automatic mode

The essence of create table like is to copy the existing table definition (do not copy its data). Except for the table name and the source table, all the details of the created table are the same, that is, there is no source table data.

for example

create table  sms_send_type_test like sms_send_type;

In this way, we have created a sms_send_type_test whose structure is consistent with sms_send_type.

Let's verify, first look at the table structure of sms_send_type_test

#查看目标表结构
show create table sms_send_type_testCREATE TABLE `sms_send_type_test` (
  `sms_type_id` bigint(20) NOT NULL COMMENT '触发类型ID',
  `send_type_name` varchar(50) NOT NULL COMMENT '短信模板触发类型描述',
  `is_receiver` tinyint(4) NOT NULL COMMENT '是否允许添加手机号:1-允许;4-不允许',
  `is_deleted` tinyint(2) NOT NULL COMMENT '是否逻辑删除:0-未删除,1-已删除',
  `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
  PRIMARY KEY (`sms_type_id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='短信模板触发类型表'
 

Look at the structure of the source table sms_send_type

#查看源表结构
show create table sms_send_typeCREATE TABLE `sms_send_type` (
  `sms_type_id` bigint(20) NOT NULL COMMENT '触发类型ID',
  `send_type_name` varchar(50) NOT NULL COMMENT '短信模板触发类型描述',
  `is_receiver` tinyint(4) NOT NULL COMMENT '是否允许添加手机号:1-允许;4-不允许',
  `is_deleted` tinyint(2) NOT NULL COMMENT '是否逻辑删除:0-未删除,1-已删除',
  `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
  PRIMARY KEY (`sms_type_id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='短信模板触发类型表'

It can be found that the structure of the two tables is the same.

to sum up

The difference between create table as and create table like

The same point: create a new table

Difference: create table as just copies the original data. In fact, it is to build a table with the results of the query. Create table like produces the same table structure as the source table, including indexes and primary keys. The data needs to be copied in with the insert into statement.

Due to the author's limited level, the deficiencies in the article are unavoidable, and I have the right to offer suggestions. Please criticize and correct any improprieties.

For technical exchanges, media cooperation, brand promotion, please add Xiaoai WeChat: iyiyouyou

Recommended in the past

Programmer: can withstand loneliness and temptation

More exciting, please scan the QR code to follow Xiao Ai

Guess you like

Origin blog.csdn.net/X8i0Bev/article/details/108557323