MySQL 仅当两个值不重复时才导入

MySQL import only if TWO VALUES don't repeat

假设您有 table:

Time Fish   Name

5    salmon John
9    whale  Michael
7    shark  Harry

我想插入一行。但我也希望它在 TIME 和 FISH 重复时不要插入它。

所以我可以加5 whale Unnamed,可以加9 salmon Michael,但是不能加5 salmon Michael,不能加5 salmon John等等。

这可能吗?

使两列唯一。 运行下面查询

ALTER TABLE mytbl ADD UNIQUE (Time, Fish);

在创建 table 时,您可以声明一个复合主键

create table tablename (Time int,fish varchar(5), name varchar(10),primary key(Time,fish)); This will allow only unique pairs of time and fish