Impala 简明教程
Impala - Insert Statement
Impala 的 INSERT 语句有两个子句: into 和 overwrite 。带有 into 子句的插入语句用于向数据库中现有的表中添加新记录。
The INSERT Statement of Impala has two clauses − into and overwrite. Insert statement with into clause is used to add new records into an existing table in a database.
Syntax
INSERT 语句有两个基本语法,如下所示:
There are two basic syntaxes of INSERT statement as follows −
insert into table_name (column1, column2, column3,...columnN)
values (value1, value2, value3,...valueN);
这里,column1、column2、…columnN 是想要在其中插入数据表的列的名称。
Here, column1, column2,…columnN are the names of the columns in the table into which you want to insert data.
您还可以添加值而不指定列名,但为此,您需要确保值的顺序与表中的列顺序相同,如下所示。
You can also add values without specifying the column names but, for that you need to make sure the order of the values is in the same order as the columns in the table as shown below.
Insert into table_name values (value1, value2, value2);
CREATE TABLE 是告诉数据库系统创建新表的关键字。表中的唯一名称或标识符遵循 CREATE TABLE 语句。您还可以选择指定 database_name 以及 table_name 。
CREATE TABLE is the keyword telling the database system to create a new table. The unique name or identifier for the table follows the CREATE TABLE statement. Optionally you can specify database_name along with the table_name.
Example
假设我们在 Impala 中创建了一个名为 student 的表,如下所示。
Suppose we have created a table named student in Impala as shown below.
create table employee (Id INT, name STRING, age INT,address STRING, salary BIGINT);
下面是一个在名为 employee 的表中创建记录的示例。
Following is an example of creating a record in the table named employee.
[quickstart.cloudera:21000] > insert into employee
(ID,NAME,AGE,ADDRESS,SALARY)VALUES (1, 'Ramesh', 32, 'Ahmedabad', 20000 );
执行上述语句后,一条记录被插入到名为 employee 的表中,并显示以下消息。
On executing the above statement, a record is inserted into the table named employee displaying the following message.
Query: insert into employee (ID,NAME,AGE,ADDRESS,SALARY) VALUES (1, 'Ramesh',
32, 'Ahmedabad', 20000 )
Inserted 1 row(s) in 1.32s
您可以插入另一条记录而不指定列名,如下所示。
You can insert another record without specifying the column names as shown below.
[quickstart.cloudera:21000] > insert into employee values (2, 'Khilan', 25,
'Delhi', 15000 );
执行上述语句后,一条记录被插入到名为 employee 的表中,并显示以下消息。
On executing the above statement, a record is inserted into the table named employee displaying the following message.
Query: insert into employee values (2, 'Khilan', 25, 'Delhi', 15000 )
Inserted 1 row(s) in 0.31s
您可以在 employee 表中插入更多记录,如下所示。
You can insert a few more records in the employee table as shown below.
Insert into employee values (3, 'kaushik', 23, 'Kota', 30000 );
Insert into employee values (4, 'Chaitali', 25, 'Mumbai', 35000 );
Insert into employee values (5, 'Hardik', 27, 'Bhopal', 40000 );
Insert into employee values (6, 'Komal', 22, 'MP', 32000 );
插入值后,Impala 中的 employee 表将如下所示。
After inserting the values, the employee table in Impala will be as shown below.
+----+----------+-----+-----------+--------+
| id | name | age | address | salary |
+----+----------+-----+-----------+--------+
| 1 | Ramesh | 32 | Ahmedabad | 20000 |
| 2 | Khilan | 25 | Delhi | 15000 |
| 5 | Hardik | 27 | Bhopal | 40000 |
| 4 | Chaitali | 25 | Mumbai | 35000 |
| 3 | kaushik | 23 | Kota | 30000 |
| 6 | Komal | 22 | MP | 32000 |
+----+----------+-----+-----------+--------+
Overwriting the Data in a Table
我们可以使用覆盖子句覆盖表的记录。覆盖的记录将从表中永久删除。以下是使用覆盖子句的语法。
We can overwrite the records of a table using overwrite clause. The overwritten records will be permanently deleted from the table. Following is the syntax of using the overwrite clause.
Insert overwrite table_name values (value1, value2, value2);
Example
以下是使用子句 overwrite 的一个示例。
Following is an example of using the clause overwrite.
[quickstart.cloudera:21000] > Insert overwrite employee values (1, 'Ram', 26,
'Vishakhapatnam', 37000 );
执行上述查询后,它会用指定的记录覆盖表数据,并显示以下消息。
On executing the above query, this will overwrite the table data with the specified record displaying the following message.
Query: insert overwrite employee values (1, 'Ram', 26, 'Vishakhapatnam', 37000 )
Inserted 1 row(s) in 0.31s
验证表格后,你可以观察到,表格 employee 的所有记录都被新记录覆盖,如下所示。
On verifying the table, you can observe that all the records of the table employee are overwritten by new records as shown below.
+----+------+-----+---------------+--------+
| id | name | age | address | salary |
+----+------+-----+---------------+--------+
| 1 | Ram | 26 | Vishakhapatnam| 37000 |
+----+------+-----+---------------+--------+
Inserting Data using Hue Browser
打开 Impala 查询编辑器并在其输入 insert 语句。然后点击执行按钮,如下所示。
Open Impala Query editor and type the insert Statement in it. And click on the execute button as shown in the following screenshot.

在执行查询/语句后,该记录会添加到表格中。
After executing the query/statement, this record is added to the table.