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 REPLACE Statement

SQLite REPLACE Statement

Summary: in this tutorial, you will learn how to use the SQLite REPLACE statement to insert or replace the existing row in a table.

Introduction to the SQLite REPLACE statement

The idea of the REPLACE statement is that when a UNIQUE or PRIMARY KEY constraint violation occurs, it does the following:

  • First, delete the existing row that causes a constraint violation.
  • Second, insert a new row.

In the second step, if any constraint violation e.g., NOT NULL constraint occurs, the REPLACE statement will abort the action and roll back the transaction.

The following illustrates the syntax of the REPLACE statement.

INSERT OR REPLACE INTO table(column_list) VALUES(value_list);
Code language: SQL (Structured Query Language) (sql)

Or in a short form:

REPLACE INTO table(column_list) VALUES(value_list);
Code language: SQL (Structured Query Language) (sql)

Let’s take a look at some examples of using the SQLite REPLACE statement to understand how it works.

The SQLite REPLACE statement examples

First, create a new table named positions with the following structure.

CREATE TABLE IF NOT EXISTS positions ( id INTEGER PRIMARY KEY, title TEXT NOT NULL, min_salary NUMERIC );
Code language: SQL (Structured Query Language) (sql)

Try It

Second, insert some rows into the positions table.

INSERT INTO positions (title, min_salary) VALUES ('DBA', 120000), ('Developer', 100000), ('Architect', 150000);
Code language: SQL (Structured Query Language) (sql)

Try It

Third, verify the insert using the following SELECT statement.

SELECT * FROM positions;
Code language: SQL (Structured Query Language) (sql)

Try It

SQLite REPLACE positions table

The following statement creates a unique index on the title column of the positions table to ensure that it doesn’t have any duplicate position title:

CREATE UNIQUE INDEX idx_positions_title ON positions (title);
Code language: SQL (Structured Query Language) (sql)

Try It

Suppose, you want to add a position into the positions table if it does not exist, in case the position exists, update the current one.

The following REPLACE statement inserts a new row into the positions table because the position title Full Stack Developer is not in the positions table.

REPLACE INTO positions (title, min_salary) VALUES('Full Stack Developer', 140000);
Code language: SQL (Structured Query Language) (sql)

Try It

SQLite REPLACE insert new row

You can verify the REPLACE operation using the SELECT statement.

SELECT id,title,min_salary FROM positions;
Code language: SQL (Structured Query Language) (sql)

Try It

See the following statement.

REPLACE INTO positions (title, min_salary) VALUES('DBA', 170000);
Code language: SQL (Structured Query Language) (sql)

Try It

SQLite REPLACE - replace the existing row

First, SQLite checked the UNIQUE constraint.

Second, because this statement violated the UNIQUE constraint by trying to add the DBA title that already exists, SQLite deleted the existing row.

Third, SQLite inserted a new row with the data provided by the REPLACE statement.

Notice that the REPLACE statement means INSERT or REPLACE, not INSERT or UPDATE.

See the following statement.

REPLACE INTO positions (id, min_salary) VALUES(2, 110000);
Code language: SQL (Structured Query Language) (sql)

Try It

What the statement tried to do is to update the min_salary for the position with id 2, which is the developer.

First, the position with id 2 already exists, the REPLACE statement removes it.

Then, SQLite tried to insert a new row with two columns: ( id, min_salary). However, it violates the NOT NULL constraint of the title column. Therefore, SQLite rolls back the transaction.

If the title column does not have the NOT NULL constraint, the REPLACE statement will insert a new row whose the title column is NULL.

In this tutorial, we have shown you how to use the SQLite REPLACE statement to insert or replace a row in a table.

  • Was this tutorial helpful ?
  • YesNo
Previous SQLite Delete
Next SQLite Transaction

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 constraints
  • 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 © 2021 SQLite Tutorial. All Rights Reserved.