[:user_id] It translates...">

Why is "BINARY" in a SELECT statement?

The model Votehas validation:

validates_uniqueness_of :topic_id, :scope => [:user_id]

It translates to the following SQL in the development log:

SELECT 1 AS one FROM `votes` WHERE (`votes`.`topic_id` = BINARY 2 AND `votes`.`user_id` = 1) LIMIT 1

Where is there BINARYbefore 2 (topic_id)? And what does it mean?

+5
source share
1 answer

This is an efficient way to compare byte with byte instead of character for character

Example

Suppose you have a database table with a name productsthat has vin_number( column name ) with an entry with a vin_numbersay value123456

Now if you run this

select * from products where vin= '123456' 

and

select * from products where vin = '123456 '

Both results will lead to the same result.

Note the space in the second selection

But with a binary comparison

select * from products where vin= BINARY '123456'

or

select * from producst where vin = BINARY '123456 '

Byte byte match is executed as character with character

valid

+7

All Articles