문제

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)
도움이 되었습니까?

해결책

Try this:

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

다른 팁

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top