INSERT
Adds one or more rows to a table. Rows can be specified as literal values, copied from another query, or generated from column defaults.Syntax
Parameters
| Parameter | Type | Description |
|---|---|---|
conflict_action | keyword | One of REPLACE, IGNORE, ABORT, ROLLBACK, or FAIL. Controls behavior on constraint violations |
table_name | identifier | The table to insert rows into |
column_name | identifier | Column to assign a value to. Unlisted columns receive their default value or NULL |
expression | expression | A value to insert. Must match the position or name of the target column |
select_statement | SELECT query | A query whose result rows are inserted into the table |
result_column | expression | Column or expression to return for each inserted row |
Description
INSERT adds new rows to the specified table. There are three forms:- VALUES: Insert one or more rows with explicit values.
- SELECT: Insert the result set of a query.
- DEFAULT VALUES: Insert a single row where every column uses its default value (or NULL if no default is defined).
Inserting Rows
Single Row
Multiple Rows
Supply multiple parenthesized groups separated by commas.All Columns
When the column list is omitted, provide a value for every column in table-definition order.INSERT INTO … SELECT
Inserts the result of a SELECT statement. The number of columns returned by the SELECT must match the number of target columns.DEFAULT VALUES
Inserts a single row where every column receives its default value. Columns without an explicit DEFAULT definition receive NULL. INTEGER PRIMARY KEY columns receive an auto-generated rowid.Column Default Values
When a column list is provided, omitted columns use their default values.Conflict Handling
TheOR clause specifies what happens when an INSERT violates a uniqueness or NOT NULL constraint.
| Action | Behavior |
|---|---|
ABORT | Roll back the current statement and return an error. Previously inserted rows within the same INSERT are undone. This is the default |
ROLLBACK | Roll back the entire transaction and return an error |
FAIL | Stop execution and return an error. Rows already inserted by this statement before the failure are kept |
IGNORE | Skip the row that caused the violation and continue with the next row |
REPLACE | Delete the conflicting row and insert the new row in its place |
For more granular conflict handling based on specific constraints, use the ON CONFLICT clause (UPSERT), which allows different actions depending on which constraint was violated.
RETURNING Clause
Returns data from the rows that were actually inserted. This is useful for retrieving generated values such as auto-incremented IDs or evaluated defaults.RETURNING with Multiple Rows
RETURNING *
Return all columns of the inserted rows.Examples
Insert with Subquery Values
Insert with Conflict Handling and RETURNING
Archival Pattern
See Also
- INSERT … ON CONFLICT (UPSERT) for conditional insert-or-update behavior
- REPLACE for shorthand INSERT OR REPLACE
- SELECT for the query syntax used with INSERT INTO … SELECT
- CREATE TABLE for column definitions and default values