Character types
CHAR, VARCHAR and the TEXT family, their length limits, the type aliases (NVARCHAR, LONG and others) and how character sets are handled.
Syntax#
CHAR [(length)]
VARCHAR (length)
TINYTEXT | TEXT | MEDIUMTEXT | LONGTEXT
[CHARACTER SET name | CHARSET name] [COLLATE name]Arguments and limits#
| Type | Length | Counted in |
|---|---|---|
CHAR(n) | n characters, default 1 | characters |
VARCHAR(n) | n characters, required (a VARCHAR without a length is an error) | characters |
TINYTEXT | 255 | bytes |
TEXT | 65,535 | bytes |
MEDIUMTEXT | 16,777,215 | bytes |
LONGTEXT | 4,294,967,295 as the type says; the practical limit is what Raft DB stores in one field | bytes |
Remarks#
- Every text is
utf8mb4.CHARACTER SET x,CHARSET xandCOLLATE xafter a text type are read and ignored; a national type (NCHAR,NVARCHAR,NATIONAL CHAR) isutf8mb4too.LENGTHcounts bytes,CHAR_LENGTHcharacters. - Too long. In strict mode a text longer than the column is error 1406 (
The value for column 'c' (row 1) is too long); without strict mode it is cut to the length and a warning is raised (for exampleINSERT INTO types (c) VALUES ('toolongvalue')into aCHAR(5)). - Numbers into text. A number written into a text column is stored as its text.
- Comparison. Texts are compared and sorted without regard to case; see Collation and case sensitivity.
- Indexes. An index on a
CHARorVARCHARcolumn up to 255 characters is used for=andINthrough a hidden lower-case copy of the value, so an indexed lookup agrees with the comparison. A prefix length in an index definition (KEY k (c(10))) is kept. - Aliases.
NATIONAL VARCHAR,NVARCHAR,CHAR VARYING,CHARACTER VARYING,VARCHARACTERandVARCHAR2areVARCHAR.NCHAR,NATIONAL CHARandCHARACTERareCHAR.LONG,LONG VARCHARandLONG CHAR VARYINGareMEDIUMTEXT.CLOBisLONGTEXT. The names that come from MariaDB (LONG ...,CHAR VARYING,VARCHARACTER,VARCHAR2,CLOB) are read in both dialects. - Result of string functions. A string function makes a
VARCHARas long as the most it can produce (CONCATthe sum of its arguments,LEFT(s, n)the smaller of the two,LPAD,REPEAT,DATE_FORMATby the format), which is whatCREATE TABLE ... AS SELECTuses. SHOW CREATE TABLE. The MariaDB dialect writesDEFAULT NULLon a nullableTEXTcolumn where the MySQL dialect writes nothing.
Examples#
CREATE TABLE a (l1 LONG, l2 LONG VARCHAR, vc VARCHARACTER(8), cv CHAR VARYING(9), ch2 CHARACTER VARYING(3));
SHOW CREATE TABLE a; -- shows mediumtext, mediumtext, varchar(8), varchar(9), varchar(3)
CREATE TABLE words (w VARCHAR(20) PRIMARY KEY, n INT);
INSERT INTO words VALUES ('banana', 3), ('Apple', 1);
SELECT w FROM words WHERE w = 'APPLE'; -- finds Apple