Support our 100% FREE Projects: Donate Now OR Sponsor Now


  

sql server move database to a different drive

In this step by step SQL tutorial I will show you how to move database to a different drive using SQL Server 2008 R2.

Before we began let's clarify why I am doing this. In my case I have virtual drive that has 30GB drive and I am running low on disk space. My biggest files on C drive are SQL Server databases therefore I have decided to move them to F drive that has much more disk space.

NOTE: This tutorial is applicable to certain development machines and should not be used on production servers as usually you require extra steps that involve "moving" folder permissions and all dependand processes.

See below images that shows that I have 877MB left on my C drive and 17.7GB available on F Drive.

Disk drive space

 

My current database files (mdf = data & ldf = log file) are located in C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA which is default installation path and below you can see the biggest 3 databases that I will want to move from C drive to F drive. As you can see below my 3 biggest databases will give me extra 1GB free space on C drive.

my biggest databases

 

How to move databases to a different drive

There are diffent ways to move a database from one drive to another one like backup / restore, T-SQL script but in our case we want the easiest and quickest one so we will use DETACH database method using SSMS and simply copy/paste files and ATTACH them back to the database instance.

I opened SSMS, connected to my local instance of SQL Server right clicked one of the databases and selected TASKS and then clicked Detach. See below example.

Detach database menu

I got a detach database dialog box where I clicked ok. 

NOTE: when you detach database you might noticed that the database does NOT disappear in solution explorer. This is because SSMS doesn't refresh the database list so you might click top level databases folder and click refresh and the detached database should disappear from the list.

Detach Database Dialog box

I have repeated DETACH operation for the remaining 2 database and now I'm ready to move my database files to new location which in my case is F:\Database\Data

See below example that shows files in current C drive location and new location. Notice that one database has 2 kind of files mdf (=data) and ldf (=log file) so I will move all 6 files.

moving files to another drive

I cut & pasted the files and below you can see that files in new F drive location.

moved databases to new drive

Now that we have files in new location it is time to ATTACH the database to server. To do that connect to your instance. Right click database folder and click attach.

attach database menu

We are presented with attach database dialog box. I pressed Add button. Find my new F drive location with new files. Selected one database and clicked OK

 

attach database dialog box

After clickng ok the dialog box was filled in with information. See example below.

attach database file

I have repeated the same ATTACH steps for the remaning files and below is screenshot of the disk space after I moved the database files to new drive.

 

disk drive space

 

I hope this tutorial will help you with moving your database to new drive.

Take care

Emil

 

Share: Share on FacebookShare on Google PlusTweet it
Comments Add Comment
No data was returned. Share your thoughts, questions and suggest improvements:
Add Comment

You found us! Below are 50 most popular searched keywords

created at TagCrowd.com

Disclaimer: While every caution has been taken to provide our readers with most accurate information and honest analysis, please use your discretion before taking any decisions based on the information in this blog. Author will not compensate you in any way whatsoever if you ever happen to suffer a loss/inconvenience/damage because of/while making use of information in this blog.