insert into select usage

This article mainly explains the usage of insert into select, as well as the pitfalls or precautions of insert into select. The sql in this article is explained based on mysql8.0

1. insert into select

This syntax is often used to query data from another table and insert it into a table. For example, when backing up a table, migrate the data in the table to the backup table.

The following test insert into select is used to backup the table

create table if not exists user
(
	id int not null primary key,
	sex char(3) null,
	NAME char(20) null
);
INSERT INTO user VALUES 
(1,'nan','陈一'),
(2,'nv','珠二'),
(3,'nv','张三'),
(4,'nan','李四'),
(5,'nv','王五'),
(6,'nan','赵六');

Insert image description here
backup table sql

-- 创建bak备份表
create table user_bak_20230731 like user;
-- 将user表的数据插入到备份表
insert into user_bak_20230731 select * from user;

The backup table data is as follows. You can see that it is consistent with the content of the user's original table.
Insert image description here

注意:

  1. If insert into select inserts too much data at one time, it is recommended to insert in batches to avoid IO exceptions or insufficient cache.

2. Insert into select failed to insert

Failure scenario: In the hive database, an error occurs when directly executing insert into select on the partitioned table. ( 其他类型数据库针对分区表直接执行insert into select没发现这个问题,mysql数据库亲测是ok的)
Error log:

FAILED: SemanticException 1:23 Need to specify partition columns because the destination table is partitioned. Error encountered near token 'user_tmp'

Solution:
You need to specify the value of the partition field in the inserted data.

For example:
table creation statement

create table test1 (
starttime string,
endtime string,
title string
)
PARTITIONED BY (username string)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY '| '
STORED AS TEXTFILE;

-- 创建另一张表
create table test2 like test1;

Insert statement:

insert into table test2 PARTITION(username='admin') select starttime, endtime, title from test1 where username = 'admin';

Guess you like

Origin blog.csdn.net/weixin_49114503/article/details/132031100