Menu

MySQL LONGTEXT: Maximum Capacity and Transfer Limits

Updated on

MySQL LONGTEXT stores nonbinary text with a type limit of up to 4,294,967,295 bytes (4 GiB minus one byte). The maximum number of characters is lower when the column character set encodes characters using multiple bytes. The effective value size is also limited by available memory and the client/server protocol packet size, including max_allowed_packet. See MySQL’s string type syntax and BLOB and TEXT reference.

Define a LONGTEXT column

CREATE TABLE archived_documents (
    document_id BIGINT PRIMARY KEY,
    body LONGTEXT NOT NULL
);

Choose LONGTEXT only when the content can exceed the capacities of TEXT or MEDIUMTEXT. LONGTEXT is a character string with a character set and collation; use LONGBLOB when the column must store binary bytes instead. A regular B-tree index on LONGTEXT requires a prefix length; FULLTEXT indexes have different rules.

Can LONGTEXT have a DEFAULT value?

Yes, in MySQL 8.0.13 and later, if the storage engine supports expression defaults. For LONGTEXT, even a literal string default must be written as a parenthesized expression:

CREATE TABLE document_drafts (
    document_id BIGINT PRIMARY KEY,
    body LONGTEXT NOT NULL DEFAULT ('draft')
) ENGINE=InnoDB;

INSERT INTO document_drafts (document_id) VALUES (1);
SELECT body FROM document_drafts WHERE document_id = 1;
-- Returns: draft

DEFAULT 'draft' without parentheses is not valid for a LONGTEXT column. Before MySQL 8.0.13, TEXT-family columns could not have explicit defaults. Some storage engines do not support expression defaults, so check the server version and engine. See the MySQL data type default rules and the related TEXT example.

Because values can be very large, transferring or selecting them can consume substantial memory and packet capacity. Include a LONGTEXT column in query results only when it is needed, and ensure both the server and client are configured to handle the intended value size.