SQLite Tutorial

  • Home
  • Start Here
  • Views
  • Indexes
  • Triggers
  • Functions
    • Aggregate Functions
    • Date Functions
    • String Functions
    • Window Functions
  • API
    • SQLite Python
    • SQLite Node.js
    • SQLite Java
    • SQLite PHP
  • Try It
Home / SQLite Tutorial / SQLite Insert

SQLite Insert

Summary: in this tutorial, you will learn how to use SQLite INSERT statement to insert new rows into a table.

To insert data into a table, you use the INSERT statement. SQLite provides various forms of the INSERT statements that allow you to insert a single row, multiple rows, and default values into a table.

In addition, you can insert a row into a table using data provided by a  SELECT statement.

SQLite INSERT – inserting a single row into a table

To insert a single row into a table, you use the following form of the INSERT statement:

INSERT INTO table (column1,column2 ,..) VALUES( value1, value2 ,...);

Let’s examine the INSERT statement in more detail:

  • First, specify the name of the table to which you want to insert data after the INSERT INTO keywords.
  • Second, add a comma-separated list of columns after the table name. The column list is optional. However, it is a good practice to include the column list after the table name.
  • Third, add a comma-separated list of values after the VALUES keyword. If you omit the column list, you have to specify values for all columns in the value list. The number of values in the value list must be the same as the number of columns in the column list.

We will use the artists table in the sample database for the demonstration.

The following statement insert a new row into the artists table:

INSERT INTO artists (name) VALUES('Bud Powell');

Try It

Because the ArtistId column is an auto-increment column, you can ignore it in the statement. SQLite automatically geneate a sequential integer number to insert into the ArtistId column.

You can verify the insert operation by using the following SELECT statement:

SELECT ArtistId, Name FROM Artists ORDER BY ArtistId DESC LIMIT 1;

Try It

SQLite Insert Example

As you see, we have a new row in the artists table.

SQLite INSERT – Inserting multiple rows into a table

To insert multiple rows into a table, you use the following form of the INSERT statement:

INSERT INTO table1 (column1,column2 ,..) VALUES (value1,value2 ,...), (value1,value2 ,...), ... (value1,value2 ,...);

Each value list following the VALUES clause is a row that will be inserted into the table.

The following example inserts three rows into the artists table:

INSERT INTO artists (name) VALUES ("Buddy Rich"), ("Candido"), ("Charlie Byrd");

Try It

SQLite issued a message:

Row Affected: 3

You can verify the result using the following statement:

SELECT ArtistId, Name FROM artists ORDER BY ArtistId DESC LIMIT 3;

Try It

SQLite Insert Multiple Example

SQLite INSERT – Inserting default values

When you create a new table using the CREATE TABLE statement, you can specify default values for columns, or a NULL if a default value is not specified.

The third form of the INSERT statement is INSERT DEFAULT VALUES, which inserts a new row into a table using the default values specified in the column definition or NULL if the default value is not available and the column does not have a NOT NULL constraint.

For example, the following statement inserts a new row into the artists table using INSERT DEFAULT VALUES:

INSERT INTO artists DEFAULT VALUES;

Try It

To verify the insert, you use the following statement:

SELECT ArtistId, Name FROM artists ORDER BY ArtistId DESC;

Try It

SQLite Insert default values

The default value of the ArtistId column is the next sequential integer . However, the name column does not have any default value, therefore, the INSERT DEFAULT VALUES statement inserts NULL  into it.

SQLite INSERT – Inserting new rows with data provided by a SELECT statement

Suppose you want to backup the artists table, you can follow these steps:

First, create a new table named artists_backup as follows:

CREATE TABLE artists_backup( ArtistId INTEGER PRIMARY KEY AUTOINCREMENT, Name NVARCHAR );

Try It

To insert data into the artists_backup table with the data from the artists table, you use the INSERT INTO SELECT statement as follows:

INSERT INTO artists_backup SELECT ArtistId, Name FROM artists;

Try It

If you query data from the artists_backup table, you will see all data in the artists table.

SELECT * FROM artists_backup;

Try It

SQLite Insert Into Select

In this tutorial, you have learned how to use various forms of SQLite INSERT statement that insert new rows into a table.

References

  • https://www.sqlite.org/lang_insert.html – SQLite INSERT statement
  • Was this tutorial helpful ?
  • YesNo
Previous SQLite CASE
Next SQLite Update

Getting Started

  • What Is SQLite
  • Download & Install SQLite
  • SQLite Sample Database
  • SQLite Commands

SQLite Tutorial

  • SQLite Select
  • SQLite Order By
  • SQLite Select Distinct
  • SQLite Where
  • SQLite Limit
  • SQLite BETWEEN
  • SQLite IN
  • SQLite Like
  • SQLite IS NULL
  • SQLite GLOB
  • SQLite Join
  • SQLite Inner Join
  • SQLite Left Join
  • SQLite Cross Join
  • SQLite Self-Join
  • SQLite Full Outer Join
  • SQLite Group By
  • SQLite Having
  • SQLite Union
  • SQLite Except
  • SQLite Intersect
  • SQLite Subquery
  • SQLite EXISTS
  • SQLite Case
  • SQLite Insert
  • SQLite Update
  • SQLite Delete
  • SQLite Replace
  • SQLite Transaction

SQLite Data Definition

  • SQLite Data Types
  • SQLite Date & Time
  • SQLite Create Table
  • SQLite Primary Key
  • SQLite Foreign Key
  • SQLite NOT NULL Constraint
  • SQLite UNIQUE Constraint
  • SQLite CHECK Constraint
  • SQLite AUTOINCREMENT
  • SQLite Alter Table
  • SQLite Rename Column
  • SQLite Drop Table
  • SQLite Create View
  • SQLite Drop View
  • SQLite Index
  • SQLite Expression-based Index
  • SQLite Trigger
  • SQLite VACUUM
  • SQLite Transaction
  • SQLite Full-text Search

SQLite Tools

  • SQLite Commands
  • SQLite Show Tables
  • SQLite Describe Table
  • SQLite Dump
  • SQLite Import CSV
  • SQLite Export CSV

SQLite Functions

  • SQLite AVG
  • SQLite COUNT
  • SQLite MAX
  • SQLite MIN
  • SQLite SUM

SQLite Interfaces

  • SQLite PHP
  • SQLite Node.js
  • SQLite Java
  • SQLite Python

About SQLite Tutorial

SQLite Tutorial website helps you master SQLite quickly and easily. It explains the complex concepts in simple and easy-to-understand ways so that you can both understand SQLite fast and know how to apply it in your software development work more effectively.

Looking for a tutorial…

If you did not find the tutorial that you are looking for, you can use the following search box. In case the tutorial is not available, you can request for it using the request for a SQLite tutorial form.

Recent Tutorials

  • SQLite IIF
  • SQLite Generated Columns
  • SQLite Getting Started
  • SQLite Programming Interfaces
  • SQLite Concat
  • SQLite INSTEAD OF Triggers
  • SQLite Join
  • SQLite IS NULL

Site Links

  • Home
  • About
  • Contact
  • Resources
  • Privacy Policy

Copyright © 2020 SQLite Tutorial. All Rights Reserved.