2007-08-28

Select binary characters

Beware, the BINARY option on fields and queries in MySQL does more than just discern case sensitivity. Not using it may give strange results: SELECT BINARY 'e' = 'E'; 0 SELECT 'e' = 'E'; 1 SELECT BINARY 'e' = 'é'; 0 SELECT 'e' = 'é'; 1 This can give strange results if you have a CHAR field as part of a primary key. You collect data in a python dict, for example and want to store that data. Python will say your keys are unique, MySQL will give a duplicate entry error. To prevent this from happening, there seem to be two options:
  1. No strange characters in your strings. Not easy.
  2. Use BINARY CHAR fields in keys. Easy, but has the case sensitive search side effect

Geen opmerkingen: