In MySQL, the COLLATE
keyword is used to specify the collation of a string. A collation defines how string comparison is performed and how strings are sorted. It determines the rules for character comparison based on specific criteria, such as case sensitivity and accent sensitivity.
utf8mb4
character set has collations like utf8mb4_general_ci
(case-insensitive) and utf8mb4_bin
(binary comparison).
CREATE TABLE my_table (
my_column VARCHAR(255) COLLATE utf8mb4_unicode_ci
);
COLLATE
keyword in queries to specify how strings should be compared.
SELECT * FROM my_table WHERE my_column = 'example' COLLATE utf8mb4_general_ci;
ALTER TABLE
statement.
ALTER TABLE my_table MODIFY my_column VARCHAR(255) COLLATE utf8mb4_bin;
utf8_general_ci
: Case-insensitive comparison for the utf8 character set.utf8mb4_unicode_ci
: Case-insensitive comparison for the utf8mb4 character set, taking into account linguistic rules.utf8_bin
: Binary comparison for the utf8 character set.utf8mb4_bin
: Binary comparison for the utf8mb4 character set.The COLLATE
keyword in MySQL allows you to control how string comparisons are performed, impacting sorting and searching operations. Choosing the appropriate collation is important for ensuring that string data behaves as expected in your database applications.