Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL Multiple Column Unique Key

Tags:

indexing

mysql

I'm familiar with the syntax of create unique index 'blah' on 'some_table' ('col1','col2') however I'm unsure exactly how this will behave. More specifically, the documentation says

A column list of the form (col1,col2,...) creates a multiple-column index. Index values are formed by concatenating the values of the given columns.

That seems like it might be okay in some cases, but I want make a table that holds just numbers unique. So, I could see col1 being 12 and col2 being 2, which will concatenate to 122 from what I can tell, and then a value of col1 1 and col2 22 would match that, which is not what I want. Is there a way to define a separator for the concatenation or is there already one in place? Should I be doing this differently?

Here's an attempt at a diagram

table 1             middle               table 2
-------             ------               -------
t1_id               mid_id               t2_id
some_value          t1_id                another_value
                    t2_id

I want the t1_id and t2_id combination to be unique, but t1 can repeat and t2 can repeat.

like image 535
Robert Avatar asked Sep 15 '26 01:09

Robert


1 Answers

It is not possible nor necessary to specify a separator. The index works as you want it to.

like image 192
Mark Byers Avatar answered Sep 16 '26 19:09

Mark Byers