How to Backup a Single Table in MySQL
In MySQL, the default behavior of the mysqldump utility is to back up an entire database. However, it is possible to selectively back up a single table for specific purposes. Here's how to achieve this:
Dumping a Single Table as an SQL File
To create a backup of a single table as an SQL file, use the following command:
mysqldump db_name table_name > table_name.sql
Where:
Restoring a Single Table from SQL File
To restore a single table from an SQL backup file, follow these steps:
Connect to the MySQL database using the following command:
mysql -u username -p db_name
Execute the following command to restore the table from the SQL file:
mysql> source full_path/table_name.sql
Alternatively, you can also restore the table in a single line:
mysql -u username -p db_name < /path/to/table_name.sql
Dumping and Restoring a Single Table in Compressed Format
You can also dump and restore a single table in a compressed (.sql.gz) format.
Dump:
mysqldump db_name table_name | gzip > table_name.sql.gz
Restore:
gunzip < table_name.sql.gz | mysql -u username -p db_name
The above is the detailed content of How to Backup and Restore a Single MySQL Table?. For more information, please follow other related articles on the PHP Chinese website!