- MySQL 5.1 Reference Manual :: 10 Data Types :: 10.4 String Types :: 10.4.1 The CHAR and VARCHAR Types
-
- MySQL 5.1 Reference Manual
- Preface, Notes, Licenses
- 1 General Information
- 2 Installing and Upgrading MySQL
- 3 Tutorial
- 4 MySQL Programs
- 5 MySQL Server Administration
- 6 Backup and Recovery
- 7 Optimization
- 8 Language Structure
- 9 Internationalization and Localization
- 10 Data Types
- 11 Functions and Operators
- 12 SQL Statement Syntax
- 13 Storage Engines
- 14 High Availability and Scalability
- 15 MySQL Enterprise Monitor
- 16 Replication
- 17 MySQL Cluster NDB 6.X/7.X
- 18 Partitioning
- 19 Stored Programs and Views
- 20 INFORMATION_SCHEMA Tables
- 21 Connectors and APIs
- 22 Extending MySQL
- A MySQL 5.1 Frequently Asked Questions
- B Errors, Error Codes, and Common Problems
- C MySQL Change History
- D Restrictions and Limits
- Index
- Standard Index
- C Function Index
- Command Index
- Function Index
- INFORMATION_SCHEMA Index
- Transaction Isolation Level Index
- JOIN Types Index
- Operator Index
- Option Index
- Privileges Index
- SQL Modes Index
- Status Variable Index
- Statement/Syntax Index
- System Variable Index
The
CHAR
andVARCHAR
types are similar, but differ in the way they are stored and retrieved. They also differ in maximum length and in whether trailing spaces are retained.The
CHAR
andVARCHAR
types are declared with a length that indicates the maximum number of characters you want to store. For example,CHAR(30)
can hold up to 30 characters.The length of a
CHAR
column is fixed to the length that you declare when you create the table. The length can be any value from 0 to 255. WhenCHAR
values are stored, they are right-padded with spaces to the specified length. WhenCHAR
values are retrieved, trailing spaces are removed unless thePAD_CHAR_TO_FULL_LENGTH
SQL mode is enabled.Values in
VARCHAR
columns are variable-length strings. The length can be specified as a value from 0 to 65,535. The effective maximum length of aVARCHAR
is subject to the maximum row size (65,535 bytes, which is shared among all columns) and the character set used.In contrast to
CHAR
,VARCHAR
values are stored as a one-byte or two-byte length prefix plus data. The length prefix indicates the number of bytes in the value. A column uses one length byte if values require no more than 255 bytes, two length bytes if values may require more than 255 bytes.If strict SQL mode is not enabled and you assign a value to a
CHAR
orVARCHAR
column that exceeds the column's maximum length, the value is truncated to fit and a warning is generated. For truncation of nonspace characters, you can cause an error to occur (rather than a warning) and suppress insertion of the value by using strict SQL mode. See Section 5.1.7, “Server SQL Modes”.For
VARCHAR
columns, trailing spaces in excess of the column length are truncated prior to insertion and a warning is generated, regardless of the SQL mode in use. ForCHAR
columns, truncation of excess trailing spaces from inserted values is performed silently regardless of the SQL mode.VARCHAR
values are not padded when they are stored. Trailing spaces are retained when values are stored and retrieved, in conformance with standard SQL.The following table illustrates the differences between
CHAR
andVARCHAR
by showing the result of storing various string values intoCHAR(4)
andVARCHAR(4)
columns (assuming that the column uses a single-byte character set such aslatin1
).Value CHAR(4)
Storage Required VARCHAR(4)
Storage Required ''
' '
4 bytes ''
1 byte 'ab'
'ab '
4 bytes 'ab'
3 bytes 'abcd'
'abcd'
4 bytes 'abcd'
5 bytes 'abcdefgh'
'abcd'
4 bytes 'abcd'
5 bytes The values shown as stored in the last row of the table apply only when not using strict mode; if MySQL is running in strict mode, values that exceed the column length are not stored, and an error results.
If a given value is stored into the
CHAR(4)
andVARCHAR(4)
columns, the values retrieved from the columns are not always the same because trailing spaces are removed fromCHAR
columns upon retrieval. The following example illustrates this difference:mysql>
CREATE TABLE vc (v VARCHAR(4), c CHAR(4));
Query OK, 0 rows affected (0.01 sec) mysql>INSERT INTO vc VALUES ('ab ', 'ab ');
Query OK, 1 row affected (0.00 sec) mysql>SELECT CONCAT('(', v, ')'), CONCAT('(', c, ')') FROM vc;
+---------------------+---------------------+ | CONCAT('(', v, ')') | CONCAT('(', c, ')') | +---------------------+---------------------+ | (ab ) | (ab) | +---------------------+---------------------+ 1 row in set (0.06 sec)Values in
CHAR
andVARCHAR
columns are sorted and compared according to the character set collation assigned to the column.All MySQL collations are of type
PADSPACE
. This means that allCHAR
andVARCHAR
values in MySQL are compared without regard to any trailing spaces. For example:mysql>
CREATE TABLE names (myname CHAR(10), yourname VARCHAR(10));
Query OK, 0 rows affected (0.09 sec) mysql>INSERT INTO names VALUES ('Monty ', 'Monty ');
Query OK, 1 row affected (0.00 sec) mysql>SELECT myname = 'Monty ', yourname = 'Monty ' FROM names;
+--------------------+----------------------+ | myname = 'Monty ' | yourname = 'Monty ' | +--------------------+----------------------+ | 1 | 1 | +--------------------+----------------------+ 1 row in set (0.00 sec)This is true for all MySQL versions, and is not affected by the server SQL mode.
Note
For more information about MySQL character sets and collations, see Section 9.1, “Character Set Support”.
For those cases where trailing pad characters are stripped or comparisons ignore them, if a column has an index that requires unique values, inserting into the column values that differ only in number of trailing pad characters will result in a duplicate-key error. For example, if a table contains
'a'
, an attempt to store'a '
causes a duplicate-key error.