"DBCC CHECKDB" is a SQL Server command used to check the integrity and consistency of a database. It scans all objects within a database, including tables, indexes, views, stored procedures, and more, ensuring that their physical and logical structures are correct. DBCC CHECKDB also detects page and row errors, missing or duplicate page references, issues with the transaction log, and many other potential problems. If any issues are found, it may automatically fix some of them or provide information on the necessary corrective actions to be taken.
DBCC CHECKDB (also referred to as the Database Console Command CHECKDB) is used to check the physical and logical integrity of objects such as tables, views, clusters, sequences, indexes, and synonyms in a SQL Server or Azure SQL database. It's typically used to repair database corruption. When you run DBCC CHECKDB, it essentially executes the following repair options:
Runs DBCC CHECKALLOC to check the consistency of the disk space allocation structures for the selected database.
Runs DBCC CHECKTABLE to check the integrity of all tables and indexed views.
Runs DBCC CHECKCATALOG to check catalog consistency within the selected database. This command requires that the database be online.
Validates the content of each index view in the specified database.
When FILESTREAM is enabled, verifies link-level consistency between table metadata, file system directories, and files when varbinary(max) data is stored in the file system.
Validates Service Broker data in the database.
Therefore, you do not have to run DBCC CHECKALLOC, DBCC CHECKTABLE, or DBCC CHECKCATALOG separately.
How to Use DBCC CHECKDB to Repair a Database in SQL Server
To fix corruption issues in your SQL database, you can run the following syntax:
This text is part of an SQL statement used to repair or check a database. The translation in English is as follows:
[ (Database_Name | Database_Identifier | 0
[ , NOINDEX
| , { REPAIR_ALLOW_DATA_LOSS | REPAIR_FAST | REPAIR_REBUILD } ]
) ]
[ WITH
{
[ ALL_ERRORMSGS ]
[ , EXTENDED_LOGICAL_CHECKS ]
[ , NO_INFOMSGS ]
[ , TABLOCK ]
[ , ESTIMATEONLY ]
[ , { PHYSICAL_ONLY | DATA_PURITY } ]
[ , MAXDOP = Processor_Count ]
}
]
]
Please note that the translation might need adjustments based on the specific SQL environment and context.
You might be wondering:
Database Name | Database ID | 0
The name or ID of the database to run the DBCC CHECKDB repair operation against. If not specified, or if 0 is specified, the command applies to the current database by default.
Do not index
Specifies that the nonclustered indexes of user tables are not checked in detail. This reduces the total execution time. NOINDEX has no effect on system tables, which are always checked for index integrity.
Fixes that allow data loss | Quick fixes | Fixes that require rebuild
When you use one of the repair options, you allow DBCC CHECKDB to fix the errors that it finds. To understand the differences among these repair options, see
< li > REPAIR_ALLOW_DATA_LOSS : If you use this repair option, DBCC CHECKDB attempts to repair the errors that it finds. If the operation is successful, data loss can occur. < /li >< li > REPAIR_FAST : This repair option does not perform any repairs. Instead, it is used only for backward compatibility syntax. < /li >< li > REPAIR_REBUILD : If you choose this repair option, repairs are performed without data loss. REPAIR_REBUILD includes two types of repairs: quick repairs and deep repairs. < /li >
- Note:
- For more information about other items in the syntax, see the relevant documentation on Microsoft.com.
However, as Microsoft suggests, use the REPAIR option only as a last resort. Why? When DBCC CHECKDB reports errors, the recommended best practice is to restore the database from the last known good backup. Because REPAIR_ALLOW_DATA_LOSS cannot replace restoring from a backup, it is recommended only when no backups are available.
How to repair a database without the CHECKDB REPAIR option
Is REPAIR_ALLOW_DATA_LOSS the only option when you can't repair a database from a backup? Actually, that's not the case. If DBCC CHECKDB reports errors in your selected database, you can use a SQL Database Recovery Software – the MS SQL Recovery tool to fix the corrupted database. This software allows you to:
Repair SQL Server databases: including primary (.mdf) and secondary (.ndf) files
Repair log files that could lead to database errors
Fix corrupted SQL Server database objects – Tables, Triggers, Indexes, Keys, Rules & Stored Procedures
Restore deleted or dropped SQL database records
Repair database:
Step 1: Select the corrupted database to be recovered
• Launch the MS SQL Recovery tool.
• Click on “Browse” (two dots) or “Search” to select the corrupt database file.
• After selecting the file, click on the “Repair” button to start the analysis process.
Note: You must stop the SQL Server service before using this utility.
Step 2: Repair the corrupted database
- The software displays all recoverable items in a tree-like structure. These items are listed in the left pane.
- Select the components that you want to retrieve. In the window, click on the Export button.
Select Database Objects
Step 3: Export to Database or Script
• If you choose to Export to Database, enter the required information and select the target database.
• A window will appear, asking you to provide the credentials to connect to the server and the location to save the recovered items. Click OK to start the repair process.
Important: You must restart the SQL Server service before clicking OK.
Bottom line
In SQL Server, users typically opt to use the DBCC CHECKDB command to fix database corruption. However, this is not your only option. If DBCC CHECKDB is unable to resolve the issue, or you don't want to use the potentially data-losing repair option – REPAIR_ALLOW_DATA_LOSS, you can utilize the MS SQL Recovery tool as an alternative to repair your database.