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');
✓ 1 row changed

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;
idnamecityphonejoined
11Preecha SomboonBangkok081-000-11112026-11-30
1 row

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');
✓ 1 row changed

That works, but give too few values and the database refuses:

INSERT INTO category VALUES ('Ice Cream');
Error: table category has 2 columns but 1 values were supplied

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);
✓ 3 rows changed
SELECT * FROM product WHERE id > 12;
idnamecategory_idpricestock
13Iced Cocoa150.040
14Sandwich265.015
15Eraser410.0100
3 rows

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';
idnamepricestock
16Ice Cream35.00
1 row

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';
idnamecityphonejoined
12Chanida RakdeeNULLNULL2026-12-01
1 row

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
1 row

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
1 row

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;
✓ 2 rows changed

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_idproduct_idqtyprice
161255.0
164235.0
2 rows

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);
Error: NOT NULL constraint failed: product.price

UNIQUE: no two rows may have the same value. Two categories can't share a name:

INSERT INTO category (name) VALUES ('Drinks');
Error: UNIQUE constraint failed: category.name

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');
Error: UNIQUE constraint failed: customer.id

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);
Error: FOREIGN KEY constraint failed

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');
Error: UNIQUE constraint failed: category.name
SELECT * FROM category;
idname
1Drinks
2Bakery
3Snacks
4Stationery
5Frozen
5 rows

There is no Toys: the whole statement was undone, not half of it. Each kind of rule has its own chapter (21 and 22).

  1. Add a category named 'Fruit' and see which id it got.
  2. Add the product 'Mango' in that category, at 30 baht, 50 in stock.
  3. 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
1 row
last_insert_rowid()
17
1 row
order_idproduct_idqtyprice
17101030.0
1711515.0
2 rows

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