ENCLOSED (usually written as ENCLOSED BY) is part of the FIELDS clause used with LOAD DATA INFILE and SELECT ... INTO OUTFILE in MySQL and MariaDB. It lets you specify a single-character string that surrounds every field (or, with the OPTIONALLY modifier, only character, VARCHAR, or text fields) in the input or output file. This is vital when working with CSV files where values may contain the field delimiter itself; enclosing each value in a quote character preserves data integrity.Behavior- If OPTIONALLY is omitted, every field (numeric or text) is wrapped by the given character on export, and the same character is expected on import.- With OPTIONALLY ENCLOSED BY, only string fields are quoted; numeric fields are written without quotes. The loader still accepts quotes around numeric fields but does not require them.- The enclosure character must be exactly one byte long. Multi-byte strings cause an error.- ENCLOSED BY can be combined with TERMINATED BY, ESCAPED BY, and LINES TERMINATED BY to fully describe the file format.Caveats- MySQL treats NULL as an unquoted \N token, even when ENCLOSED BY is in use.- If the enclosure character appears inside a value, it must be escaped using the ESCAPED BY character (default backslash) or doubled if ESCAPED BY ''.- Other RDBMSs (PostgreSQL COPY, SQL Server BULK INSERT, Oracle SQL*Loader) use different keywords; ENCLOSED is MySQL-specific.
- ENCLOSED BY STRING
(length 1) - Character used to wrap each field.- OPTIONALLY
(KEYWORD) - Quotes only string columns when present.- TERMINATED BY
(STRING) - Delimiter between fields (works with ENCLOSED BY).- ESCAPED BY STRING
(length 1) - Escape character for enclosure or delimiters.LOAD DATA INFILE, SELECT INTO OUTFILE, FIELDS TERMINATED BY, OPTIONALLY, ESCAPED BY, LINES TERMINATED BY, COPY (PostgreSQL)
MySQL 3.23
Any single-byte character is allowed, but double quotes ('"') and single quotes ('\'') are the most common.
Use the ESCAPED BY character (default backslash) before the quote, or double the quote if ESCAPED BY '' is set.
No. ENCLOSED BY is meaningful only when you also use FIELDS TERMINATED BY to create delimited, variable-width files.
While not mandatory, quoting fields is recommended for maximum compatibility, especially when data may contain delimiter characters.