I used to get really nervous about renaming database files, which I think is an appropriate response. Changing the attributes of the database’s physical objects does sound a bit scary on the surface, but once you’ve done it a few times you’ll realize it’s easy to do, and easy to fix if you make a mistake. This simple script template shows the basic steps of the process.
USE [master]; GO --Change logical file names. ALTER DATABASE [My_Database] MODIFY FILE(NAME = [old_name_data], NEWNAME = [new_name_data]); ALTER DATABASE [My_Database] MODIFY FILE(NAME = [old_name_log], NEWNAME = [new_name_log]); GO --Disconnect all existing sessions. ALTER DATABASE [My_Database] SET SINGLE_USER WITH ROLLBACK IMMEDIATE; GO --Change database to OFFLINE mode. ALTER DATABASE [My_Database] SET OFFLINE; GO /****************************************** STOP! NOW RENAME THE PHYSICAL FILES ON DISK ******************************************/ --Point databases at new phyisical files. ALTER DATABASE [My_Database] MODIFY FILE(NAME = 'new_name_data', FILENAME = 'D:\mssql13.MSSQLSERVER\MSSQL\DATA\new_name_data.mdf'); ALTER DATABASE [My_Database] MODIFY FILE(NAME = 'new_name_log', FILENAME = 'L:\MSSQL13.MSSQLSERVER\MSSQL\DATA\new_name_log.ldf'); GO --Change database to ONLINE mode. ALTER DATABASE [My_Database] SET ONLINE; GO --Reopen connections to all users ALTER DATABASE [My_Database] SET MULTI_USER; GO↧