Skip to main content

How to Stop Duplicate Rows in SQLite with a UNIQUE Constraint

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

One person, one vote: a poll shouldn't count the same voter twice. A UNIQUE constraint makes SQLite refuse a row that repeats values another row already has. This guide lets each voter vote once per poll, with real output from SQLite 3.54.1.

Syntax​

Syntax
CREATE TABLE <table> (
<column 1> <type> NOT NULL,
<column 2> <type> NOT NULL,
UNIQUE (<column 1>, <column 2>)
);
PartWhat it means
UNIQUE (<column 1>, <column 2>)A separate line after the columns: no two rows may have the same pair of values
NOT NULLKeeps empty values out. NULL never counts as a repeat, as Common Mistakes shows

SQLite's documentation on UNIQUE constraints: "For each UNIQUE constraint on the table, each row must contain a unique combination of values in the columns identified by the UNIQUE constraint."

Prerequisites​

  • The sqlite3 command-line shell. This guide uses SQLite 3.54.1. Run sqlite3 --version to see yours; the SQLite download page has the command-line tools for Windows, macOS and Linux.

Step 1: Write the Script​

Save this as unique_constraint.sql:

unique_constraint.sql
CREATE TABLE votes (
poll TEXT NOT NULL,
voter TEXT NOT NULL,
choice TEXT NOT NULL,
UNIQUE (poll, voter)
);

INSERT INTO votes (poll, voter, choice) VALUES ('lunch', 'ana', 'pizza');
INSERT INTO votes (poll, voter, choice) VALUES ('lunch', 'ben', 'soup');
INSERT INTO votes (poll, voter, choice) VALUES ('dinner', 'ana', 'curry');
INSERT INTO votes (poll, voter, choice) VALUES ('lunch', 'ana', 'salad');

SELECT * FROM votes;

The unique_constraint.sql script in a macOS-style code window: a votes table with poll, voter and choice columns and UNIQUE (poll, voter), four inserts where ana votes in lunch, ben in lunch, ana in dinner and ana in lunch again, and a SELECT of every row.

  • UNIQUE (poll, voter) allows each voter one row per poll.
  • ana votes in lunch and dinner, and ben in lunch: no pair repeats.
  • The last INSERT is ana voting in lunch again.

Step 2: Run It​

In the folder that holds the script, run:

Terminal
sqlite3 -table polls.db ".read unique_constraint.sql"

sqlite3 opens the database file polls.db, and creates it when it isn't there. -table prints each result as a table, and .read runs the statements in the script. The command is the same in bash, zsh and PowerShell.

Output
Error near line 11 of unique_constraint.sql: UNIQUE constraint failed: votes.poll, votes.voter
+--------+-------+--------+
| poll | voter | choice |
+--------+-------+--------+
| lunch | ana | pizza |
| lunch | ben | soup |
| dinner | ana | curry |
+--------+-------+--------+

The output in a terminal: an error near line 11 of unique_constraint.sql, UNIQUE constraint failed: votes.poll, votes.voter, then the votes table with ana's pizza in lunch, ben's soup in lunch and ana's curry in dinner.

The first three votes went in. ana appears twice, but in different polls. Line 11 repeated the pair lunch and ana, and SQLite refused it with an error that names both columns. The shell reported it and carried on to the SELECT.

Common Mistakes​

MistakeWhat happensDo this instead
Leaving out NOT NULLTwo lunch votes with no voter both went in: a NULL never equals another NULL, so it isn't a repeatMark the columns NOT NULL
Expecting 'Ana' and 'ana' to clashBoth went in, because text comparison is case-sensitiveAdd COLLATE NOCASE to the column: voter TEXT NOT NULL COLLATE NOCASE refused the second with UNIQUE constraint failed: votes.poll, votes.voter

Troubleshooting​

SymptomLikely causeFix
UNIQUE constraint failed: votes.poll, votes.voterThat voter already voted in that pollSkip the row: it's a repeat
Parse error near line 1 of unique_constraint.sql: table votes already existsThe script already ran on polls.dbDelete polls.db and run the script again

FAQ​

How do I make one column unique?​

Write UNIQUE after its type: username TEXT NOT NULL UNIQUE. A second user with the same username was refused with UNIQUE constraint failed: users.username.

How is UNIQUE different from a primary key?​

A table has at most one primary key, but it can have many UNIQUE constraints. The documentation: "A UNIQUE constraint is similar to a PRIMARY KEY constraint, except that a single table may have any number of UNIQUE constraints."

Conclusion​

UNIQUE (poll, voter) let ana vote in two polls and ben in one, and refused ana's second vote in lunch.

Additional Resources​