Sitemap
DevOps.dev

Devops.dev is a community of DevOps enthusiasts sharing insight, stories, and the latest development in the field.

MySQL case-sensitivity solution

4 min readOct 23, 2024

--

1. MySQL case-sensitivity control

MySQL controls case sensitivity through the lower_case_table_names parameter, which is under the [mysqld] node. The author has taken a screenshot from the official website for the specific meaning.

Press enter or click to view image in full size

Note:

① About the lower_case_table_names parameter to control the case sensitivity of table names or database names .

② The default value is 0 under Unix, which means case-sensitive. The default value is 1 under Windows, which means insensitive. The default value is 2 on macOS, which means case-sensitive storage but converted to lowercase when querying.

③ For case-insensitive systems (Windows and macOS), this field cannot be set to 0.

2. Problems

#1. Linux defaults to lower_case_table_names=0, which is case sensitive. Note: The author’s data version is 5.7.22.

Note: lower_case_file_system: OFF means case-sensitive, ON means case-insensitive. lower_case_table_names: 0 means case-sensitive, 1 means case-insensitive.

#2. Specific demonstration.

Press enter or click to view image in full size

Note: Create a test table and insert a record.

Case 1:

Press enter or click to view image in full size

analyze:

By comparing the two sets of queries, we can see that under Linux, MySQL is case-sensitive to table names by default.

Case 2:

Press enter or click to view image in full size

analyze:

Get Harries’s stories in your inbox

Join Medium for free to get updates from this writer.

The same results are obtained by searching with ‘Aa’ and ‘aa’, which indicates that the field content is converted (uppercase to lowercase) during the query, resulting in this phenomenon.

Case 3:

Press enter or click to view image in full size

analyze:

When querying, the results can also be queried for the field ‘Name’, indicating that the query is not case sensitive to the field.

Combining the above cases, we can draw the following conclusions:

① By default, Linux is case-sensitive to database and table names because the Linux system itself is case-sensitive.

②The field content is case insensitive.

③Column names are case insensitive.

The above-mentioned problems still have corresponding impacts on actual business, so corresponding solutions should be found.

3. Solutions for case-insensitive field content and column names

① When creating a table, mark the field as binary . Binary fields are case sensitive.

Press enter or click to view image in full size

② Add binary before the field in the query condition, but this is not recommended because it will invalidate the field index .

③ Set the collation rules ( collate ) when creating the table .

collate rules:

*_bin : indicates binary case sensitive collation, which means it is case sensitive .
*_ci: case insensitive collation, which is not case sensitive .

Press enter or click to view image in full size
Press enter or click to view image in full size

④ If the proofreading rule does not use *_bin when creating the table, you can directly modify the properties of the field .

alter  table test3 modify name varchar ( 20 ) collate utf8_bin;
Press enter or click to view image in full size

IV. Conclusion

As long as you specify collate as utf8_bin when creating a table , case sensitivity can be achieved. If it is not specified when creating the table, you can modify the collation rules of the field to achieve case sensitivity.

--

--

Written by Harries

Build in Public · AI · Indie Hacking · Engineering

Published in DevOps.dev

Devops.dev is a community of DevOps enthusiasts sharing insight, stories, and the latest development in the field.