Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rename database data file in SQL Server 2005

Tags:

I need to rename a databases data file. Is this possible through SQL Server Management Studio?

FYI, I do not have permissions to the underlying box.

Edit: I also need to change the location of the file.

like image 539
Jason Avatar asked Feb 03 '10 16:02

Jason


People also ask

How do I rename a SQL Server database file?

If you are using SQL Server, you can set the database to single-user mode to close any open connections and prevent other users from connecting while you are changing the database name. In Object Explorer, expand Databases, right-click the database to rename, and then select Rename.

How do I rename an MDF file in SQL Server?

Open Microsoft SQL Server Management Studio. Connect to the server wherein the DB you want to rename is located. Go to the location that MDF and LDF files are located and rename them exactly as you specified in first two alter commands. If you changed the folder path, then you need to move them there.

How do you change a file name in SQL?

Change the logical file for the SQL Server Database To change the logical file name, view the database properties by right click on the database -> properties.

Which syntax is correct for rename database in SQL?

ALTER keyword is used to add, delete/drop or modify columns in the existing table. It is also used to add and drop various constraints on the existing table. Renaming means we are modifying the data. Hence, we will use ALTER – MODIFY.


1 Answers

Yes, you can do this, as long as you have the right to detach and re-attach the database, and as long as you find a way to physically rename the files on disk:

1) issues these commands

ALTER DATABASE yourdatabase MODIFY FILE (NAME = logical_file_name, FILENAME = 'your-new-file-on-disk.mdf' ) 

(as DGGenuine pointed out in a comment: the 'your-new-file-on-disk.mdf' must be a full file name - including path - on your disk)

2) detach the database

3) rename the files on disk

4) re-attach the database again

like image 171
marc_s Avatar answered Oct 14 '22 05:10

marc_s