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

How to Backup and Restore a Single MySQL Table?

Susan Sarandon
Release: 2024-12-01 16:19:09
Original
299 people have browsed it

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
Copy after login

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
Copy after login

Execute the following command to restore the table from the SQL file:

mysql> source full_path/table_name.sql
Copy after login

Alternatively, you can also restore the table in a single line:

mysql -u username -p db_name < /path/to/table_name.sql
Copy after login

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
Copy after login

Restore:

gunzip < table_name.sql.gz | mysql -u username -p db_name
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template