【问题标题】:Prevent duplicate entries in a database table based on 2 columns防止基于 2 列的数据库表中的重复条目
【发布时间】:2015-05-01 01:57:02
【问题描述】:

我在 MySQL 数据库中记录了一场比赛的数据。

我需要为每个玩家设置唯一的条目,所以我这样做了:

alter table xtu_datas_competition add unique index(email);

完美运行。

但是有几个比赛,所以我也有一个colum id_比赛,我希望有一个独特的电子邮件和特定colum id_比赛的条目

例如:

2 identical email for 2 identical colum id_ competition : duplicate
2 identical email  for 2 different colum id_ competition : not duplicate

如何调整我以前的代码?

【问题讨论】:

    标签: mysql sql duplicates unique alter-table


    【解决方案1】:

    您只需定义一个包含两列的唯一约束:

    alter table xtu_datas_competition add unique index(id_competition,email);
    

    希望这会对你有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-23
      • 2014-05-04
      • 1970-01-01
      • 2019-07-24
      • 2015-11-29
      • 1970-01-01
      相关资源
      最近更新 更多