MySQL - CHECKSUM TABLE Statement

In case something wrong happens to the database server like if the server was shutdown unexpectedly or, an error occurred while writing data to the hard disk, etc. These situations could make the database operate incorrectly and in the worst case, it can be crashed. Sometimes specific tables will be crushed.
A checksum is a mechanism to detect issues in the table space. The CHECKSUM TABLE Statement is used to report checksum on the specified table. To execute this statement, you need SELECT privilege.
Following is the syntax of the MySQL CHECK TABLE Statement −
CHECK TABLE tbl_name [, tbl_name] ... [option] ...
option: {
   FOR UPGRADE
   | QUICK
   | FAST
   | MEDIUM
   | EXTENDED
   | CHANGED
}

Assume we have created a table named sales as shown below −
mysql> CREATE TABLE sales(
   ID INT,
   ProductName VARCHAR(255),
   CustomerName VARCHAR(255),
   DispatchDate date,
   DeliveryTime time,
   Price INT,
   Location VARCHAR(255)
);
Query OK, 0 rows affected (2.22 sec)
Now, we will insert 5 records in Sales table using INSERT statements −
mysql> insert into sales values(1, 'Key-Board', 'Raja', DATE('2019-09-01'), TIME('11:00:00'), 7000, 'Hyderabad');
Query OK, 1 row affected (0.18 sec)

mysql> insert into sales values(2, 'Earphones', 'Roja', DATE('2019-05-01'), TIME('11:00:00'), 2000, 'Vishakhapatnam');
Query OK, 1 row affected (0.14 sec)

mysql> insert into sales values(3, 'Mouse', 'Puja', DATE('2019-03-01'), TIME('10:59:59'), 3000, 'Vijayawada');
Query OK, 1 row affected (0.17 sec)

mysql> insert into sales values(4, 'Mobile', 'Vanaja', DATE('2019-03-01'), TIME('10:10:52'), 9000, 'Chennai');
Query OK, 1 row affected (0.07 sec)

mysql> insert into sales values(5, 'Headset', 'Jalaja', DATE('2019-04-06'), TIME('11:08:59'), 6000, 'Goa');
Query OK, 1 row affected (0.07 sec)
Following table retrieves the checksum of the above table −
mysql> CHECKSUM TABLE Sales;
+------------+------------+
| Table      | Checksum   |
+------------+------------+
| mydb.sales | 1059835375 |
+------------+------------+
1 row in set (0.00 sec)
You can also retrieve the ckecksum of multiple tables at once using the CHECKSUM TABLE Statement.
Assume we have created three new tables as shown below −
mysql> CREATE TABLE Test1(ID INT, Name VARCHAR(255));
Query OK, 0 rows affected (2.27 sec)

mysql> CREATE TABLE Test2(ID INT, Name VARCHAR(255));
Query OK, 0 rows affected (0.47 sec)

mysql> CREATE TABLE Test3(ID INT, Name VARCHAR(255));
Query OK, 0 rows affected (0.52 sec)
Following query gets the checksum of all these tables and displays the result −
mysql> CHECKSUM TABLE Test1, Test2, Test3;
+------------+----------+
| Table      | Checksum |
+------------+----------+
| mydb.test1 | 0        |
| mydb.test2 | 0        |
| mydb.test3 | 0        |
+------------+----------+
3 rows in set (0.00 sec)
The CHECKSUM TABLE statement provides various optional clauses −
If you specify the QUICK clause in the CHECKSUM TABLE statement, it just checks the incorrect links −
mysql> CHECKSUM TABLE sales QUICK;
+------------+----------+
| Table      | Checksum |
+------------+----------+
| mydb.sales | NULL     |
+------------+----------+
1 row in set (0.00 sec).
If you specify the EXTENDED clause in the CHECKSUM TABLE statement, it performs a full check for all keys in each row −
mysql> CHECKSUM TABLE sales EXTENDED;
+------------+------------+
| Table      | Checksum   |
+------------+------------+
| mydb.sales | 1059835375 |
+------------+------------+
1 row in set (0.00 sec)

    31 Lectures 
     6 hours 

 Eduonix Learning Solutions

    84 Lectures 
     5.5 hours 

 Frahaan Hussain

    6 Lectures 
     3.5 hours 

 DATAhill Solutions Srinivas Reddy

    60 Lectures 
     10 hours 

 Vijay Kumar Parvatha Reddy

    10 Lectures 
     1 hours 

 Harshit Srivastava

    25 Lectures 
     4 hours 

 Trevoir Williams
 Print
 Add Notes
 Bookmark this page