18

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?

1
  • what's the context? e.g. language you're working on?
    – yoda
    Commented Feb 2, 2011 at 21:08

3 Answers 3

31

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.)

8
  • 6
    Alternatively utf8_bin is also commonly used.
    – unode
    Commented Feb 2, 2011 at 21:14
  • @Unode Yup, utf8_bin doesn't equate "á" to "a", etc. Commented Feb 2, 2011 at 21:15
  • Coupled to what @middaparka said and my previous comment, you should be careful about sorting since accentuated characters may not always sort the way you expect them to. However most of these issues are related to specific encodings and locale settings. (beyond the scope of this question)
    – unode
    Commented Feb 2, 2011 at 21:19
  • 11
    Error Code: 1273 Unknown collation: 'utf8_general_cs'
    – Cerin
    Commented Feb 3, 2011 at 15:53
  • 1
    I've tried both ALTER DATABASE mydb CHARACTER SET utf8 COLLATE utf8_bin; and ALTER TABLE mydb.mytable CHARACTER SET utf8 COLLATE utf8_bin; but when I run select first_name from mydb.mytable where first_name = 'joe2' it still shows "Joe2", indicating it's search is still case-insensitive. Am I doing something wrong?
    – Cerin
    Commented Feb 10, 2011 at 16:35
7

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;
1
  • I added this and it works - there are limitations for the column_datatype like VARCHAR limits < 300 lengths
    – Gordon
    Commented Apr 30, 2020 at 17:41
1

The most common way to create a MySQL database with case-sensitive collation is:

CREATE DATABASE your_db_name CHARACTER SET utf8 COLLATE utf8_bin;
2
  • I'm getting ERROR 1273 (HY000): Unknown collation: 'utf8'
    – Petro
    Commented Dec 12, 2022 at 22:36
  • Do you mean uft8_bin (with an underscore)? Commented Mar 8, 2023 at 6:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.