Case sensitive database - how to fetch, update, or delete data if case-insensitive _ci encoding is selected
Selecting data from the database (DB) is a normal part of the work of any modern application. In most cases, it is sufficient to retrieve data in a case insensitive manner, and this is how most databases work, which can improve performance.
However, what to do in cases when it is necessary to case sensitive selection of data from the database ? This problem is relevant for non-binary strings (VARCHAR, TEXT, CHAR, etc.), while data in binary form is case-sensitive (BLOB, BINARY and other types).
There are several ways to solve this problem:
- use the BINARY command for binary data processing. In this case, the data will be processed in binary form. Example query: SELECT * FROM `table` WHERE BINARY` column` = 'value' ;
- use case-sensitive database encoding, collation is named ending in _cs or _bin. Most likely, this method will not work for most cases, since no one will change the encoding of the entire database due to one operation with the need to process data in case sensitive ;
- use in a request a command with an explicitly specified encoding for a specific request, but errors are possible if the specified encoding is not available on the database server. Example query: SELECT * FROM `table` WHERE` column` COLLATE latin1_general_cs = 'value' ;
- use special SQL UPPER or LOWER commands, but this may affect performance if there is a large amount of data. Example query: SELECT * FROM `table` WHERE LOWER (` column`) = LOWER ('value') ;
- use case conversion in the code and then execute queries, for this functions like mb_strtolower, mb_strtoupper, etc. can be used ;
- use logic in your code to filter strings with different case. This means that first all the necessary data is selected that meet the specified criteria, after which an additional case check occurs in the code, for example, in PHP .
The last two methods may not be the most efficient for a large dataset. This approach is not suitable for all cases, since it is recommended to use database tools for better performance .
We have covered some techniques for case sensitive database . Similar working methods can be used for update and delete operations, that is, for UPDATE and DELETE commands.
- 09.11.23IT / Database Errors when migrating from MySQL 5.6 to 5.7 and how to fix them - database dump import failed with an error or INSERT does not work. Disabling STRICT_TRANS_TABLES strict mode or using IGNORE
- 09.07.22IT / Misc Convert office files DOC, DOCX, DOCM, RTF to DOCX, DOCM, DOC, RTF, PDF, HTML, XML, TXT formats without loss and markup changes
- 07.07.22IT / Safety How to protect PHP, JS, HTML, CSS source code - obfuscation, minification, compression and encryption
- 06.07.22IT / Safety Connection not secure, problem with Lets Encrypt - how to fix expired 09/30/2021 DST Root CA X3, remove it manually and install ISRG Root X1. Example on MS Windows 7
- 08.07.21IT / Misc How to make a free translation for a website without an API, translate documents in Google Translate