The mysqldump
command is used to create backups of MySQL databases. One of its useful options is --fields-optionally-enclosed-by=name
, which allows fields in the output to be optionally enclosed by a specified character or string. This can be particularly handy when dealing with data that contains special characters or needs to be formatted in a specific way for import into another system.
Here are several examples demonstrating how to use --fields-optionally-enclosed-by
:
Example 1: mysqldump --fields-optionally-enclosed-by='"' dbname tablename > output.sql
This command dumps the table tablename
from database dbname
into output.sql
, with fields optionally enclosed by double quotes ("
).
Example 2: mysqldump --fields-optionally-enclosed-by='' dbname tablename > output.sql
Here, fields in the dump will be optionally enclosed by empty strings, which means no enclosing characters are used.
Example 3: mysqldump --fields-optionally-enclosed-by='`' --tab=/path/to/output dbname tablename
This command generates tab-separated output files for each table in the specified database, with fields optionally enclosed by backticks (`
).
Example 4: mysqldump --fields-optionally-enclosed-by='|' -u username -p password dbname tablename | gzip > output.sql.gz
In this case, fields are optionally enclosed by a vertical bar (|
), and the output is piped through gzip compression.
Example 5: mysqldump --fields-optionally-enclosed-by="\"" --fields-terminated-by=, dbname tablename | tee output.csv
This command exports data from MySQL to a CSV file (output.csv
), with fields optionally enclosed by double quotes ("
) and terminated by commas (,
).
To verify whether the --fields-optionally-enclosed-by
option is applied correctly, you can open the resulting SQL or CSV file in a text editor or use command-line tools to inspect the structure. Look for the specified enclosing characters around fields that require them, ensuring the output matches your expectations for formatting and data integrity.
Also check similar articles.
Enclosing Fields in mysqldump Output
Setting Field Terminator in mysqldump Output
Using Extended INSERT Syntax in mysqldump
Dumping Events Using mysqldump
Dumping Slave Position in mysqldump Output (Deprecated)
Discussion about this post