I'm not certain. I tried to start out be reproducing your problem but the alter worked fine for me.
test > CREATE TABLE `bar` ( `content` text ) ENGINE=MyISAM DEFAULT CHARSET=latin1; INSERT INTO bar VALUES (0x8081828384858687898A8B8C8D8E8F909192939495969798999A9B9C9D9E9F);
Query OK, 0 rows affected (0.02 sec)
Query OK, 1 row affected (0.00 sec)
test > ALTER TABLE bar CHANGE content content TEXT CHARACTER SET UTF8;
Query OK, 1 row affected (0.04 sec)
Records: 1 Duplicates: 0 Warnings: 0
test > select * from bar;
+---------------------------------+
| content |
+---------------------------------+
| ����������������������������� |
+---------------------------------+
1 row in set (0.00 sec)
test > set names utf8;
Query OK, 0 rows affected (0.00 sec)
test > select * from bar;
+---------------------------------------------------------------------------------+
| content |
+---------------------------------------------------------------------------------+
| €‚ƒ„…†‡‰Š‹ŒŽ‘’“”•–—˜™š›œžŸ |
+---------------------------------------------------------------------------------+
1 row in set (0.00 sec)
Here's my related char settings
test > show variables like '%char%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
Edit
My char settings before running set names utf8
test > show variables like '%char%';
+--------------------------+----------------------------+
| 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 | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
Version
test > select version();
+-------------------------+
| version() |
+-------------------------+
| 5.1.41-3ubuntu12.10-log |
+-------------------------+
1 row in set (0.00 sec)
QUESTION #1
Why are there different levels of MySQL collation/charsets?
ANSWER TO QUESTION #1
There are two good reasons for different character sets and collations
Reason #1 : Disk Space
When you run this query
SELECT
maxlen,
GROUP_CONCAT(CHARACTER_SET_NAME) CharSets,
COUNT(1) CharSetCount
FROM information_schema.character_sets
GROUP BY maxlen\G
You get this:
mysql> SELECT
-> maxlen,
-> GROUP_CONCAT(CHARACTER_SET_NAME) CharSets,
-> COUNT(1) CharSetCount
-> FROM information_schema.character_sets
-> GROUP BY maxlen\G
*************************** 1. row ***************************
maxlen: 1
CharSets: cp1257,cp850,binary,koi8r,latin2,ascii,tis620,koi8u,greek,armscii8,keybcs2,macroman,latin7,cp1251,cp1256,dec8,hp8,geostd8,latin1,swe7,hebrew,cp1250,latin5,cp866,macce,cp852
CharSetCount: 26
*************************** 2. row ***************************
maxlen: 2
CharSets: big5,cp932,sjis,gbk,ucs2,euckr,gb2312
CharSetCount: 7
*************************** 3. row ***************************
maxlen: 3
CharSets: eucjpms,ujis,utf8
CharSetCount: 3
*************************** 4. row ***************************
maxlen: 4
CharSets: utf16,utf32,utf8mb4
CharSetCount: 3
4 rows in set (0.00 sec)
mysql>
Some character sets have a Maximum Length of 1 byte to represent a character. Other need more. Give this information, you may want to refrain from using the eucjpms, ujis, utf8, utf16, utf32, utf8mb4 character sets so that VARCHAR and TEXT data takes less space on disk.
Reason #2 : Internationalization
Characters Sets Each Come With One or More Collations to cover a variety of Languages
When you run this query
SELECT
A.CHARACTER_SET_NAME,
GROUP_CONCAT(COLLATION_NAME) Collations,
COUNT(1) CollationCount
FROM
information_schema.character_sets A
INNER JOIN information_schema.collations B
USING (CHARACTER_SET_NAME)
GROUP BY A.CHARACTER_SET_NAME\G
You will see that some Characters Sets have with multiple collations for Different Parts of Europe. Chinese, Japanese, Greek, and parts of Asia Minor and Scandinavia are also available.
QUESTION #2
Should you always ensure your PHP connection matches the charset of the database you're working on?
ANSWER TO QUESTION #2
SCENARIO
You are driving at 3:00 AM. You are the only driver on the road. You come to an intersection. You have the red light.
Question : Do you stop or go through the red light?
Answer : Depends on the neighborhood
- Safe neighborhood ?
- Some abide by the law, stop at the red, and wait for green.
- Some chance it and go through
- Bad neighborhood or new to the area ?
- Some abide by the law, stop at the red, and wait for green AT THE RISK OF A CARJACKING
- Some chance it and go through to AVOID OR REDUCE RISK OF A CARJACKING
- Assume the worst and find another route
How does this apply?
You should err on the side of caution. You should always check the charset beforehand because you do not know the neighborhood (client program, internet browser) the PHP connection will be entering and if there is a risk of a carjacking (putting invalid data into the database, requesting too much data for retrieval).
QUESTION #3
If you can have different tables that use different character sets do you just use SET NAMES or mysql(i)_set_charset to switch?
ANSWER TO QUESTION #3
By all means
QUESTION #4
If you have a table that has multiple charsets how do you manage that since the connection can only use one charset at a time?
ANSWER TO QUESTION #4
You may have to shift character sets with the DB Session. Here are the settings that can be changed at the session level:
Please set these carefully before reading from and writing to the database. It would also be wise to store the character set name and collation in the same table you will be accessing.
Best Answer
MySQL and MariaDB handle character sets the same. However... Different versions of MySQL have different defaults. Ditto for MariaDB.
SET NAMES
declares what encoding is in the client. This is independent of theCHARACTER SET
on a column or table. (There are, of course, characters that are not representable in latin1, so there can be conversion problems.) As you say, the conversion occurs onINSERT
orSELECT
.To further pursue this, please tell us what client you are using, what connection parameters were used, any SETs performed, and what happened.
For diagnosing what is stored, use
SELECT col, HEX(col)...
. More details.