INSERT

Adding rows with INSERT

SQLite in your browser
Lesson concept What you'll do
1 / 4

Add new books to the table.

INSERT INTO the table, list the columns, then give the VALUES in the same order.

The database
8 rows
book_idtitleauthorgenreyearpages
1The ShiningStephen KingHorror1977447
2The Two TowersJ.R.R. TolkienFantasy1954352
3Charlie and the Chocolate FactoryRoald DahlChildren1964155
4The Girl with the Dragon TattooStieg LarssonThriller2005465
5The Lion, the Witch and the WardrobeC.S. LewisFantasy1950172
6The AlchemistPaulo CoelhoFiction1988197
7Angels & DemonsDan BrownThriller2000616
8And Then There Were NoneAgatha ChristieMystery1939264
Click a name to type it
Your query
 
Hint: click the green edit button on the board, or type it yourself
books after your query Ready to run

Run your query to see the result table.

Press the green Run button or Ctrl+Enter.

Study notes

How INSERT works

INSERT adds a new row at the end. Nothing else changes.

INSERT INTO books (book_id, title, genre, year)
VALUES (5, 'Matilda', 'Children', 1988);

books before

book_idtitlegenreyear
1FrankensteinHorror1818
2EmmaRomance1815
3DuneScience Fiction1965
4CarrieHorror1974

books after (1 row added)

book_idtitlegenreyear
1FrankensteinHorror1818
2EmmaRomance1815
3DuneScience Fiction1965
4CarrieHorror1974
5MatildaChildren1988
  • How do I add a row?

    INSERT INTO books (title, author) VALUES ('Dune', 'Frank Herbert');

    List the columns, then one value for each, in the same order.

  • Do I have to list the columns?

    No, but then you must give a value for every column, in the table's order. Listing them is safer: it still works if someone adds a column later.

  • What about columns I leave out?

    They get NULL (or a default the table sets). Here, leaving out book_id lets SQLite pick the next free number.

  • What if the text has an apostrophe?

    Write it twice: 'Charlotte''s Web'. Two single quotes inside the text stand for one.

  • How is this like Excel?

    Like typing a new row at the bottom of a table. Here nothing is saved: every run starts again from the original table.

Other databases

How other databases do it

The lessons run on SQLite. Most of SQL is the same everywhere; these are the places where this lesson's SQL differs.

Several rows in one INSERT: VALUES (...), (...)
DatabaseHowNote
SQLite (these lessons)YesSince SQLite 3.7.11 (2012).
PostgreSQLYes
MySQL / MariaDBYes
SQL ServerYesSince SQL Server 2008, up to 1,000 rows at a time.
OracleSince 23aiBefore that: one INSERT per row, or INSERT ALL.
Sybase ASENoOne INSERT per row.
Letting the database pick the next id
DatabaseHowNote
SQLite (these lessons)book_id INTEGER PRIMARY KEYLeave book_id out of the INSERT and SQLite uses the next number.
PostgreSQLbook_id INT GENERATED ALWAYS AS IDENTITYOlder code uses SERIAL.
MySQL / MariaDBbook_id INT AUTO_INCREMENT PRIMARY KEY
SQL Serverbook_id INT IDENTITY(1,1) PRIMARY KEY
Oraclebook_id NUMBER GENERATED AS IDENTITYSince Oracle 12c. Before that, a sequence.
Sybase ASEbook_id NUMERIC(10,0) IDENTITY

Know someone fighting with a spreadsheet? Send them this page.