SQLite: Preventing Duplicate Rows

后端 未结 3 1564
庸人自扰
庸人自扰 2020-12-14 09:32
CREATE TABLE Permission ( 
    permissionID INTEGER PRIMARY KEY UNIQUE,
    user         INTEGER
    location     INTEGER 
);

I don\'t want to have

3条回答
  •  暖寄归人
    2020-12-14 10:09

    Declare a unique constraint on (user, location).

    CREATE TABLE Permission (
        permissionID integer primary key,
        user integer not null,
        location integer not null,
        unique (user, location)
    );
    
    sqlite> insert into Permission (user, location) values (1, 2);
    sqlite> insert into Permission (user, location) values (1, 2);
    Error: UNIQUE constraint failed: Permission.user, Permission.location
    

提交回复
热议问题