Search code examples
sqliteunique

Sqlite create unique pair of columns


I want to create a table with two columns: user_id, image_id. I don't want user_id or image_id to be unique, but I also want to protect my table from duplicate pairs of same user_id and image_id. Can I do that?


Solution

  • Add a separate constraint for both columns:

    CREATE TABLE MyTable(
        user_id INTEGER,
        image_id INTEGER,
        [...],
        UNIQUE(user_id, image_id)
    )