Home >Database >Mysql Tutorial >How to Backup and Restore a Single MySQL Table?

How to Backup and Restore a Single MySQL Table?

Susan Sarandon
Susan SarandonOriginal
2024-12-01 16:19:09312browse

How to Backup and Restore a Single MySQL Table?

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:

  • db_name is the name of the database containing the table.
  • table_name is the name of the table to be backed up.

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn