Check Character Sets and Collations in MariaDB / MySQL

Check and set the character set and collation setting in MariaDB / MySQL

Check the character set at the session or global level:

-- Check at local session level:
SHOW VARIABLES LIKE 'character_set\_%';
Variable_name           |Value  |
------------------------+-------+
character_set_client    |utf8mb4|
character_set_connection|utf8mb4|
character_set_database  |latin1 |
character_set_filesystem|binary |
character_set_results   |utf8mb4|
character_set_server    |latin1 |
character_set_system    |utf8mb3|

-- Check at global level:
Variable_name           |Value  |
------------------------+-------+
character_set_client    |latin1 |
character_set_connection|latin1 |
character_set_database  |latin1 |
character_set_filesystem|binary |
character_set_results   |latin1 |
character_set_server    |latin1 |
character_set_system    |utf8mb3|

Check the collation setting

SHOW  VARIABLES LIKE 'collation%';
Variable_name       |Value             |
--------------------+------------------+
collation_connection|utf8mb4_unicode_ci|
collation_database  |latin1_swedish_ci |
collation_server    |latin1_swedish_ci |

Alter the character set & collation at the Server level:

SET character_set_server = 'latin2';
SET collation_server = 'latin2_czech_cs';

Check and change the character set & collation at the Database level:

-- Check the collation setting at database level
SELECT * FROM INFORMATION_SCHEMA.SCHEMATA;
OR
SHOW CREATE DATABASE db_name;

--Change or set the collation Setting while creating the DB:
CREATE DATABASE db_name 
  CHARACTER SET = 'keybcs2'
  COLLATE = 'keybcs2_bin';

-- Alter the databae character set and Collate 
ALTER DATABASE test CHARACTER SET='utf8' COLLATE='utf8_bin';

Note: The parameters are character_Set_database and collation_database which can be set dynamically for setting the default database character set while creating.

Check & Change the collation/character set at the table level

-- Check the setting at table level:
SHOW CREATE TABLE table_name;
OR
select table_name,table_collation,table_Schema from information_schema.tables where table_name = 'test';

--Alter the table collation or character set:
ALTER TABLE table_name CHARACTER SET utf8 COLLATE utf8_bin;
OR 
ALTER TABLE test CONVERT TO CHARACTER SET utf8 COLLATE utf8_bin;

Check & change the character set and collate for the table column:

--Check for table column 
show full columns from table_name;
OR
select CHARACTER_SET_NAME ,COLLATION_NAME ,COLUMN_NAME  from information_schema.columns where table_name = 'test';

--Alter the column character set or collation name:
ALTER TABLE table_name MODIFY column_name DATA_TYPE CHARACTER SET UTF8 COLLATE utf8_bin;
Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.