The mysqldump
command is used to create backups of MySQL databases. One useful option is --add-drop-table
, which includes DROP TABLE statements in the dump file. This option is particularly handy when you want to ensure that existing tables are dropped before recreating them with the dump.
Here are several examples demonstrating the use of --add-drop-table
:
Example 1: Dump a single database with DROP TABLE statements:
mysqldump --add-drop-table mydatabase > mydatabase_backup.sql
This command dumps the database mydatabase
into mydatabase_backup.sql
file, including DROP TABLE statements for each table.
Example 2: Dump all databases on the server with DROP TABLE statements:
mysqldump --add-drop-table --all-databases > alldatabases_backup.sql
This command dumps all databases on the MySQL server into alldatabases_backup.sql
, ensuring that DROP TABLE statements are included.
Example 3: Dump a database and compress the output:
mysqldump --add-drop-table mydatabase | gzip > mydatabase_backup.sql.gz
Here, the output is piped to gzip
to compress it, but the DROP TABLE statements are still included in the compressed file.
Example 4: Include extended insert statements with DROP TABLEs:
mysqldump --add-drop-table --extended-insert mydatabase > mydatabase_backup.sql
This command uses extended inserts and includes DROP TABLE statements for mydatabase
.
Example 5: Dump a specific table with DROP TABLE statement:
mysqldump --add-drop-table mydatabase mytable > mytable_backup.sql
This dumps only mytable
from mydatabase
into mytable_backup.sql
with a DROP TABLE statement.
Example 6: Dump a database, excluding a specific table:
mysqldump --add-drop-table mydatabase --ignore-table=mydatabase.mytable > mydatabase_without_mytable.sql
Excludes mytable
from the dump of mydatabase
, but other tables include DROP TABLE statements.
Example 7: Dump only the structure (schema) with DROP TABLE statements:
mysqldump --no-data --add-drop-table mydatabase > mydatabase_structure.sql
This command dumps only the schema of mydatabase
without data, including DROP TABLE statements.
Example 8: Dump a database with INSERT IGNORE and DROP TABLE:
mysqldump --add-drop-table --insert-ignore mydatabase > mydatabase_insert_ignore.sql
This uses INSERT IGNORE to handle duplicate rows and includes DROP TABLE statements for mydatabase
.
Example 9: Dump a database with DROP TABLE and a specific character set:
mysqldump --add-drop-table --default-character-set=utf8 mydatabase > mydatabase_utf8.sql
This sets the character set to UTF-8 and includes DROP TABLE statements for mydatabase
.
Example 10: Dump a database and specify a custom format:
mysqldump --add-drop-table --tab=/path/to/dumpdir mydatabase
Using --tab
, this command dumps mydatabase
and includes DROP TABLE statements, saving the output in a custom directory.
To verify whether the command executed successfully, check the content of the generated dump file. Open the file using a text editor or use command-line tools like grep
or less
to confirm that DROP TABLE statements are present where expected.
Read Also
Add DROP DATABASE Statements in mysqldump Output
Omit Tablespace Information in mysqldump Output
Using Custom Configuration Files with mysqldump
Dump All Tablespaces with mysqldump
Dump All Databases Using mysqldump
Discussion about this post