Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

How can SQLite prevent duplicate combinations of column values?

Quick answer

Add a table-level UNIQUE(person_id, project_id) constraint. It makes the pair unique, so the same person can appear with different projects and the same project with different people. Do not put a separate UNIQUE constraint on each column: that would require every person and every project to appear at most once, which is a stricter, different rule. [S1]

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

I’m making an assignments table. A person can work on several projects, and a project can have several people, but the same person should not be assigned to the same project twice. How should I express that?

Geez

Put a single table-level UNIQUE constraint over both columns. For example, assuming person_id and project_id are the identifiers you use, a disposable schema is:

CREATE TABLE assignments (
  person_id  INTEGER NOT NULL,
  project_id INTEGER NOT NULL,
  UNIQUE (person_id, project_id)
);

The rule applies to the combination: a row with (12, 45) and another with (12, 46) have different pairs, as do (12, 45) and (13, 45). Repeating (12, 45) would repeat the pair that the constraint requires to be unique. SQLite permits a table-level UNIQUE constraint on a list of columns, including this kind of pair. [S1]

The NOT NULL declarations are example schema choices, not part of the duplicate-combination rule itself. Replace the sample identifiers and types to fit your design.

Follow-up question

Why not write UNIQUE on both columns separately? That sounds like it would also stop duplicate assignments.

Geez

Because separate uniqueness rules express a different, stricter requirement. If person_id alone is unique, a person can occur in only one assignment row. If project_id alone is unique, a project can occur in only one assignment row. Together, those rules prevent a person from having multiple projects and a project from having multiple people.

For the many-to-many assignment rule you described, use one constraint on the pair:

UNIQUE (person_id, project_id)

SQLite documents this distinction: uniqueness across a list requires each pair to be unique, while separate uniqueness on each column limits each individual value to at most one appearance. [S1]

Follow-up question

Can you show the difference with a small example I can check against my intended rule?

Geez

Treat these rows as an illustrative example, not as data from a particular database:

person_id | project_id
----------+-----------
12        | 45
12        | 46
13        | 45

All three pairs are different. The pair-based constraint permits these combinations because it does not make either column individually unique. Adding (12, 45) again would duplicate a pair, so it conflicts with the stated rule. By contrast, separate UNIQUE(person_id) and UNIQUE(project_id) rules would reject the example: person 12 appears twice, and project 45 appears twice. [S1]

When checking your own design, write down two test cases: a repeated pair that should be disallowed, and two different pairs sharing one value that should be allowed. That helps reveal whether the constraint matches the rule.

Follow-up question

What should I verify before I add this to my table?

Geez

First, confirm that the rule really is “no repeated pair,” rather than “each person can appear once” or “each project can appear once.” Then put both column names in the same table-level UNIQUE constraint. If the table already has rows, consider whether existing repeated pairs need attention before applying the rule; the example does not establish how your schema or migration should handle existing data.

This is a CREATE TABLE example. The SQLite rule described here distinguishes table-level uniqueness across a column list from separate per-column uniqueness; it does not cover every schema detail, migration method, or application-specific assignment rule. [S1]

A practical checklist

  • Write the rule as: “No two rows may have the same person-and-project pair.”

  • Use one table-level UNIQUE (person_id, project_id) constraint.

  • Do not use separate UNIQUE constraints on the two columns unless each value must appear at most once.

  • Check that different pairs sharing a person or a project should remain allowed.

  • Review existing rows and your table’s other requirements before applying a schema change.

A few more useful answers

Does the order of the two columns change which pair is constrained?

The columns correspond by position: (12, 45) and (45, 12) are different combinations when the first value is in person_id and the second is in project_id. The constraint makes the specified combination unique; it does not make each column individually unique. [S1]

Can I use this for a different kind of combination?

Yes. Apply the same design pattern to the set of columns that together define the duplicate you want to prevent. Make sure the rule is about the combination, not about each column appearing only once.

Does this example describe how to change a table that already exists?

No. It shows a disposable CREATE TABLE example. The cited SQLite material supports the constraint behavior, but does not establish a migration procedure for an existing table.

Sources

References used for this guide.

  1. [S1] SQLite CREATE TABLE: table constraints and UNIQUE
    sqlite.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work