Reputation: 4972
Is it possible to make a 2 coulmns unique in mysql db? for example:
ID | columnA | columnB
1 | Dan | 1
2 | Dan | 2
3 | Zak | 1
4 | Dan | 1 (WHEN TRYING TO INSERT ROW 4 - NOT ALLOWED! DUPLICATE COULMNA + COLUMNB)
Upvotes: 1
Views: 74
Reputation: 13484
A unique index guarantees that the index key contains no duplicate values and therefore every row in the table is in some way unique. There are no significant differences between creating a UNIQUE constraint and creating a unique index that is independent of a constraint. Data validation occurs in the same manner, and the query optimizer does not differentiate between a unique index created by a constraint or manually created. However, creating a UNIQUE constraint on the column makes the objective of the index clear
Upvotes: 0
Reputation: 29071
Try this:
ALTER TABLE tableA ADD UNIQUE INDEX idxColAB (columnA, columnB)
Upvotes: 6