CHECK TABLE - MySQL


## Overview

CHECK TABLE inspects a table to detect and report any discrepancies, errors, or inconsistencies within its structure or data. It verifies table integrity, checks for corrupted rows or pages, and ensures data accuracy and consistency.

## Syntax

CHECK TABLE [table_name] [option]...

## Options/Flags

  • QUICK: Performs a quick check, skipping index checks and some other checks.
  • FAST: Performs a faster check by skipping some time-consuming checks.
  • MEDIUM: Default check mode, performing more thorough checks than QUICK or FAST.
  • EXTENDED: Most thorough check mode, performing all available checks.
  • NO_FOREIGN_KEY_CHECKS: Disables foreign key constraint checking.
  • CHECKSUM: Calculates a checksum for each row and compares it to the stored checksum.
  • REPAIR: Automatically repairs any errors found.

## Examples

Simple check:

CHECK TABLE my_table;

Quick check:

CHECK TABLE my_table QUICK;

Extended check with foreign key constraints disabled:

CHECK TABLE my_table EXTENDED NO_FOREIGN_KEY_CHECKS;

Repair any errors found:

CHECK TABLE my_table REPAIR;

## Common Issues

  • Table is locked: Another session or process may be holding a lock on the table, preventing CHECK TABLE from accessing it.
  • Corrupted data: CHECK TABLE will report any corrupted rows or pages detected. These errors may require manual intervention or data recovery procedures.
  • Slow performance: Extended checks can take a long time, especially for large tables. Use QUICK or FAST options for faster checks.

## Integration

  • Use with OPTIMIZE TABLE: CHECK TABLE can identify table fragments that can be optimized using OPTIMIZE TABLE.
  • Monitor table integrity: Schedule regular CHECK TABLE tasks to ensure data integrity and detect problems early.
  • Data recovery: CHECK TABLE with REPAIR can help recover data from corrupted tables.

## Related Commands

  • ANALYZE TABLE: Gathers statistics about table structure and data distribution.
  • REPAIR TABLE: Repairs corrupted tables.
  • OPTIMIZE TABLE: Optimizes table structure and performance.