INSERT¶
Updates a table by inserting one or more rows into the table. The values inserted into each column in the table can be explicitly-specified or the results of a query.
- See also:
Syntax¶
Required parameters¶
target_tableSpecifies the target table into which to insert rows.
VALUES ( value | DEFAULT | NULL [ , ... ] ) [ , ( ... ) ]Specifies one or more values to insert into the corresponding columns in the target table.
In a
VALUESclause, you can specify the following:value: Inserts the explicitly-specified value. The value can be a literal or an expression that evaluates to a single value.DEFAULT: Inserts the default value for the corresponding column in the target table.NULL: Inserts aNULLvalue.
Each value in the clause must be separated by a comma.
You can insert multiple rows by specifying additional sets of values in the clause. For more information, see the Usage notes and the Examples.
querySpecify a query statement that returns values to be inserted into the corresponding columns. This allows you to insert rows into a target table from one or more source tables.
Optional parameters¶
OVERWRITESpecifies that the target table should be truncated before inserting the values into the table. Note that specifying this option does not affect the access control privileges on the table.
INSERT statements with
OVERWRITEcan be processed within the scope of the current transaction, which avoids DDL statements that commit a transaction, such as:Default: No value (the target table is not truncated before performing the inserts).
( target_col_name [ , ... ] )Specifies one or more columns in the target table into which the corresponding values are inserted. The number of target columns specified must match the number of specified values or columns (if the values are the results of a query) in the
VALUESclause.Default: No value (all the columns in the target table are updated).
Usage notes¶
Using a single INSERT command, you can insert multiple rows into a table by specifying additional sets of values separated by commas in the VALUES clause.
For example, the following clause would insert 3 rows in a 3-column table, with values
1,2, and3in the first two rows and values2,3, and4in the third row:To use the OVERWRITE option on INSERT, you must use a role that has DELETE privilege on the table because OVERWRITE will delete the existing records in the table.
Some types of expressions can’t be specified in the VALUES clause, including the following expressions:
Subqueries
For example:
Values of the semi-structured or structured data type.
For example:
-
For example:
-
For example:
As an alternative to the VALUES clause, specify the expression in a
queryclause. For example, you can replace the following expression:with this expression:
The VALUES clause is limited to 200,000 rows. This limit applies to a single INSERT INTO … VALUES statement and a single INSERT INTO … SELECT … FROM VALUES statement. Consider using the COPY INTO <table> command to perform a bulk data load. For more information about using the VALUES clause in a SELECT statement, see VALUES.
For information about inserting data into hybrid tables, see Loading data.
Examples¶
The following examples use the INSERT command.
Single row insert using a query¶
Convert three string values to dates or timestamps and insert them into a single row in the mytable table:
Similar to previous example, but specify to update only the first and third columns in the table:
Multi-row insert using explicitly-specified values¶
Create the employees table and insert four rows of data into it by providing sets of values in a
comma-separated list in the VALUES clause:
In multi-row inserts, make sure that the data types of the inserted values are consistent across the rows because the data type of the first row is used as a guide. Create a table and insert two rows:
The first insert works as expected:
The second insert fails because the data type of the value in the second row ('d') is a string, which is
different from the numeric data type of the value in the first row (3). The insert fails even though both values
can be coerced to VARCHAR, which is the data type of the column in
the table. The insert fails even though the data type of the value 'd' is the same as the data type of column v:
When the data types are consistent across the rows, the insert succeeds, and both numeric values are coerced to the VARCHAR data type:
Multi-row insert using query¶
Insert multiple rows of data from the contractors table into the employees table:
Select only those rows where the
worknumcolumn contains area code650.Insert a NULL value in the
citycolumn.
Insert multiple rows of data from the contractors table into the employees table using a common table expression:
Insert columns from two tables (emp_addr, emp_ph) into a third table (emp) using an INNER JOIN on the id
column in the source tables:
Multi-row insert for JSON data¶
Insert two JSON objects into a VARIANT column in a table:
Insert using OVERWRITE¶
This example uses INSERT with OVERWRITE to rebuild the sf_employees table from employees after new records were added
to the employees table.
Here is the initial data for both tables:
This statement inserts rows into the sf_employees table using the OVERWRITE clause:
Because the INSERT used the OVERWRITE clause, the old rows from sf_employees are gone:
Write to a v3 Apache Iceberg™ table¶
The following example inserts a row into an Apache Iceberg™ table that conforms to v3 of the Apache Iceberg™ table specification: