A Chinese character occupies a few bytes in the database

Source: Internet
Author: User
Tags mysql version

The ORACLE10G database table field in the project is a varchar (n) type, which is simple to keep in English, as long as the number of letters is not greater than N. But for Chinese characters, the same approach is not. Because of the different character sets for Chinese characters, the bytes consumed in the database are not the same. UTF-8 character Set, a Chinese character accounted for three bytes, GBK character set, a Chinese character accounted for two bytes, such as varchar (10) Type of field, UTF-8 Chinese characters, can only save 3, GBK character set can save 5 characters.
So in the program according to the size of the table field varchar, save or update to make the necessary checks or the background will be error.
Methods: 1, using the table maxlength attributes, such as maxlength= "5", that is, only 5 characters can be saved.

MySQL version above 5.0:

1. How much length of a Chinese character is related to coding:

UTF-8: One kanji = 3 bytes

GBK: one kanji = 2 bytes

2.varchar (n) represents n characters, both Chinese and English, MySQL can be stored in n characters, only the actual byte length is different

3.MySQL check length, available in SQL language:

Select LENGTH (fieldname) from TableName to view

A Chinese character occupies a few bytes in the database

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.