Home >Database >Mysql Tutorial >Why not use UTF-8 encoding in MySQL

Why not use UTF-8 encoding in MySQL

步履不停
步履不停Original
2019-06-19 14:01:042494browse

Why not use UTF-8 encoding in MySQL

##MySQL UTF-8 encoding

MySQL has supported UTF-8 since version 4.1, that is, in 2003. However, the currently popular UTF-8 standard (RFC 3629) was stipulated after that. Because of this, UTF-8 in MySQL is inconsistent with UTF-8 in our daily development, which has caused some problems. MySQL's UTF-8 only supports up to three bytes per character, while true UTF-8 is up to four bytes per character.

Reproduction of the problem

There are database tables as follows: utf8 encoding method

下载 (2).jpg##Go to a record in the database:

@Test public void testInsert() {     User user = new User();     user.setUsername("\uD83D\uDE00 ");     user.setPassword("123456");     userRepo.save(user); }

This is just part of the code. It doesn’t matter if you don’t understand it. This is to insert a record into the user table. Where username is \uD83D\uDE00.

Actually \uD83D\uDE00 is an emoji expression.

Why not use UTF-8 encoding in MySQLBecause the utf8 character set in MySQL only supports the Unicode range of three-byte UTF-8 encoding, and emoji characters belong to the four-byte encoding part, so the program is expected to run Reported an error. Run this code:

Why not use UTF-8 encoding in MySQL# As expected, an error is reported.

Solve the problem

Although MySQL's UTF-8 is defective, MySQL (including mariadb) officially did not fix this bug. Instead, it supported the real version through "utf8mb4" re-released in 2010. UTF-8. Therefore, if you want to solve this problem, you can only set the MySQL database to the utf8mb4 character set.

Summary

This problem was also discovered because an emoji expression was saved when saving data. In fact, when I first started using MySQL, I discovered utf8mb4, but I didn't understand the difference between UTF8 and UTF8MB4. After this lesson, when using MySQL in the future, just honestly set the character set to utf8mb4.

For more MySQL related technical articles, please visit the

MySQL Tutorial column to learn!

The above is the detailed content of Why not use UTF-8 encoding in MySQL. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn