I want to define a composite unique constraint in a multi-reference table in Database Manager.
This is typically accomplished with:
UNIQUE ("UserId", "TaskId")
For example, the table below should only allow one record for each user and task. Multiple tasks per user can be allowed, but the user (e.g. user 1) and task (e.g. task 1) should never be duplicated.
Structure
Example data
UserId | TaskId | IsPinned | IsStarred |
---|---|---|---|
user1 | task1 | TRUE | FALSE |
user1 | task2 | FALSE | TRUE |
user2 | task1 | TRUE | TRUE |
With the UNIQUE ("UserId", "TaskId")
constraint:
- Allowed: Inserting a new record with
user2
andtask2
because this combination doesn't exist yet. - Not Allowed: Inserting a duplicate record with
user1
andtask1
because this combination already exists.