Char and varchar types are similar, but they are stored and retrieved in different ways. In MySQL5.0.3, their maximum length and whether to retain trailing spaces are also different. Char and varchar define the maximum length of a string. For example, char (30) can contain 30 characters. The length of the char column is the fixed length you define when creating a table. The value range is 0.
Char and varchar types are similar, but they are stored and retrieved in different ways. In MySQL5.0.3, their maximum length and whether to retain trailing spaces are also different. Char and varchar define the maximum length of a string. For example, char (30) can contain 30 characters. The length of the char column is the fixed length you define when creating a table. The value range is 0.
Char and varchar types are similar, but they are stored and retrieved in different ways. In MySQL5.0.3, their maximum length and whether to retain trailing spaces are also different.
Char and varchar define the maximum length of a string. For example, char (30) can contain 30 characters.
The length of the char column is the fixed length you define when creating a table. The value range is 0 ~ 255. When the char value is stored, the right part is filled with spaces to the specified length. When retrieving char, the trailing space is automatically removed.
The varchar column is variable-length. The length that can be defined before MySQL5.0.3 is 0 ~ 255, which can be defined as 0 ~ 65535. The valid length of the varchar column in MySQL 5.0.3 and later versions is limited by the maximum value of the row and the characters used.
Compare the Data Length of char and varchar with the prefix as one byte or two bytes. The length of this prefix indicates the number of bytes of the value. If the value does not exceed 255, this column uses a byte length. If the value may exceed 255 bytes, the length of two bytes is used.
If the strict SQL mode is not enabled, If you allocate a char or varchar column that exceeds the maximum length of the column, this value will be truncated to fill the column, in this case, a warning is generated. For truncated non-space characters, you can set a strict SQL mode to generate an error.
For varchar columns, when SQL mode is enabled, a large number of trailing spaces are truncated before insertion, generating a warning. For char columns, no matter whether the SQL mode is enabled or not, excessive trailing spaces of inserted values are removed silently.
Varchar values are not filled during storage. How to handle trailing spaces depends on the version. In MySQL5.0.3, when the value is stored and retrieved, trailing spaces are retained, which is consistent with standard SQL. Before MySQL5.0.3, when the value is saved to the varchar column, spaces at the end are removed. This means that these spaces do not exist during value retrieval.
Before MySQL5.0.3, if you need a data type that does not remove trailing spaces, consider the BLOB or TEXT type. Similarly, when you store encrypted or compressed data, you also need to use BLOB columns instead of char or varchar columns to avoid potential problems of trailing spaces when changing values.
The following table describes the differences between char and varchar storage methods and display results. (Assume that the column uses very simple characters and latin1 encoding)