16. INSERT: adding rows
The shop gets new customers, new products and new orders every day. This chapter is how to add a row to a table.
One row with INSERT ... VALUES
So far we have only read data. From this chapter on we change it. The first statement is INSERT, which adds a new row: the table's name, the list of columns in brackets, then VALUES with a value for each column, in the same order:
INSERT INTO customer (id, name, city, phone, joined)
VALUES (11, 'Preecha Somboon', 'Bangkok', '081-000-1111', '2026-11-30');The database answers that 1 row changed: one customer added. Read it back to see it is really there:
SELECT * FROM customer WHERE id = 11;| id | name | city | phone | joined |
|---|---|---|---|---|
| 11 | Preecha Somboon | Bangkok | 081-000-1111 | 2026-11-30 |
Text values go between '...'; numbers are written as they are.
When a text has a ' inside it, write it twice: 'Chef''s Choice' is the text Chef's Choice.
Why to write the list of columns
Without a list of columns you must give a value for every column, in exactly the table's order:
INSERT INTO category VALUES (5, 'Frozen');That works, but give too few values and the database refuses:
INSERT INTO category VALUES ('Ice Cream');Always write the list of columns, even when you give every one. A statement without it is tied to the order of the table's columns: when the table gets a new column one day, the statement breaks, and when two values swap places by mistake (the price and the stock, say), the database quietly keeps each in the wrong column and nobody knows.
Several rows in one statement
Put several brackets after VALUES, separated by commas, and one statement adds several rows:
INSERT INTO product (name, category_id, price, stock) VALUES
('Iced Cocoa', 1, 50, 40),
('Sandwich', 2, 65, 15),
('Eraser', 4, 10, 100);SELECT * FROM product WHERE id > 12;| id | name | category_id | price | stock |
|---|---|---|---|---|
| 13 | Iced Cocoa | 1 | 50.0 | 40 |
| 14 | Sandwich | 2 | 65.0 | 15 |
| 15 | Eraser | 4 | 10.0 | 100 |
We gave no id at all, yet the new products got 13, 14 and 15, after the ones already there. The next sections tell where those numbers come from.
Columns left out: DEFAULT and NULL
A column left out of the list gets its default value. The product table says stock defaults to 0, so a product added without a stock has 0:
INSERT INTO product (name, category_id, price) VALUES ('Ice Cream', 5, 35);
SELECT id, name, price, stock FROM product WHERE name = 'Ice Cream';| id | name | price | stock |
|---|---|---|---|
| 16 | Ice Cream | 35.0 | 0 |
A column with no default gets NULL, like a customer whose city and phone we don't know yet:
INSERT INTO customer (name, joined) VALUES ('Chanida Rakdee', '2026-12-01');
SELECT * FROM customer WHERE name = 'Chanida Rakdee';| id | name | city | phone | joined |
|---|---|---|---|---|
| 12 | Chanida Rakdee | NULL | NULL | 2026-12-01 |
To get the default, leave the column out of the list (SQLite does not take the word DEFAULT as a value inside VALUES). Giving a column a default is in chapter 21.
The id the database gives
Every table of the shop has id INTEGER PRIMARY KEY. Such a column is special: give it no value and the database gives one, the largest number in the table plus one. That is why the customer just added got id 12.
After adding a row you often want its number, to use next. last_insert_rowid() gives the number of the last row added:
SELECT last_insert_rowid();| last_insert_rowid() |
|---|
| 12 |
A real case is opening a new order: add the order first, then ask which number it got:
INSERT INTO orders (customer_id, ordered, status) VALUES (1, '2026-12-01', 'paid');
SELECT last_insert_rowid();| last_insert_rowid() |
|---|
| 16 |
The new order is number 16. The next section uses it to add the order's lines.
last_insert_rowid() remembers only the last row that our own connection added: other people adding rows at the same time don't mix in. A Rust program using Phai SQL can read this number directly too.
INSERT ... SELECT: adding what a SELECT finds
Instead of typing the values, let a SELECT find them. Customer 1 wants exactly what was in order 1 again, so we copy order 1's lines into order 16:
INSERT INTO order_item (order_id, product_id, qty, price)
SELECT 16, product_id, qty, price
FROM order_item
WHERE order_id = 1;The SELECT finds 2 rows, so 2 rows are added. The SELECT's columns pair up with the list of columns in order (16 goes to order_id, and so on):
SELECT * FROM order_item WHERE order_id = 16;| order_id | product_id | qty | price |
|---|---|---|---|
| 16 | 1 | 2 | 55.0 |
| 16 | 4 | 2 | 35.0 |
The prices copied are the ones of the earlier sale. For today's prices, JOIN with product and pick product.price instead. The SELECT inside an INSERT can use everything you have learnt: WHERE, JOIN, GROUP BY.
When the database refuses a row
A table has rules (constraints) that keep wrong data out. When a new row breaks one, the database doesn't add it and says which rule it broke. Beginners meet four kinds most.
NOT NULL: the column must have a value. A product must have a price:
INSERT INTO product (name, category_id) VALUES ('Mystery Box', 3);UNIQUE: no two rows may have the same value. Two categories can't share a name:
INSERT INTO category (name) VALUES ('Drinks');PRIMARY KEY: the id can't repeat, because it is what tells the rows apart. Customer 1 is already there:
INSERT INTO customer (id, name) VALUES (1, 'Somsak Dee');The message says UNIQUE: a primary key is one kind of value that must not repeat.
FOREIGN KEY: a value that points into another table must be there. There is no category 99:
INSERT INTO product (name, category_id, price) VALUES ('Mystery Box', 99, 100);A statement that breaks a rule changes nothing at all, even when it adds several rows and only one is wrong. Here 'Toys' is fine but 'Drinks' repeats:
INSERT INTO category (name) VALUES ('Toys'), ('Drinks');SELECT * FROM category;| id | name |
|---|---|
| 1 | Drinks |
| 2 | Bakery |
| 3 | Snacks |
| 4 | Stationery |
| 5 | Frozen |
There is no Toys: the whole statement was undone, not half of it. Each kind of rule has its own chapter (21 and 22).
- Add a category named
'Fruit'and see whichidit got. - Add the product
'Mango'in that category, at 30 baht, 50 in stock. - Customer 7 orders the same as order 15 again, on
'2026-12-02', status'paid'. Add the new order and its lines.
Show the answer
INSERT INTO category (name) VALUES ('Fruit');
SELECT last_insert_rowid();
INSERT INTO product (name, category_id, price, stock) VALUES ('Mango', 6, 30, 50);
INSERT INTO orders (customer_id, ordered, status) VALUES (7, '2026-12-02', 'paid');
SELECT last_insert_rowid();
INSERT INTO order_item (order_id, product_id, qty, price)
SELECT 17, product_id, qty, price FROM order_item WHERE order_id = 15;
SELECT * FROM order_item WHERE order_id = 17;| last_insert_rowid() |
|---|
| 6 |
| last_insert_rowid() |
|---|
| 17 |
| order_id | product_id | qty | price |
|---|---|---|---|
| 17 | 10 | 10 | 30.0 |
| 17 | 11 | 5 | 15.0 |
Phai (bamboo), given as a gift of knowledge: free to use, learn from and pass on · every example on this page was run on Phai SQL and checked against SQLite when the page was made