Multi-column UNIQUE-ness constraint
Sometimes the each column can have the same value more than once, but 2 (or more column) put together must be unique. For example in this table we are storing coordinates of points. Both x and y can be any value and the same value can appear multiple times in either x or y.
However the (x, y) pair must be unique.
Here is how we can accomplish that.
CREATE TABLE points (
x INTEGER,
y INTEGER
);
CREATE UNIQUE INDEX xy_index ON points (x, y);
INSERT INTO points (x, y) VALUES (2, 3);
INSERT INTO points (x, y) VALUES (4, 5);
INSERT INTO points (x, y) VALUES (2, 3);
SELECT * FROM points;
$ sqlite3 < examples/points.sql
Error near line 10: UNIQUE constraint failed: points.x, points.y
2|3
4|5