MySQL Tutorial
MySQL Installation and Configuration
MySQL Database Operations
Database Design
MySQL Data Types
MySQL Storage Engines
MySQL Basic Operations of Tables
MySQL Constraints
MySQL Operators
MySQL Function
MySQL Manipulate Table Data
MySQL View
MySQL Indexes
MySQL Stored Procedure
MySQL Trigger
MySQL Transactions
MySQL Character Set
MySQL User Management
MySQL Database Backup and Recovery
MySQL Log
MySQL Performance Optimization
MySQL uses character sets and collations to manage how text data is stored, compared, and sorted in the database. A character set is a collection of characters with corresponding encoding, and a collation defines the rules for comparing characters within a character set.
When MySQL chooses a character set for storing or manipulating text data, it follows a hierarchy of settings to determine the correct one. Here is the hierarchy, from the highest to the lowest priority:
Column level: If a character set is explicitly specified when defining a column in a table, MySQL uses that character set for the column.
Example:
CREATE TABLE example ( text_column VARCHAR(255) CHARACTER SET utf8mb4 );
Table level: If a character set is not specified at the column level, MySQL looks for a character set defined at the table level. If one is specified, it will be used for all columns in the table that do not have an explicitly defined character set.
Example:
CREATE TABLE example ( text_column VARCHAR(255) ) CHARACTER SET utf8mb4;
Database level: If a character set is not specified at the column or table level, MySQL looks for a character set defined at the database level. If one is specified, it will be used for all tables and columns within the database that do not have an explicitly defined character set.
Example:
CREATE DATABASE example_db CHARACTER SET utf8mb4;
Server level: If a character set is not specified at the column, table, or database level, MySQL uses the default character set defined at the server level. You can check the server-level character set and collation by running the following commands:
SHOW VARIABLES LIKE 'character_set_server'; SHOW VARIABLES LIKE 'collation_server';
If you want to change the default character set and collation at the server level, you can edit your MySQL server configuration file (e.g., my.cnf
or my.ini
) and add or modify the following lines:
[mysqld] character-set-server = utf8mb4 collation-server = utf8mb4_unicode_ci
After making changes, restart your MySQL server for the new settings to take effect.
By understanding the hierarchy of character set settings in MySQL, you can ensure that your text data is stored and processed correctly, and that the appropriate character set and collation are used for your specific requirements.
Specifying character set in MySQL queries:
CHARACTER SET
clause, ensuring consistency in data handling.SELECT * FROM table_name WHERE column_name = 'value' COLLATE utf8mb4_general_ci;
Changing character set at runtime in MySQL:
SET NAMES
statement.SET NAMES 'utf8mb4';