Introduction
Sometimes, we need to change the database file location from one drive to another drive. Here are some simple tips on how we can achieve this goal.
Steps
- Suppose you have a database file named Test and you want to move it to other drive from the default location. First, run the following query to get information about the database file.
select * from Test.sys.database_files
Using databse_files
view, you can get information about databasefile
such as Name
, physical_name
, state_desc
. From physical_name
column, you get the current location of the database file.
- Run database
OFFLINE
script:
use master
alter database Test set OFFLINE
- Move database file (both .mdf, .ldf) to the desired location. In my case, I have moved my files in D drive SqlDatabase folder (D:\SqlDatabase).
- Update database and
modify File
. Set FileName
as your desired location:
ALTER DATABASE test
MODIFY FILE (
NAME = 'test',
FILENAME = 'D:\SqlDatabase\Test.mdf'
)
ALTER DATABASE test
MODIFY FILE (
NAME = 'test_log',
FILENAME = 'D:\SqlDatabase\Test_log.ldf'
)
- Change the permission of these files and give full control to Users Group.
- Run database
online
script:
ALTER DATABASE Test SET ONLINE
By following these simple steps, you can move your database from one drive to another.
Points of Interest
History
- 18th October, 2018: Initial version