Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL: Unique constraint on multiple fields [duplicate]

Tags:

sql

mysql

unique

I have two tables --> Variables (id, name) and Variable_Entries (id, var_id, value).

I want each variable to have a unique set of entries. If I make the value entry unique then a different variable won't be able to have that same value which is not right.

Is there some way to make the value column unique for identical var_id's?

like image 781
Cypher Avatar asked Mar 23 '10 22:03

Cypher


1 Answers

Yes:

alter table Variable_Entries add unique (var_id, value);

Now you have a unique constraint across var_id and value together. In other words, no occurrence of var_id and value can appear more than once.

like image 126
Ross Snyder Avatar answered Oct 02 '22 12:10

Ross Snyder