[英]Create mysql unique key based on three columns and a specific value
我有一個表,該表在兩列user_id
和country_id
上具有唯一索引
我添加了一個新列deleted_at
以便可以在保留數據的同時刪除行。
我現在想更新唯一鍵,使其基於user_id
, country_id
以及deleted_at
NULL的位置。 如果可能的話,這可能嗎?
+----+---------+------------+------------+
+ id | user_id | country_id | deleted_at |
+----+---------+------------+------------+
+ 2 | 3 | 1 | NULL |
+ 3 | 3 | 1 | 2012-10-16 |
| 4 | 3 | 1 | 2012-10-15 |
+----+---------+------------+------------+
使用以上內容作為參考,由於ID為2,因此無法添加行,但是,如果未設置第2行,則可以創建新行。
修改表mytable應該可以解決問題:
alter table mytable drop index user_country;
alter table mytable add
unique index user_country_deleted (user_id, country_id, deleted_at);
編輯:我太快了。 根據CREATE INDEX語法,這僅適用於BDB存儲。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.