Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysql query to discover current default database collation (via command line client)

I can't figure out which is the query to run in order to see the current collation set for a mysql database.

I know I can change it running a query like this:

ALTER DATABASE db_name
[[DEFAULT] CHARACTER SET charset_name]
[[DEFAULT] COLLATE collation_name]

but I don't want to do it without knowing the current value.

I think it's actually possible because in phpmyadmin you can see the current value in the operation tab of your database, but I need to do it via the command line client and after googling a little bit I found no answers.

Probably I will open phpmyadmin code to discover it.. before doing it, does anybody know which is the right query?

Thanks in advance.

like image 705
lucke84 Avatar asked Aug 24 '12 09:08

lucke84


People also ask

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 the command to check the current DB in use?

mysql> show databases; Here is the output that displays all the databases. As you can see above, we have both databases, and we can get the current database name with the help of DATABASE() method.

What is the default collation for MySQL?

The default MySQL server character set and collation are latin1 and latin1_swedish_ci , but you can specify character sets at the server, database, table, column, and string literal levels.

How do I run MySQL client from command line?

Launch the MySQL Command-Line Client. To launch the client, enter the following command in a Command Prompt window: mysql -u root -p . The -p option is needed only if a root password is defined for MySQL. Enter the password when prompted.


1 Answers

Figured out by myself, looking at phpmyadmin code.

Depending on mysql version, the query to run is this:

SELECT DEFAULT_COLLATION_NAME FROM data_dictionary.SCHEMAS WHERE SCHEMA_NAME = 'nameOfMyDb' LIMIT 1;

or

SELECT DEFAULT_COLLATION_NAME FROM information_schema.SCHEMATA WHERE SCHEMA_NAME = 'nameOfMyDb' LIMIT 1;
like image 187
lucke84 Avatar answered Nov 15 '22 20:11

lucke84