Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Discover collation of a MySQL column

I previously created a MySQL table and now I want to find out what collation some of the fields are using. What SQL or MySQL commands can I use to discover this?

like image 622
Trindaz Avatar asked Oct 01 '11 00:10

Trindaz


People also ask

How do you find the collation of a column?

To view a collation setting for a column in Object ExplorerExpand Databases, expand the database and then expand Tables. Expand the table that contains the column and then expand Columns. Right-click the column and select Properties. If the collation property is empty, the column is not a character data type.

How do I find MySQL database collation?

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 SQL column collation?

SQL Server collation refers to a set of character and character encoding rules, and influences how information is stored according to the order in the data page, how data is matched by comparing two columns, and how information is arranged in the T-SQL query statement.


2 Answers

You could use SHOW FULL COLUMNS FROM tablename which returns a column Collation, for example for a table 'accounts' with a special collation on the column 'name'

mysql> SHOW FULL COLUMNS FROM accounts; +----------+--------------+-------------------+------+-----+---------+----------+ | Field    | Type         | Collation         | Null | Key | Default | Extra    | +----------+--------------+-------------------+------+-----+---------+----------| | id       | int(11)      | NULL              | NO   | PRI | NULL    | auto_inc | | name     | varchar(255) | utf8_bin          | YES  |     | NULL    |          | | email    | varchar(255) | latin1_swedish_ci | YES  |     | NULL    |          | ... 

Or you could use SHOW CREATE TABLE tablename which will result in a statement like

mysql> SHOW CREATE TABLE accounts; CREATE TABLE `accounts` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL, `email` varchar(255) DEFAULT NULL, ... 
like image 176
0x4a6f4672 Avatar answered Oct 21 '22 06:10

0x4a6f4672


If you want the collation for just that specific column (for possible use with a subquery)...

SELECT COLLATION_NAME FROM information_schema.columns WHERE TABLE_SCHEMA = 'tableschemaname' AND TABLE_NAME = 'tablename' AND COLUMN_NAME = 'fieldname'; 
like image 44
HoldOffHunger Avatar answered Oct 21 '22 06:10

HoldOffHunger