Here in this article, I will describe how to attach a database in SQL Server.

One of the easiest way to distribute, copy or backup a database in SQL Server is to use attach and detach options.

A SQL Server database has two types of files. The first file is the main data file and it has extension of .MDF and the second file is the log file and it has an extension of .LDF. If you have the log file for a database, the easiest way to attach the database is to use the existing log file.

Follow these steps for attaching database:

The default directory for SQL Server database is:

C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data

Open Object Explorer in SQL Server and right click on Databases and select Attach menu item as you see in the below image.

new2.jpg

Figure 1.

Now, select a file option and use Browse button to browse your MDF and LDF files as seen in below image.

new image.jpg

Figure 2.

That's it. Your database is attached now and ready to use.

The reverse operation of Attach is detach. The Detach option removes the database from the Databases list in the Object Explorer and saves the files to the given location.

Right click on the database name you would like to detach and select Detach menu item.

Image3.jpg

Figure 3.

Once a database is detached, you may distribute or back it up and once you're done, you may want to attach it again to start using it.

Hope this tip was useful for who are looking for attach and detach options in SQL Server.