SIGN UP MEMBER LOGIN:    
ARTICLE

How to Attach a Database in SQL Server 2005

Posted by Raj Kumar Articles | SQL Server 2012 September 02, 2008
This article describes how to attach database in SQL Server 2005.
Reader Level:

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

One of the easiest way to distribute, copy or backup a database in SQL Server 2005 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:

  • To attach a database right click on the Database folder and select the Attach Database command to display the Attach Database dialog box then click on the Add button and use the resulting dialog box to select the .MDF file for the database. This should add both the data file and the log file for the database to the database details pane at the bottom of the dialog box and at last click OK to attach the database. 
  • If you want to attach a database that does not have a log file or if you want create a new log file for database, you can remove the log file for the database before you click on OK button. To do that select the log file in database details pane and click the Remove button. 
  • To detach a database right click on the database and select Tasks/Detach command. The click on the OK button.

The default directory for SQL Server 2005 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.

Image1.jpg

Figure1.

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

Image2.jpg

Figure2.

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 

Figure3.

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 2005.

 

Login to add your contents and source code to this article
share this article :
post comment
 

Example: DBCC TRACEON(1807) GO PRINT 'ATTACH FIRST' CREATE DATABASE TEST ON ( FILENAME = N'\\server\tmp\test.mdf' ), ( FILENAME = N'\\server\tmp\test_log.ldf' ) FOR ATTACH; GO PRINT 'Detach' exec sp_detach_db TEST GO The Service account has permissions on the network drive. The problem is that SQL Express need full rights because of changing the permissions when attaching the db files. First time the database could attached, but then the permissions are changed an the database could not attach again.

Posted by Raj Kumar Feb 20, 2011

thanks sir.

Posted by vikas kumar Feb 08, 2011

i want to know attach database over the network .It is sql qurey

Posted by thiri nwe Sep 27, 2010

Both are used to restore database we Use attach database when we have .mdf file to upload data into database while we use restore database when we have .bak file to upload data into database.

Posted by Raj Kumar Jun 07, 2010

thanks buddy

Posted by Rahul K May 26, 2010
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Become a Sponsor