更新MySQL主鍵 - Updating MySQL primary key

問題:

I have a table user_interactions with 4 columns: 我有一個表user_interactions有4列:

 user_1
 user_2
 type
 timestamp

The primary key is (user_1,user_2,type) 主鍵是(user_1,user_2,type)
and I want to change to (user_2,user_1,type) 我想更改爲(user_2,user_1,type)

So what I did was : 所以我所做的是:

drop primary key ...  
add primary key (user_2,user_1,type)...

and voila... 瞧...

The problem is that database is live on a server. 問題在於數據庫在服務器上處於活動狀態。

So before I could update the primary key, many duplicates already crept in, and they are continuously creeping in. 因此,在我可以更新主鍵之前,許多重複項已經悄悄潛入,並且它們一直在不斷蔓延。

What to do? 該怎麼辦?

What I want to do now is to remove duplicates and keep the ones with the latest timestamp (which is a column in the table). 我現在想做的是刪除重複項,並保留最新的timestamp (在表中的一列)。

And then somehow update the primary key again. 然後以某種方式再次更新主鍵。


解決方案:

參考: https://stackoom.com/en/question/9p9M
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章