How to restore and repair MDF files of SQLServer

Source: Internet
Author: User
If the backup database has two files. LDF and. MDF: Open the Enterprise Manager, right-click the instance, select all tasks, and attach the database. MDF file. Alternatively, enter sp_attach_db Database Name, path file name. ldf, and path file name. MDFSQLServer database backup in the query analyzer.

If the backup database has two files. LDF and. MDF: Open the Enterprise Manager, right-click the instance, select all tasks, and attach the database. MDF file. Alternatively, enter sp_attach_db Database Name, path file name. ldf, and path file name. mdf SQL Server database backup in the query analyzer.

If the backup database has two files. LDF and. MDF: Open the Enterprise Manager, right-click the instance, select all tasks, and attach the database. MDF file.

Alternatively, enter:

Sp_attach_db "Database Name", "path file name. ldf", "path file name. MDF"

There are two ways to back up SQL Server databases: one is to back up DATABASE files using BACKUP DATABASE, and the other is to directly copy the DATABASE file mdf and Log File ldf. The following describes the backup and recovery of the latter. This document assumes that you are familiar with SQL Server Enterprise Manager and SQL Server Quwey Analyser)

1. Normal backup and recovery methods

Normally, we need to back up a database. First, we need to disconnect the database from the running data server, or stop the entire database server, and then copy the file.

Command for removing the database: Sp_detach_db Database Name

Command for connecting to the database: Sp_attach_db or sp_attach_single_file_db

S_attach_db [@ dbname =] 'dbname', [@ filename1 =] 'filename _ n' [,... 16]

Sp_attach_single_file_db [@ dbname =] 'dbname', [@ physname =] 'physical _ name'

This method can be used to correctly restore the database files of SQL Sever7.0 and SQL Server 2000. The key point is that the mdf and ldf files must be backed up during backup. The mdf file is the database data file, ldf is a database log file.

Example:

Assume that the database is test, the data file is test_data.mdf, and the log file is test_log.ldf. Next we will discuss how to back up and restore the database.

Detach a database: sp_detach_db 'test'

Connect to the database: sp_attach_db 'test', 'c: Program FilesMicrosoft SQL ServerMSSQLDatatest_data.mdf ', 'c: Program FilesMicrosoft SQL servermssqldatatest_log.ldf'

Sp_attach_single_file_db 'test', 'c: Program FilesMicrosoft SQL ServerMSSQLDatatest_data.mdf'

// This article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.