如何将唯一键添加到现有表(具有非唯一行) [英] How add unique key to existing table (with non uniques rows)

查看:89
本文介绍了如何将唯一键添加到现有表(具有非唯一行)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想向现有表添加复杂的唯一键.键包含4个字段(user_idgame_iddatetime). 但是表具有非唯一行. 我知道我可以删除所有重复的日期,然后再添加复杂的密钥.

I want to add complex unique key to existing table. Key contains from 4 fields (user_id, game_id, date, time). But table have non unique rows. I understand that I can remove all duplicate dates and after that add complex key.

也许存在不搜索所有重复数据的另一种解决方案. (例如添加唯一的忽略等).

Maybe exist another solution without searching all duplicate data. (like add unique ignore etc).

UPD 我搜索了如何删除重复的mysql行-我认为这是一个很好的解决方案. 仅使用MySQL查询删除重复项?

UPD I searched, how can remove duplicate mysql rows - i think it's good solution. Remove duplicates using only a MySQL query?

推荐答案

您可以按照yAnTar的建议进行操作

You can do as yAnTar advised

ALTER TABLE TABLE_NAME ADD Id INT AUTO_INCREMENT PRIMARY KEY

OR

您可以添加约束

ALTER TABLE TABLE_NAME ADD CONSTRAINT constr_ID UNIQUE (user_id, game_id, date, time)

但是我认为您可以添加一个身份列,然后创建一个复合键,以免丢失现有数据.

But I think to not lose your existing data, you can add an indentity column and then make a composite key.

这篇关于如何将唯一键添加到现有表(具有非唯一行)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆