How to Stop Duplicate Rows in SQLite with a UNIQUE Constraint
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
CREATE TABLE <table> (
<column 1> <type> NOT NULL,
<column 2> <type> NOT NULL,
UNIQUE (<column 1>, <column 2>)
);
| Part | What it means |
|---|---|
UNIQUE (<column 1>, <column 2>) | A separate line after the columns: no two rows may have the same pair of values |
NOT NULL | Keeps 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 --versionto 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:
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;

UNIQUE (poll, voter)allows each voter one row per poll.anavotes in lunch and dinner, andbenin lunch: no pair repeats.- The last
INSERTisanavoting in lunch again.
Step 2: Run It
In the folder that holds the script, run:
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.
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 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
| Mistake | What happens | Do this instead |
|---|---|---|
Leaving out NOT NULL | Two lunch votes with no voter both went in: a NULL never equals another NULL, so it isn't a repeat | Mark the columns NOT NULL |
Expecting 'Ana' and 'ana' to clash | Both went in, because text comparison is case-sensitive | Add COLLATE NOCASE to the column: voter TEXT NOT NULL COLLATE NOCASE refused the second with UNIQUE constraint failed: votes.poll, votes.voter |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
UNIQUE constraint failed: votes.poll, votes.voter | That voter already voted in that poll | Skip the row: it's a repeat |
Parse error near line 1 of unique_constraint.sql: table votes already exists | The script already ran on polls.db | Delete 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
- UNIQUE constraints — how SQLite compares the values.
- CREATE TABLE — column and table constraints.
