已有數據的表-建聯合索引

清理重複數據,創建聯合惟一索引spa

1. 查看重複數據code

SELECT * FROM holiday_focamobilerel 
WHERE (cardnumber, mobile) IN (SELECT cardnumber, mobile FROM holiday_focamobilerel GROUP BY cardnumber, mobile HAVING COUNT(1) > 1);

2. 刪除重複數據blog

- 保留order_id and sub_order_id不爲空的索引

- 若是order_id和sub_order_id都爲空,保留id最大的table

DELETE FROM holiday_focamobilerel h
WHERE (cardnumber, mobile) IN (SELECT cardnumber, mobile FROM holiday_focamobilerel GROUP BY cardnumber, mobile HAVING COUNT(1) > 1)
AND h.id NOT IN (select id from holiday_focamobilerel h where h.order_id  is not null  or h.sub_order_id  is not null )
and h.id not in (SELECT MAX(id) FROM holiday_focamobilerel GROUP BY cardnumber, mobile HAVING COUNT(1) > 1);

 3. 創建聯合惟一約束class

alter table holiday_focamobilerel add constraint cardnumber_mobile_key unique("cardnumber","mobile")
相關文章
相關標籤/搜索