Data Changes
Insert Rows
Adding Data
INSERT adds new rows to an existing table.
Program
Play the script to watch the task list grow.
insert_rows.sql
Replay: real traced execution (multi-file project)
CREATE TABLE tasks (id INTEGER, title TEXT, done INTEGER);
INSERT INTO tasks VALUES (1, 'write', 0);
INSERT INTO tasks VALUES (2, 'review', 0);
SELECT id, title FROM tasks ORDER BY id;
tasks ← 0 rows
1CREATE TABLE tasks (id INTEGER, title TEXT, done INTEGER);2INSERT INTO tasks VALUES (1, 'write', 0);values this step0 rowstaskstasks ← 1 row
1CREATE TABLE tasks (id INTEGER, title TEXT, done INTEGER);2INSERT INTO tasks VALUES (1, 'write', 0);3INSERT INTO tasks VALUES (2, 'review', 0);values this step1 rowtaskstasks ← 2 rows
2INSERT INTO tasks VALUES (1, 'write', 0);3INSERT INTO tasks VALUES (2, 'review', 0);4SELECT id, title FROM tasks ORDER BY id;values this step2 rowstasksresult ← 2 rows
3INSERT INTO tasks VALUES (2, 'review', 0);4SELECT id, title FROM tasks ORDER BY id;values this step2 rowsresult
Follow the Inserts
- The first
INSERTadds task1, titlewrite, done0. - The second
INSERTadds task2, titlereview, done0. - The table now has two task rows.
- The final
SELECT id, titleshows only theidandtitlecolumns. | moment | visible rows | | --- | --- | | after first insert |(1, write, 0)| | after second insert |(1, write, 0),(2, review, 0)| | final select |(1, write),(2, review)|
mutation
A mutation changes stored table data.
row
Each `VALUES` tuple becomes one row.
read after write
A later `SELECT` sees earlier inserts.
Exercise: insert_rows.sql
Reproduce the final rows write and review, then point to the table state after each INSERT.