我是否正确定义了所有密钥?我需要添加索引吗?

Am I defining all my keys correctly? Do I need to add an index?

我有几个表:帐户和员工,我正在尝试为帐户和员工之间的任何给定组合存储评级和时间戳。对于给定的帐户 + 员工组合,同一时间戳不应存在两个评级。

这是我目前拥有的:

CREATE TABLE main_table (
    _ID INTEGER PRIMARY KEY AUTOINCREMENT, 
    account_id INTEGER NOT NULL, 
    employee_id INTEGER NOT NULL,
    rating REAL NOT NULL, 
    timestamp LONG NOT NULL, 
    FOREIGN KEY(account_id) REFERENCES ACCOUNTS(_ID), 
    FOREIGN KEY(employee_id) REFERENCES EMPLOYEES(_ID), 
    UNIQUE (account_id, employee_id, timestamp));

这是定义我想要做的事情的正确方法吗?我还需要创建一个单独的索引吗?

CREATE INDEX main_table_idx ON main_table (account_id, employee_id, timestamp);

使用唯一索引实现唯一约束。

因此,不需要单独创建索引。

documentation 中所述:

In most cases, UNIQUE and PRIMARY KEY constraints are implemented by creating a unique index in the database. (The exceptions are INTEGER PRIMARY KEY and PRIMARY KEYs on WITHOUT ROWID tables.)

根据您的描述,唯一约束似乎是正确的。