Suppose if create a database with utf8 character set and created table and inserted data. Here my doubt is if the character is 1 byte then it will occupy only 1 byte or it will occupy 3 bytes because of utf8 character set


Depends on a character. Some need one byte, some - four. See http://www.fileformat.info/info/charset/UTF-8/list.htm . MySQL has two UFT8 character sets:

mysql> SHOW CHARACTER SET like 'UTF8%';
| Charset | Description   | Default collation  | Maxlen |
| utf8    | UTF-8 Unicode | utf8_general_ci    |      3 |
| utf8mb4 | UTF-8 Unicode | utf8mb4_general_ci |      4 |
2 rows in set (0.01 sec)

If a character is a one byte, it will use one byte in memory and on a disk. If four - then four. Plus the string will use one or two bytes to encode length of the string. Note, that utf8 character set cannot encode all UTF8 characters.

|improve this answer|||||

CHAR(n) -- Depending on the vintage of your copy of MySQL and the ROW_FORMAT, CHAR may allocate a full 3 bytes per character.

VARCHAR(n) -- This will occupy one or two bytes for a length, plus only as many bytes are needed. That is each character will occupy 1, 2, or 3 bytes for the CHARACTER SET utf8 (utf8mb3).

In general, you should go for utf8mb4, with a max of 4 bytes per character.

After you have inserted some text, do

       LENGTH(col),      -- number of bytes
       CHAR_LENGTH(col)  -- number of characters
    FROM ... WHERE ...;

This should let you see for yourself what is going on.

|improve this answer|||||

From MySql documentation:

Requires a maximum of four bytes per multibyte character.

Or, if your database character encoding is set as utf8mb3:

Requires a maximum of three bytes per multibyte character.

So, yes, an utf8 character will occupy the same size in your database as it would have. No more, no less.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.