Bug #2151 USE db with non-default character set affects further CREATE DATABASEs
Submitted: 18 Dec 2003 2:44 Modified: 19 Dec 2003 7:20
Reporter: Alexander Barkov Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server Severity:S3 (Non-critical)
Version:4.1.x OS:Any (any)
Assigned to: Alexander Barkov CPU Architecture:Any

[18 Dec 2003 2:44] Alexander Barkov
Description:
Why does character_set_database apply on the newly created database?

mysql> show variables like "%character_set%";
+--------------------------+------------------------------------------------+
| Variable_name            | Value                                          |
+--------------------------+------------------------------------------------+
| character_set_server     | latin1                                         |
| character_set_system     | utf8                                           |
| character_set_database   | latin1                                         |
| character_set_client     | latin1                                         |
| character_set_connection | latin1                                         |
| character-sets-dir       | /usr/local/mysql-st-4.1.1/share/mysql/charsets |
| character_set_results    | latin1                                         |
+--------------------------+------------------------------------------------+
7 rows in set (0.02 sec)

mysql> create database db1 default character set cp1251;
Query OK, 1 row affected (0.04 sec)

mysql> show create database db1;
+----------+----------------------------------------------------------------+
| Database | Create Database                                                |
+----------+----------------------------------------------------------------+
| db1      | CREATE DATABASE `db1` /*!40100 DEFAULT CHARACTER SET cp1251 */ |
+----------+----------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> use db1;
Database changed
mysql> create database db2;
Query OK, 1 row affected (0.00 sec)

mysql> show create database db2;
+----------+----------------------------------------------------------------+
| Database | Create Database                                                |
+----------+----------------------------------------------------------------+
| db2      | CREATE DATABASE `db2` /*!40100 DEFAULT CHARACTER SET cp1251 */ |
+----------+----------------------------------------------------------------+
1 row in set (0.00 sec)

But according MySQL manual character set of the db2 should be latin1:

MySQL chooses the database character set and database collation thus:

- If both CHARACTER SET X and COLLATE Y were specified, then character set X and collation Y.
- If CHARACTER SET X was specified without COLLATE, then character set X and its default collation.
- Otherwise, the server character set and server collation.

How to repeat:
Imagine, server is running latin1 by default.

create database db1 default character set cp1251;
use db1;
create database db2;
show create database db2;
[19 Dec 2003 7:20] Alexander Barkov
Thank you for your bug report. This issue has been committed to our
source repository of that product and will be incorporated into the
next release.

If necessary, you can access the source repository and build the latest
available version, including the bugfix, yourself. More information 
about accessing the source trees is available at
    http://www.mysql.com/doc/en/Installing_source_tree.html

Additional info:

Thanks Victoria for finding this issue.