SQLite Tutorial

  • Home
  • Views
  • Indexes
  • Triggers
  • Functions
    • Aggregate Functions
    • Date Functions
    • String Functions
    • Window Functions
  • Interfaces
    • SQLite Java
    • SQLite Node.js
    • SQLite PHP
    • SQLite Python
  • Try It
Home / SQLite Tutorial / SQLite UNIQUE Constraint

SQLite UNIQUE Constraint

Summary: in this tutorial, you will learn how to use the SQLite UNIQUE constraint to ensure all values in a column or a group of columns are unique.

Introduction to SQLite UNIQUE constraint

A UNIQUE constraint ensures all values in a column or a group of columns are distinct from one another or unique.

To define a UNIQUE constraint, you use the UNIQUE keyword followed by one or more columns.

You can define a UNIQUE constraint at the column or the table level. Only at the table level, you can define a UNIQUE constraint across multiple columns.

The following shows how to define a UNIQUE constraint for a column at the column level:

1
2
3
4
5
CREATE TABLE table_name(
    ...,
    column_name type UNIQUE,
    ...
);

Or at the table level:

1
2
3
4
CREATE TABLE table_name(
    ...,
    UNIQUE(column_name)
);

The following illustrates how to define a UNIQUE constraint for multiple columns:

1
2
3
4
CREATE TABLE table_name(
    ...,
    UNIQUE(column_name1,column_name2,...)
);

Once a UNIQUE constraint is defined, if you attempt to insert or update a value that already exists in the column, SQLite will issue an error and abort the operation.

SQLite UNIQUE constraint examples

Let’s take some examples of using the UNIQUE constraint.

Defining a UNIQUE constraint for one column example

The following statement creates a new table named contacts with a UNIQUE constraint defined for the email column:

1
2
3
4
5
6
CREATE TABLE contacts(
    contact_id INTEGER PRIMARY KEY,
    first_name TEXT,
    last_name TEXT,
    email TEXT NOT NULL UNIQUE
);

The following example inserts a new row into the contacts table:

1
2
INSERT INTO contacts(first_name,last_name,email)
VALUES ('John','Doe','john.doe@gmail.com');

If you attempt to insert a new contact with the same email, you will get an error message:

1
2
INSERT INTO contacts(first_name,last_name,email)
VALUES ('Johnny','Doe','john.doe@gmail.com');

Here is the error message:

1
Error while executing SQL query on database 'chinook': UNIQUE constraint failed: contacts.email

Defining a UNIQUE constraint for multiple columns example

The following statement creates the shapes table with a UNIQUE constraint defined for the background_color and foreground_color columns:

1
2
3
4
5
6
CREATE TABLE shapes(
    shape_id INTEGER PRIMARY KEY,
    background_color TEXT,
    foreground_color TEXT,
    UNIQUE(background_color,foreground_color)
);

The following statement inserts a new row into the shapes table:

1
2
INSERT INTO shapes(background_color,foreground_color)
VALUES('red','green');

The following statement works because of no duplication violation in both background_color and foreground_color columns:

1
2
INSERT INTO shapes(background_color,foreground_color)
VALUES('red','blue');

However, the following statement causes an error due to the duplicates in both background_color and foreground_color columns:

1
2
INSERT INTO shapes(background_color,foreground_color)
VALUES('red','green');

Here is the error:

1
Error while executing SQL query on database 'chinook': `UNIQUE` constraint failed: shapes.background_color, shapes.foreground_color

SQLite UNIQUE constraint and NULL

SQLite treats all NULL values are different, therefore, a column with a UNIQUE constraint can have multiple NULL values.

The following statement creates a new table named lists whose email column has a UNIQUE constraint:

1
2
3
4
CREATE TABLE lists(
    list_id INTEGER PRIMARY KEY,
    email TEXT UNIQUE
);

The following statement inserts multiple NULL values into the email column of the lists table:

1
2
INSERT INTO lists(email)
VALUES(NULL),(NULL);

Let’s query data from the lists table:

1
SELECT * FROM lists;

Here is the output:

SQLite UNIQUE Constraint Example

As you can see, even though the email column has a UNIQUE constraint, it can accept multiple NULL values.

In this tutorial, you have learned how to use the SQLite UNIQUE constraint to ensure all values in a column or a group of columns are unique.

  • Was this tutorial helpful ?
  • YesNo
Previous Tutorial: SQLite NOT NULL Constraint
Next Tutorial: SQLite CHECK Constraint

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 Concat
  • SQLite INSTEAD OF Triggers
  • SQLite Join
  • SQLite IS NULL
  • SQLite Rename Column
  • SQLite DROP VIEW
  • SQLite Window Frame
  • SQLite CUME_DIST

Site Links

  • Home
  • About
  • Contact
  • Resources
  • Privacy Policy

Copyright © 2020 SQLite Tutorial. All rights Reserved.

⤒