Sub topic 4
Inserting Several Rows in One Statement
Assume you already know how tables, columns, rows, primary keys, and basic SELECT queries work. This chapter uses standard SQL syntax. Small differences exist between database systems.
The multi-row `INSERT` pattern
An INSERT statement can add one row or several rows. To add several rows, write one VALUES keyword followed by a comma-separated row for each new record.
Always list the target columns explicitly. The values in every row must appear in the same order as those columns.
CREATE TABLE customers (
id INTEGER PRIMARY KEY,
name VARCHAR(100) NOT NULL,
email VARCHAR(255) UNIQUE,
status VARCHAR(20) NOT NULL DEFAULT 'active'
);
INSERT INTO customers (id, name, email)
VALUES
(1, 'Ada Lovelace', '[email protected]'),
(2, 'Grace Hopper', '[email protected]'),
(3, 'Alan Turing', NULL);The statement inserts three rows. The id, name, and email values match the listed columns by position.
The status column is omitted. Because it has a default value, each new row receives 'active'.
NULL represents a missing or unknown value. It is different from an empty string such as ''. The email column allows NULL, so Alan's row is valid. The name column is NOT NULL, so every row must provide a name.
How the rows are processed
The database checks each row against the table's constraints. For this table:
- Each
idmust be different because it is a primary key. - Each
namemust have a value because it isNOT NULL. - Each non-NULL
emailmust be unique. - An omitted
statusreceives'active'.
If two rows use the same primary key, or if two non-NULL rows use the same unique email, the statement is rejected. For example, this conflicts with Ada's existing id and email:
INSERT INTO customers (id, name, email)
VALUES
(1, 'Another Person', '[email protected]'),
(4, 'Katherine Johnson', '[email protected]');Many database systems reject the statement as a whole when one row violates a constraint. Do not assume that some rows were stored unless you check the database's result and transaction behavior.
Choosing between separate and multi-row inserts
A multi-row insert is usually shorter and can reduce communication between your application and the database. It is useful when the rows belong to the same operation.
Use separate statements when rows need different logic, error handling, or permissions. For very large imports, a database's bulk-loading feature may be faster than one enormous INSERT.
| Method | Best for |
|---|---|
| Multi-row `INSERT` | A small or moderate batch of related rows |
| Separate `INSERT` statements | Rows requiring different decisions |
| Bulk-loading tool | Very large files or data imports |
Exercise
Write one statement that inserts customers with IDs 4 and 5, names 'Katherine Johnson' and 'Jim Gray', and emails '[email protected]' and '[email protected]'. Leave status omitted so its default is used.
Show answer
INSERT INTO customers (id, name, email) VALUES (4, 'Katherine Johnson', '[email protected]'), (5, 'Jim Gray', '[email protected]');