
- MySQL - Home
- MySQL - Introduction
- MySQL - Features
- MySQL - Versions
- MySQL - Variables
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Node.js Syntax
- MySQL - Java Syntax
- MySQL - Python Syntax
- MySQL - Connection
- MySQL - Workbench
- MySQL Databases
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Show Database
- MySQL - Copy Database
- MySQL - Database Export
- MySQL - Database Import
- MySQL - Database Info
- MySQL Users
- MySQL - Create Users
- MySQL - Drop Users
- MySQL - Show Users
- MySQL - Change Password
- MySQL - Grant Privileges
- MySQL - Show Privileges
- MySQL - Revoke Privileges
- MySQL - Lock User Account
- MySQL - Unlock User Account
- MySQL Tables
- MySQL - Create Tables
- MySQL - Show Tables
- MySQL - Alter Tables
- MySQL - Rename Tables
- MySQL - Clone Tables
- MySQL - Truncate Tables
- MySQL - Temporary Tables
- MySQL - Repair Tables
- MySQL - Describe Tables
- MySQL - Add/Delete Columns
- MySQL - Show Columns
- MySQL - Rename Columns
- MySQL - Table Locking
- MySQL - Drop Tables
- MySQL - Derived Tables
- MySQL Queries
- MySQL - Queries
- MySQL - Constraints
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Replace Query
- MySQL - Insert Ignore
- MySQL - Insert on Duplicate Key Update
- MySQL - Insert Into Select
- MySQL Indexes
- MySQL - Indexes
- MySQL - Create Index
- MySQL - Drop Index
- MySQL - Show Indexes
- MySQL - Unique Index
- MySQL - Clustered Index
- MySQL - Non-Clustered Index
- MySQL Operators and Clauses
- MySQL - Where Clause
- MySQL - Limit Clause
- MySQL - Distinct Clause
- MySQL - Order By Clause
- MySQL - Group By Clause
- MySQL - Having Clause
- MySQL - AND Operator
- MySQL - OR Operator
- MySQL - Like Operator
- MySQL - IN Operator
- MySQL - ANY Operator
- MySQL - EXISTS Operator
- MySQL - NOT Operator
- MySQL - NOT EQUAL Operator
- MySQL - IS NULL Operator
- MySQL - IS NOT NULL Operator
- MySQL - Between Operator
- MySQL - UNION Operator
- MySQL - UNION vs UNION ALL
- MySQL - MINUS Operator
- MySQL - INTERSECT Operator
- MySQL - INTERVAL Operator
- MySQL Joins
- MySQL - Using Joins
- MySQL - Inner Join
- MySQL - Left Join
- MySQL - Right Join
- MySQL - Cross Join
- MySQL - Full Join
- MySQL - Self Join
- MySQL - Delete Join
- MySQL - Update Join
- MySQL - Union vs Join
- MySQL Keys
- MySQL - Unique Key
- MySQL - Primary Key
- MySQL - Foreign Key
- MySQL - Composite Key
- MySQL - Alternate Key
- MySQL Triggers
- MySQL - Triggers
- MySQL - Create Trigger
- MySQL - Show Trigger
- MySQL - Drop Trigger
- MySQL - Before Insert Trigger
- MySQL - After Insert Trigger
- MySQL - Before Update Trigger
- MySQL - After Update Trigger
- MySQL - Before Delete Trigger
- MySQL - After Delete Trigger
- MySQL Data Types
- MySQL - Data Types
- MySQL - VARCHAR
- MySQL - BOOLEAN
- MySQL - ENUM
- MySQL - DECIMAL
- MySQL - INT
- MySQL - FLOAT
- MySQL - BIT
- MySQL - TINYINT
- MySQL - BLOB
- MySQL - SET
- MySQL Regular Expressions
- MySQL - Regular Expressions
- MySQL - RLIKE Operator
- MySQL - NOT LIKE Operator
- MySQL - NOT REGEXP Operator
- MySQL - regexp_instr() Function
- MySQL - regexp_like() Function
- MySQL - regexp_replace() Function
- MySQL - regexp_substr() Function
- MySQL Fulltext Search
- MySQL - Fulltext Search
- MySQL - Natural Language Fulltext Search
- MySQL - Boolean Fulltext Search
- MySQL - Query Expansion Fulltext Search
- MySQL - ngram Fulltext Parser
- MySQL Functions & Operators
- MySQL - Date and Time Functions
- MySQL - Arithmetic Operators
- MySQL - Numeric Functions
- MySQL - String Functions
- MySQL - Aggregate Functions
- MySQL Misc Concepts
- MySQL - NULL Values
- MySQL - Transactions
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - SubQuery
- MySQL - Comments
- MySQL - Check Constraints
- MySQL - Storage Engines
- MySQL - Export Table into CSV File
- MySQL - Import CSV File into Database
- MySQL - UUID
- MySQL - Common Table Expressions
- MySQL - On Delete Cascade
- MySQL - Upsert
- MySQL - Horizontal Partitioning
- MySQL - Vertical Partitioning
- MySQL - Cursor
- MySQL - Stored Functions
- MySQL - Signal
- MySQL - Resignal
- MySQL - Character Set
- MySQL - Collation
- MySQL - Wildcards
- MySQL - Alias
- MySQL - ROLLUP
- MySQL - Today Date
- MySQL - Literals
- MySQL - Stored Procedure
- MySQL - Explain
- MySQL - JSON
- MySQL - Standard Deviation
- MySQL - Find Duplicate Records
- MySQL - Delete Duplicate Records
- MySQL - Select Random Records
- MySQL - Show Processlist
- MySQL - Change Column Type
- MySQL - Reset Auto-Increment
- MySQL - Coalesce() Function
MySQL - Collation
MySQL Collation is a set of rules used to decide how to compare and sort various characters of a character set. MySQL supports multiple character sets including ASCII, Unicode System, Binary, etc.
Every character of these character sets is subjected to a weight. A MySQL collation orders the characters based on their respective weights. For instance, when comparing two characters in a character set, if one character holds heavier weight than the other, it is greater; and vice-versa. If both characters have equal weights, they are equal.
Each character set must have at least one collation (or more) and no two character sets can have the same collation.
Implementing MySQL Collations
MySQL implements various types of collations in order to compare character strings −
Simple Collations for 8-bit Character Sets
Complex Collations for 8-bit Character Sets
Collations for Non-Unicode Multibyte Character Sets
Collations for Unicode Multibyte Character Sets
Miscellaneous Collations
Every character set has a built-in binary collation, so they need not be redefined. Built-in collations like these must not be modified in any case to avoid unexpected server behaviour.
Simple Collations for 8-bit Character Sets
- As the 8-bit character sets can only hold up to 256 characters, this type of collation is implemented by using a weights array of length 256.
- Each character in the character set is one-to-one mapped to the weights.
- It is a case-insensitive collation, so the uppercase and lowercase of same character hold the same weight.
Complex Collations for 8-bit Character Sets
- For complex 8-bit character sets, collations are implemented by defining the order of characters using functions.
- Here, we create a C source file that specifies the character set properties and defines the necessary support routines to perform operations on that character set properly.
Collations for Non-Unicode Multibyte Character Sets
- Unlike single-byte (8-bit) characters, there are two types of relationships between codes and weights of multi-byte characters.
- Weight of a character is equal to its code.
- Character codes are mapped one-to-one with weights, where weights are not necessarily equal to codes.
Collations for Unicode Multibyte Character Sets
Some collations are based on the Unicode Collation Algorithm (UCA). They hold the following properties −
- If a character has weight, each weight uses 2 bytes.
- If a character has no weight, then the character is ignorable.
- A single character can have many weights. This is called Expansion. For example, the German letter (SHARP S) has a weight of 0x0FEA0FEA.
- Multiple characters together can have only one weight. This is called Contraction. For example, 'ch' is a single letter in Czech and has a weight of 0x0EE2.
Miscellaneous Collations
- Collations that do not fall into any previous categories are termed as Miscellaneous Collations.
Set Character Set and Collation
MySQL allows us to set the character sets and collations at three different levels. The same is described below:
At Server level
At Database level
At Table level
At Server Level
In MySQL, the character set latin1 will be used as the default character set. So, the default collation will be latin1_swedish_ci. MySQL allows us to change these default settings at the server startup level.
When starting up a MySQL server, if we specify a character set, it will use the default collation of that set. But if we explicitly specify both a character set and collation, MySQL will use that combination for all databases created further.
Example
In the following query, we will set the character set as utf8 and the collation as utf8_unicode_cs for the sever.
mysqld --character-set-server=utf8 --collation-server=utf8_unicode_cs
A warning is issued if --collation-server is set to a user-defined collation name.
At Database Level
When we create a database and if we do not provide any character set and collation, the database will use the default character set and collation of the server.
We can override the default character set and collation at the database level using the CREATE DATABASE statement.
If we want to override default settings for existing database, we can use the ALTER DATABASE statement.
Syntax
Following is the basic syntax to override the default settings at database level −
[CREATE | ALTER] DATABASE database_name CHARACTER SET character_set_name COLLATE collation_name;
Example
Here, we are creating a database and specifying the character set as utf8 and collation as utf8_unicode_ci using the following query −
CREATE DATABASE testdb CHARACTER SET utf8 COLLATE utf8_unicode_ci;
At Table Level
In MySQL, a database may contain tables with different characters sets and collations than the database's character set and collation.
We can specify the default character set and collation at the while creating the table using the CREATE TABLE statement.
If we want to override default settings for existing table, we can use the ALTER TABLE statement.
Syntax
Following is the syntax for specifying default character set and collation for a table using the CREATE TABLE statement −
[CREATE | ALTER] TABLE table_name column_name datatype (length) CHARACTER SET character_set_name COLLATE collation_name
Example
In the following query, we are creating a table without any character set and collation. So, it uses the database's character set and collation.
CREATE TABLE CUSTOMERS( ID VARCHAR(45), NAME VARCHAR(45), AGE INT );
Now, we are using the ALTER TABLE statement to modify the character set as 'latin1' and collation as 'latin_german_ci'.
ALTER TABLE CUSTOMERS CHARACTER SET latin1 COLLATE latin1_german1_ci;
Displaying Default Collations
We can display all the default collations of character sets in MySQL database server using the SHOW CHARACTER SET query.
SHOW CHARACTER SET;
User-defined collations are deprecated in the latest versions of MySQL. Thus, the server issues a warning if they are used in any SQL statement.
A collation string for every character set starts with the character set name and ends with _ci (case insensitive), _cs(case sensitive) or _bin(binary).
The MySQL LIKE Clause
In MySQL, using the LIKE clause with the SHOW COLLATION statement, we can specify a pattern to fetch the names and other information of the collations that match the given pattern.
SHOW COLLATION LIKE 'greek%';
Output
The above query returns all the collations with the name greek in it.
Collation | Charset | Id | Default | Compiled | Sortlen |
---|---|---|---|---|---|
greek_bin | greek | 70 | Yes | 1 | |
greek_general_ci | greek | 25 | Yes | Yes | 1 |
The MySQL WHERE Clause
We can use the WHERE clause with the SHOW COLLATION statement to retrieve collation names that match the specified condition.
SHOW COLLATION WHERE Charset = 'cp1251';
Output
The above query returns all the collations where the charset id equal to 'cp1251'.
Collation | Charset | Id | Default | Compiled | Sortlen |
---|---|---|---|---|---|
cp1251_bin | cp1251 | 50 | Yes | 1 | |
cp1251_bulgarian_ci | cp1251 | 14 | Yes | 1 | |
cp1251_general_ci | cp1251 | 51 | Yes | Yes | 1 |
cp1251_general_cs | cp1251 | 52 | Yes | 1 | |
cp1251_ukrainian_ci | cp1251 | 23 | Yes | 1 |