Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store unicode in MySQL?

Tags:

mysql

unicode

How do I store Unicode in free edition of MySQL?

There doesn't seem to be nvarchar type as in SQL Server. Is Unicode not supported in MySQL? I tried using text but that too is not working.

like image 688
TCM Avatar asked Jul 25 '10 11:07

TCM


People also ask

Does MySQL support Unicode?

MySQL supports multiple Unicode character sets: utf8mb4 : A UTF-8 encoding of the Unicode character set using one to four bytes per character. utf8mb3 : A UTF-8 encoding of the Unicode character set using one to three bytes per character. utf8 : An alias for utf8mb3 .

Can varchar store Unicode characters?

Varchar uses Windows-1252 character encoding, which is for all practical purposes standard ASCII. As others have noted, nvarchar allows the storage of unicode characters.

How do I set MySQL database to UTF-8?

To change the character set encoding to UTF-8 for the database itself, type the following command at the mysql> prompt. Replace dbname with the database name: Copy ALTER DATABASE dbname CHARACTER SET utf8 COLLATE utf8_general_ci; To exit the mysql program, type \q at the mysql> prompt.


2 Answers

You need to choose a utf8_* character set for your table. Text and memo fields will then automatically be stored in UTF-8. Support for UTF-16 is coming in mySQL 6.

like image 99
Pekka Avatar answered Nov 08 '22 01:11

Pekka


The character set for a given string column (CHAR, VARCHAR or *TEXT) is determined by its character set and/or collation. This is a quite intense topic so it's best to read the documentation I linked. For example:

CREATE TABLE t1 (     col1 CHAR(10) CHARACTER SET utf8 COLLATE utf8_unicode_ci ) 

will create a table t1 with a col1 that stores its content in UTF-8 encoding.

like image 27
Stefan Gehrig Avatar answered Nov 08 '22 03:11

Stefan Gehrig