What are the commands to enable and disable case sensitivity by default for an entire database on a MySQL server? I'm aware of the COLLATE statement, but it seems it's necessary to place this in every SQL statement that's run. Is there an option to set this globally?
To set the collation for the entire database, you can use: CREATE DATABASE test_database CHARACTER SET utf8 COLLATE utf8_general_cs; You can also change the collation on an existing database via ALTER DATABASE. (For more information see the MySQL Database Character Set and Collation manual entry.)
Table names are stored in lowercase on disk and name comparisons are not case-sensitive. MySQL converts all table names to lowercase on storage and lookup. This behavior also applies to database names and table aliases.
For windows, it's available in the C:\ProgramData\MySQL\MySQL Server X.X directory. Open this file in administrator mode and set the lower_case_table_names variable value to 2.
You can set collation at both the database creation and table creation level as a part of the CREATE TABLE statement.
To set the collation for the entire database, you can use:
CREATE DATABASE test_database CHARACTER SET utf8 COLLATE utf8_general_cs;
You can also change the collation on an existing database via ALTER DATABASE. (For more information see the MySQL Database Character Set and Collation manual entry.)
If however, only a single table needs to be treated as case sensitive, you could simply use:
DROP TABLE IF EXISTS test_table;
CREATE TABLE test_table (
test_id bigint unsigned NOT NULL auto_increment,
...
PRIMARY KEY test_id (test_id),
...
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE utf8_general_cs;
(Case insensitive being "utf8_general_ci
".)
Finally, the main MySQL Character Set Support manual section is probably worth a quick peruse. (It lists the character sets and collations supported by MySQL, tells you how to set the character set/collation at the server level, etc.)
I've come here looking to modify the collation only for a specific column for it to be case-sensitive and not for the entire table or the database. Hope it helps someone looking just for this.
This query could be tried:
ALTER TABLE table_name MODIFY column_name column_datatype COLLATE utf8_bin;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With