Insert Multiple Rows
Adding rows one at a time works, but batching rows in a single INSERT is cleaner and often faster.
Core Concepts
flowchart TD
A[One INSERT statement] --> B[Multiple VALUES tuples]
B --> C[Many rows inserted at once]
| Approach | Example | Typical use |
|---|---|---|
| Single-row insert | One VALUES (...) tuple | Interactive edits |
| Multi-row insert | VALUES (...), (...), (...) | Seed data, batch setup |
Code Examples
-- Setup table for batch insert demo.
CREATE TABLE tags (
tag_id INTEGER PRIMARY KEY,
label TEXT NOT NULL UNIQUE
);
-- Insert multiple rows in one statement.
INSERT INTO tags (label)
VALUES
('sqlite'),
('database'),
('sql');
| Expected output |
|---|
| Table created and three rows inserted. |
-- Confirm inserted data and count.
SELECT count(*) AS row_count FROM tags;
SELECT tag_id, label FROM tags ORDER BY tag_id;
| row_count |
|---|
| 3 |
| tag_id | label |
|---|---|
| 1 | sqlite |
| 2 | database |
| 3 | sql |
SQLite-Specific Nuances
SQLite Nuance
SQLite supports multi-row VALUES syntax, which is ideal for small-to-medium seed datasets.
Common Pitfalls / Best Practices
Pitfall
Building giant insert statements without validating data first.
A single malformed value can fail the whole statement.
Best Practice
Batch related rows together and verify with count(*) immediately after insert.
Quick Challenge
Insert two more tags: performance and indexing.
View Solution
INSERT INTO tags (label)
VALUES ('performance'), ('indexing');
| Expected output |
|---|
Two additional rows inserted into tags. |