0

I want to set my table so that it won't allow insertion of a row if there's already row with the same values on two columns, but still keep the id column?

My table has columns - id(int), user_id(int), answer_id(int)

I want the id to autoincrement and unique. And yet have user_id + answer_id as unique combination, so that there will never be two rows having same user_id and answer_id

How can I do that?

4

2 回答 2

3
ALTER TABLE t1 ADD UNIQUE KEY (user_id, answer_id)
于 2013-10-07T23:38:38.700 回答
2

try

ALTER TABLE yourtable ADD UNIQUE KEY (user_id, answer_id)
于 2013-10-07T23:48:40.330 回答