SQLite Tutorial SQLite Advanced SQLite Database SQLite References

SQLite - UNIQUE



The SQLite UNIQUE constraint is used to ensure that all values in a column of a table are different (unique). To ensure uniqueness of values in a column either UNIQUE or PRIMARY KEY constraints can be used. A PRIMARY KEY constraint automatically has a UNIQUE constraint. However, a table can have only one PRIMARY KEY constraint, but many UNIQUE constraints.

SQLite UNIQUE constraint with CREATE TABLE

The below mentioned statement creates a table called Employee which contains five columns: EmpID, Name, City, Age and Salary in which UNIQUE constraint is applied on column EmpID.

CREATE TABLE Employee (
  EmpID INT NOT NULL UNIQUE,
  Name VARCHAR(255) NOT NULL,
  City VARCHAR(100),
  Age INT,
  Salary DECIMAL(18,2)
);

Alternatively, it can also be created as mentioned below:

CREATE TABLE Employee (
  EmpID INT NOT NULL,
  Name VARCHAR(255) NOT NULL,
  City VARCHAR(100),
  Age INT,
  Salary DECIMAL(18,2),
  UNIQUE(EmpID)
);

To provide a name to UNIQUE constraint, and to define a UNIQUE constraint on multiple columns (say EmpID and Name), the statement is given below:

CREATE TABLE Employee (
  EmpID INT NOT NULL,
  Name VARCHAR(255) NOT NULL,
  City VARCHAR(100),
  Age INT,
  Salary DECIMAL(18,2),
  CONSTRAINT UC_Employee UNIQUE(EmpID, Name)
);

SQLite UNIQUE constraint with ALTER TABLE

SQLite does not support ALTER TABLE statement to add UNIQUE constraint to the existing table. However this can be achieved using following steps:

  • The foreign key constraint should be checked off
  • Rename the table to some other name
  • Create a new table with same structure but UNIQUE constraint added
  • Copy the data from renamed table to new table
  • In the end, turn on the foreign key constraints

For example, to enforce UNIQUE constraint on EmpID column of the existing table Employee, the following statement can be used:

PRAGMA foreign_keys=off;

BEGIN TRANSACTION;

ALTER TABLE Employee RENAME TO Employee_old;

CREATE TABLE Employee (
  EEmpID INT NOT NULL UNIQUE,
  Name VARCHAR(255),
  City VARCHAR(100),
  Age INT,
  Salary DECIMAL(18,2)
);

INSERT INTO Employee SELECT * FROM Employee_old;

COMMIT;

PRAGMA foreign_keys=on;

DROP UNIQUE constraint

SQLite does not support ALTER TABLE or DROP statement to drop an existing UNIQUE constraint from a table. To achieve this, the following steps can be used:

  • The foreign key constraint should be checked off
  • Rename the table to some other name
  • Create a new table with same structure but UNIQUE constraint dropped
  • Copy the data from renamed table to new table
  • In the end, turn on the foreign key constraints

For example, to drop UNIQUE constraint from EmpID column of the existing table Employee, the following statement can be used:

PRAGMA foreign_keys=off;

BEGIN TRANSACTION;

ALTER TABLE Employee RENAME TO Employee_old;

CREATE TABLE Employee (
  EmpID INT NOT NULL,
  Name VARCHAR(255),
  City VARCHAR(100),
  Age INT,
  Salary DECIMAL(18,2)
);

INSERT INTO Employee SELECT * FROM Employee_old;

COMMIT;

PRAGMA foreign_keys=on;