Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql check collation of a table

People also ask

How do you find the collation of a table?

To view the collation setting of a databaseIn Object Explorer, connect to an instance of the Database Engine and on the toolbar, click New Query. In the query window, enter the following statement that uses the sys. databases system catalog view. SELECT name, collation_name FROM sys.

How do I see table collations in MySQL?

To see the default character set and collation for a given database, use these statements: USE db_name; SELECT @@character_set_database, @@collation_database; Alternatively, to display the values without changing the default database: SELECT DEFAULT_CHARACTER_SET_NAME, DEFAULT_COLLATION_NAME FROM INFORMATION_SCHEMA.

What is MySQL table collation?

A collation is a set of rules that defines how to compare and sort character strings. Each collation in MySQL belongs to a single character set. Every character set has at least one collation, and most have two or more collations. A collation orders characters based on weights.

How do I change the table collation in MySQL?

Command Line Run the following command to change the character set and collation of your table: ALTER TABLE tablename CHARACTER SET utf8 COLLATE utf8_general_ci; For either of these examples, please replace the example character set and collation with your desired values.


SHOW TABLE STATUS shows information about a table, including the collation.

For example SHOW TABLE STATUS where name like 'TABLE_NAME'


The above answer is great, but it doesn't actually provide an example that saves the user from having to look up the syntax:

show table status like 'test';

Where test is the table name.

(Corrected as per comments below.)


Checking the collation of a specific table

You can query INFORMATION_SCHEMA.TABLES and get the collation for a specific table:

SELECT TABLE_SCHEMA
    , TABLE_NAME
    , TABLE_COLLATION 
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME = 't_name';

that gives a much more readable output in contrast to SHOW TABLE STATUS that contains a lot of irrelevant information.


Checking the collation of columns

Note that collation can also be applied to columns (which might have a different collation than the table itself). To fetch the columns' collation for a particular table, you can query INFORMATION_SCHEMA.COLUMNS:

SELECT TABLE_SCHEMA 
    , TABLE_NAME 
    , COLUMN_NAME 
    , COLLATION_NAME 
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 't_name';

For more details you can refer to the article How to Check and Change the Collation of MySQL Tables


Use this query:

SHOW CREATE TABLE tablename

You will get all information related to table.


Check collation of the whole database

If someone is looking here also for a way to check collation on the whole database:

  1. use mydatabase; (where mydatabase is the name of the database you're going to check)
  2. SELECT @@character_set_database, @@collation_database;

You should see the result like:

+--------------------------+----------------------+
| @@character_set_database | @@collation_database |
+--------------------------+----------------------+
| utf8mb4                  | utf8mb4_unicode_ci   |
+--------------------------+----------------------+
1 row in set (0.00 sec)