Adding rows
After this: Add rows with INSERT, let the database pick the id, and see a key refuse a copy.
On this page · 7 sections · 3 checks
A new student, Gus, joins the school today. Until now you have only asked the database questions: which students live in Leeds, whose mark is highest. Now the data itself has to change. Gus needs a row in the students table, and later he will need his courses and his marks.
This tutorial is about changing what a database holds: adding rows, changing them, removing them, and making sure a group of changes never stops halfway.
The table before
Here is the students table as every lesson starts it: six students, each with an id, a name, a year (1 to 3) and a city. Press Run to see it.
Each Run here builds a fresh copy of the school database, so nothing you change in one block carries over to the next block, or to the next Run of the same block.
Adding one row
To add a row, you say which table, which columns you are filling, and the values, in the same order. The SQL word for adding rows is . A is one complete instruction, ended by a semicolon.
Here is Gus, step by step:
INSERT INTO studentssays the new row goes in the students table.(id, name, year, city)lists the columns you are filling.VALUES (7, 'Gus', 1, 'Hull')gives one value for each of those columns, in the same order: id 7, name Gus, year 1, city Hull. Text goes in single quotes; numbers do not.
The block holds two statements, and they run in order. The list under the result shows what each one did: the INSERT says "1 row changed", and the SELECT after it shows seven students, with Gus at the end.
Naming the columns is a good habit. The values then match the names you wrote, not the order the columns happen to have in the table. And if a column is added to the table later, an INSERT that names its columns keeps working; one that does not is refused, because it now gives too few values.
Letting the database choose the id
You do not have to invent the id. If you leave the id column out of the list, SQLite fills it for you. For a column declared INTEGER PRIMARY KEY, as students.id is, it uses one more than the largest id in the table.
Add Hana after Gus, without an id:
- The largest id is now 7 (Gus).
- Hana's INSERT names only name, year and city.
- SQLite gives her 7 + 1 = 8.
Leaving other columns out
A column you leave out gets its default value. With no default written in the table, that is , the marker for "no value". (A column marked NOT NULL with no default cannot be left out: the INSERT is refused.) The enrolments table has one row per student per course, and its mark column may be NULL: a student who has started a course has no mark yet.
One INSERT can also add several rows: write one bracket of values for each row, with commas between them. Here Eve starts Art (course 4) and Music (course 5), with no marks:
The INSERT says "2 rows changed", and both new rows show NULL as the mark, written in grey.
When the table says no
The table has rules, written when it was made, and the database checks them on every change. Such a rule is a . Two of them protect the students table:
idis the , so no two rows may share an id.yearmust be between 1 and 3.
Try to add a second student with id 3, which Chloe already has:
The database refuses with UNIQUE constraint failed: students.id, and no row is added. Chloe keeps her row and there is no Zed. A year of 4 is refused the same way:
The message names the rule that was broken: CHECK constraint failed: year BETWEEN 1 AND 3. Lesson 2 shows that the same rules guard changes to rows that are already there.
Check yourself
On the fresh school database (six students, ids 1 to 6), Ivy is added without an id. What does the SELECT show?
INSERT INTO students (name, year, city)VALUES ('Ivy', 1, 'Bath');
SELECT id, name FROM studentsWHERE name = 'Ivy';Chloe already has id 3. What happens when this statement runs?
INSERT INTO students (id, name, year, city)VALUES (3, 'Zed', 1, 'York');Eve has no enrolments yet. One INSERT adds two rows for her, with no mark. What does the SELECT show?
INSERT INTO enrolments (student_id, course_id)VALUES (5, 1), (5, 2);
SELECT * FROM enrolmentsWHERE student_id = 5ORDER BY course_id;Honesty notes
- Add a row with INSERT INTO table (columns) VALUES (values), naming the columns and giving the values in the same order.
- Leave the id out and let SQLite fill an INTEGER PRIMARY KEY with one more than the largest id: Hana got 8 after Gus's 7.
- Add several rows in one INSERT with one bracket of values per row; a column left out with no default holds NULL.
- Read the message when a rule refuses a row: a copied id fails with UNIQUE constraint failed, a year of 4 with CHECK constraint failed.