Question

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)
Was it helpful?

Solution

Try this:

ALTER TABLE tableA ADD UNIQUE INDEX idxColAB (columnA, columnB)

OTHER TIPS

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

link

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top