OPTIONALLY is a MySQL keyword that appears in the FIELDS clause of LOAD DATA INFILE and SELECT ... INTO OUTFILE statements. When you write FIELDS OPTIONALLY ENCLOSED BY '"', the server treats the enclosing character as optional: - During import (LOAD DATA INFILE) only values for character columns (CHAR, VARCHAR, TEXT, etc.) may be quoted; numeric and other non-string columns do not need the quotes.- During export (SELECT ... INTO OUTFILE) MySQL surrounds only character columns with the specified quote character. Non-string columns are written without quotes, producing smaller, cleaner files.Without OPTIONALLY, ENCLOSED BY forces every column value—regardless of data type—to be wrapped in the quote character. OPTIONALLY therefore offers more flexible CSV handling, especially when you need compatibility with tools like Excel, pandas, or other databases that expect numbers to appear unquoted.Caveats:- OPTIONALLY works only in MySQL and MariaDB. Other systems either ignore it or raise an error.- The keyword must precede ENCLOSED BY. Placing it after ENCLOSED BY is a syntax error.- It has no effect if ENCLOSED BY is omitted.
LOAD DATA INFILE, SELECT INTO OUTFILE, ENCLOSED BY, TERMINATED BY, ESCAPED BY, CSV import, CSV export
MySQL 3.23
It signals that only text columns may be quoted with the specified character during import or export. Numeric columns remain unquoted, creating cleaner CSV files.
Only inside the FIELDS clause of LOAD DATA INFILE and SELECT ... INTO OUTFILE in MySQL or MariaDB.
MySQL will quote every column value when exporting, and will expect every column value to be quoted when importing.
Yes, slightly. Exporting fewer quote characters reduces file size, which can improve IO performance when moving large datasets.