Examples of MYSQL REPAIR TABLE
Example 1:
- In the given example first, we will create a table and insert some data into it, then we will corrupt our table for the demonstration purpose.
- After this we will use “REPAIR TABLE” statement to repair the table.
-- Create a table
CREATE TABLE my_table (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL
);
-- Insert some data
INSERT INTO my_table (id,name) VALUES (1,'Badal'), (2,'Ashutosh'), (3,'Diya');
-- Corrupt the table (for demonstration purposes)
UPDATE my_table SET id = id + 1 WHERE id = 2;
-- Check the table status
CHECK TABLE my_table;
-- Repair the table
REPAIR TABLE my_table;
-- Check the table status again
CHECK TABLE my_table;
Output:
Table |
sandbox_db.my_table |
---|---|
Op |
check |
Msg_Table |
status |
Msg_Text |
OK |
-- Select data from the table
SELECT * FROM my_table;
id |
name |
---|---|
1 |
Badal |
2 |
Ashutosh |
3 |
Diya |
Example 2
- The REPAIR TABLE command is generally used to repair the table in MySQL in the case your table is corrupted.
- But if we are using the “REPAIR TABLE my_table USE_FRM” command then it will instruct to recreate the .frm file present in the table. It can help us in the case our file is corrupted.
Note: Replace the my_table to your table name and include the USE_FRM command.
Syntax:
REPAIR TABLE my_table USE_FRM;
Output:
Table |
Op |
Msg_type |
Msg_text |
---|---|---|---|
database.mytable |
repair |
status |
OK |
Example 3:
- In the case we are having multiple tables then we can repair them all at a single query or we just have to use table names along with the “REPAIR TABLE” command.
- This command will repair our all the tables in a single query. After running this command, the status of all the tables will appear.
Output:
Table |
Op |
Msg_type |
Msg_text |
---|---|---|---|
database.table1 |
repair |
status |
OK |
database.table2 |
repair |
status |
OK |
database.table3 |
repair |
status |
OK |
MySQL REPAIR TABLE
MySQL is an open–source Relational Database Management system that stores the data in the form of rows and columns and SQL is the language that is used to store, manipulate, and retrieve the data. “REPAIR TABLE” is one of the useful commands in MySQL and is used for repairing the tables in case the tables get corrupted.
The corruption of the tables can lead to many problems such as table structure, key entries, lost rows, and so on. In this article, we will explore how we can use in “REPAIR TABLE” command in many ways to repair our tables.
Contact Us