LokoTerrorita
LokoTerrorita

Reputation: 117

How to set unique key on two columns and still have primary index with auto increment in MySQL?

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?

Upvotes: 0

Views: 62

Answers (2)

sunysen
sunysen

Reputation: 2361

try

ALTER TABLE yourtable ADD UNIQUE KEY (user_id, answer_id)

Upvotes: 2

Explosion Pills
Explosion Pills

Reputation: 191809

ALTER TABLE t1 ADD UNIQUE KEY (user_id, answer_id)

Upvotes: 3

Related Questions