有一個SQL表那樣 MySQL的插入相同的行
INSERT INTO users_words(iduser,idword,status_of_word) VALUES(4,6,2);
INSERT INTO users_words(iduser,idword,status_of_word) VALUES(4,6,1);
INSERT INTO users_words(iduser,idword,status_of_word) VALUES(4,6,0);
INSERT INTO users_words(iduser,idword,status_of_word) VALUES(5,6,0);
INSERT INTO users_words(iduser,idword,status_of_word) VALUES(8,7,1);
INSERT INTO users_words(iduser,idword,status_of_word) VALUES(8,9,2);
I want to a unique index is together with iduser,idword. How to avoid inserting query with same values together index columns? For example: Bloking row inserting like 13 and 14 (idusers_words) rows
SELECT * FROM users_words;
> idusers_words | iduser | idword | status_of_word |
12 4 6 2
13 4 6 1
14 4 6 0
15 5 6 0
16 8 7 1
17 8 9 2
編輯:
I tried this code. But comes that error "#1062 - Duplicate entry '1-1' for key 'my_unique'"
ALTER TABLE users_words
ADD CONSTRAINT my_unique UNIQUE(iduser,idword);
在編輯的錯誤:那是因爲你已經在表中的一些重複的數據。在添加約束之前,您必須將其刪除。此外,放棄較早的唯一索引/約束,這是多餘的。 –
@I_am_Batman非常感謝你。你救了我的命。 –