How to Repair Table in MySQL: Syntax and Examples
To repair a table in MySQL, use the
REPAIR TABLE table_name; command. This command attempts to fix corrupted MyISAM or ARCHIVE tables and returns the repair status.Syntax
The REPAIR TABLE command is used to fix corrupted tables in MySQL. It works mainly with MyISAM and ARCHIVE storage engines.
table_name: The name of the table you want to repair.QUICK(optional): Repairs only the index file, faster but less thorough.EXTENDED(optional): Does a more thorough repair, slower but more effective.USE_FRM(optional): Uses the .frm file to recreate the table.
sql
REPAIR TABLE table_name [QUICK | EXTENDED | USE_FRM];Example
This example shows how to repair a table named customers. It runs a basic repair and shows the result.
sql
REPAIR TABLE customers;Output
+------------------+--------+----------+--------------------------------+
| Table | Op | Msg_type | Msg_text |
+------------------+--------+----------+--------------------------------+
| test.customers | repair | status | OK |
+------------------+--------+----------+--------------------------------+
Common Pitfalls
Common mistakes when repairing tables include:
- Trying to repair InnoDB tables with
REPAIR TABLE, which does not support InnoDB. UseCHECK TABLEand InnoDB recovery methods instead. - Not backing up data before repair, risking data loss.
- Ignoring error messages returned by the repair command.
sql
/* Wrong: Trying to repair InnoDB table */ REPAIR TABLE orders; /* Right: Check InnoDB table instead */ CHECK TABLE orders;
Quick Reference
| Command | Description |
|---|---|
| REPAIR TABLE table_name; | Basic repair of MyISAM or ARCHIVE table |
| REPAIR TABLE table_name QUICK; | Fast repair, only indexes |
| REPAIR TABLE table_name EXTENDED; | Thorough repair, slower |
| REPAIR TABLE table_name USE_FRM; | Recreate table using .frm file |
| CHECK TABLE table_name; | Check table integrity (use for InnoDB) |
Key Takeaways
Use REPAIR TABLE to fix MyISAM or ARCHIVE tables in MySQL.
REPAIR TABLE does not work for InnoDB tables; use CHECK TABLE instead.
Always back up your data before attempting repairs.
Use QUICK or EXTENDED options based on repair needs.
Check the command output for repair status and errors.