SQL reference (MySQL and MariaDB dialects)

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#

SQL
CHAR [(length)]
VARCHAR (length)
TINYTEXT | TEXT | MEDIUMTEXT | LONGTEXT
    [CHARACTER SET name | CHARSET name] [COLLATE name]

Arguments and limits#

TypeLengthCounted in
CHAR(n)n characters, default 1characters
VARCHAR(n)n characters, required (a VARCHAR without a length is an error)characters
TINYTEXT255bytes
TEXT65,535bytes
MEDIUMTEXT16,777,215bytes
LONGTEXT4,294,967,295 as the type says; the practical limit is what Raft DB stores in one fieldbytes

Remarks#

  • Every text is utf8mb4. CHARACTER SET x, CHARSET x and COLLATE x after a text type are read and ignored; a national type (NCHAR, NVARCHAR, NATIONAL CHAR) is utf8mb4 too. LENGTH counts bytes, CHAR_LENGTH characters.
  • 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 example INSERT INTO types (c) VALUES ('toolongvalue') into a CHAR(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 CHAR or VARCHAR column up to 255 characters is used for = and IN through 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, VARCHARACTER and VARCHAR2 are VARCHAR. NCHAR, NATIONAL CHAR and CHARACTER are CHAR. LONG, LONG VARCHAR and LONG CHAR VARYING are MEDIUMTEXT. CLOB is LONGTEXT. 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 VARCHAR as long as the most it can produce (CONCAT the sum of its arguments, LEFT(s, n) the smaller of the two, LPAD, REPEAT, DATE_FORMAT by the format), which is what CREATE TABLE ... AS SELECT uses.
  • SHOW CREATE TABLE. The MariaDB dialect writes DEFAULT NULL on a nullable TEXT column where the MySQL dialect writes nothing.

Examples#

SQL
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

See also#