Inserting Data — SQL

INSERT INTO table (column1, column2) VALUES (value1, value2); adds one row to a table. The first parentheses hold the list of columns you fill, the…

The class list from the last lesson has its lines drawn, but it is still empty. Now the class teacher records the first student: Dewi, grade 90, from London. She writes it in a new row, each entry in its own column. The number continues from the last one.

In a database, the command that records a new row is INSERT INTO. You name the columns you want to fill (the column list), then what goes into each of those columns, in the same order. These are called the values. The number, which is the id column, is filled in by the database itself. Every command that succeeds adds rows to the table.

Two mistakes are the most common with this command: text that is missing its quotes, and values in a different order from the columns.

The shape of INSERT INTO

The command below adds one row to the students table from the Creating Tables lesson, which is still empty:

INSERT INTO students (name, grade, city) VALUES ('Dewi', 90, 'London');

The students table now holds:

id name grade city
1 Dewi 90 London

If you run the same command one more time, a second Dewi row is added with id 2. INSERT always adds a new row. It never replaces a row that is already there.

Many rows at once

One INSERT can add many rows. Each row goes inside its own parentheses, separated from the next row by a comma. There is only one semicolon (;), at the end of the command:

INSERT INTO students (name, grade, city) VALUES
  ('Dewi', 90, 'London'),
  ('Rizky', 85, 'Manchester'),
  ('Maya', 92, 'Leeds');

On an empty table, the result is 3 rows:

id name grade city
1 Dewi 90 London
2 Rizky 85 Manchester
3 Maya 92 Leeds

One command works for all of its rows, or for none. For example, if Leeds has no quotes, SQLite reads it as a column name. SQLite answers no such column: Leeds, and not one row goes in.

The values must follow the order of the columns

The database matches values to columns by their position, not by their meaning: the first value goes into the first column, the second value into the second column. Guess first: in the command INSERT INTO students (name, grade, city) VALUES (90, 'Dewi', 'London');, 90 and 'Dewi' have changed places. Does SQLite refuse the command, or accept it?

SQLite accepts it, with no error:

id name grade city
1 90 Dewi London

Now name holds 90 and grade holds Dewi. The database only follows the order, so the command succeeds but the data is wrong. MySQL (with its default settings) and PostgreSQL refuse the text 'Dewi' in a number column, but SQLite is not strict about types. So check the order yourself.

With no column list

You may leave out the column list. But then you must give a value for every column, in the order of the table's columns, id included. The command below gives only three values:

INSERT INTO students VALUES ('Dewi', 90, 'London');

SQLite refuses it, because the students table has four columns:

Error: table students has 4 columns but 3 values were supplied

The command that works gives NULL for id, so the database still fills it in: INSERT INTO students VALUES (NULL, 'Dewi', 90, 'London');. It is safer to always write the column list.

One more common mistake: VALUES (name = 'Dewi', grade = 90). VALUES does not use an equals sign. SQLite reads name = 'Dewi' as a comparison (is name equal to 'Dewi'?). Inside VALUES, a column name cannot be read, so SQLite answers no such column: name. Column names go in the column list, the values in VALUES.

⚠️ Wrong quotes, missing values

TL;DR: INSERT INTO table (column1, column2) VALUES (value1, value2); adds one row to a table. The first parentheses hold the list of columns you fill, the parentheses after VALUES hold the values (what goes into each column). Values are matched to columns by position, so both must be in the same order. Text goes inside single quotes ('Dewi'), numbers have no quotes. Many rows at once are written VALUES (...), (...), (...);, and if even one value causes an error, no row goes in. An id column with AUTOINCREMENT needs no value. With no column list, every column needs a value. Inside VALUES, write only the values, with no column names and no equals sign.

⚡ Add one row

Write:

INSERT INTO students (name, grade, city) VALUES ('Dewi', 90, 'London');

Meaning: INSERT INTO students adds one row to the students table. The first parentheses hold the list of columns you fill. After VALUES come the values (what goes into each column), in the same order: 'Dewi' goes into name, 90 into grade, 'London' into city. Text goes inside single quotes, numbers have no quotes. id is not mentioned, because the database fills it.

⚡ Many rows at once

Write:

INSERT INTO students (name, grade, city) VALUES
  ('Dewi', 90, 'London'),
  ('Rizky', 85, 'Manchester'),
  ('Maya', 92, 'Leeds');

Meaning: One INSERT can add many rows. Each row goes inside its own parentheses, separated by commas, with only one semicolon (;), at the end of the command. If even one value causes an error (for example, text with no quotes), not one row goes in.

⚡ Swapped values

Guess first: the column list is still (name, grade, city), but the values are written VALUES (90, 'Dewi', 'London'): the number and the name have changed places. Does SQLite refuse it?

No. SQLite accepts it with no error. The name column holds 90 and the grade column holds Dewi, because values are matched to columns by position, not by meaning.

Meaning: Check yourself that the values are in the same order as the columns. SQLite is not strict about types (text in a number column is still accepted), so it does not report a mistake like this one.

⚡ VALUES has no equals sign

Meaning: VALUES does not use an equals sign. VALUES (name = 'Dewi', grade = 90) is refused with the error no such column: name, because a column name cannot be read inside VALUES. Column names go in the column list, and VALUES holds only the values.

What you will learn