How to Rename a SQL Server Database using T-SQL

While you can certainly rename a SQL Server database in the SSMS GUI by right-clicking on the database name and selecting Rename, sometimes you might prefer (or need) to do it using Transact-SQL.

The most basic way to rename a database using T-SQL is like this:

-- Change to the master database
USE master;

-- Change the database name
ALTER DATABASE Films  
Modify Name = Movies; 
GO

The only problem with this basic script is that it doesn’t change the name of the data files and log files. In most cases you’ll probably want to change the names of these files to match the new name. In that case you can take the following script and replace the database name with your own (as well as its file names and paths):

-- Change to the master database
USE master;

-- Change the database name
ALTER DATABASE Films  
Modify Name = Movies; 
GO

-- Change the logical name of the data file
ALTER DATABASE Movies
MODIFY FILE ( 
  NAME = 'Films', 
  NEWNAME = 'Movies' 
  );

-- Change the logical name of the log file
ALTER DATABASE Movies
MODIFY FILE ( 
  NAME = 'Films_log', 
  NEWNAME = 'Movies_log' 
  );

-- Change the physical name of the data file
ALTER DATABASE Movies
MODIFY FILE ( 
  NAME = 'Movies', 
  FILENAME = 'D:\mssql\data\Movies.mdf' 
  );

-- Change the physical name of the log file
ALTER DATABASE Movies
MODIFY FILE ( 
  NAME = 'Movies_log', 
  FILENAME = 'D:\mssql\data\Movies_log.ldf' 
  );  

-- Check it (View the list of database files and their locations)
USE master;
SELECT 
  name 'Logical Name', 
  physical_name 'File Location',
  size 'File Size'
FROM sys.master_files;

This script renames the database, renames the logical names of the data file and log file, and then renames the actual physical file name of those files. It finishes off with selecting a list of all database files and their locations. This allows you to check that your files do in fact have the correct name.

Using Linux/Mac File Paths

The above example uses Windows file path syntax (back slashes and a drive letter). If you use Linux or Mac, the file path syntax will look more like the following example (only the file paths have changed – everything else remains the same).

-- Change to the master database
USE master;

-- Change the database name
ALTER DATABASE Films  
Modify Name = Movies; 
GO

-- Change the logical name of the data file
ALTER DATABASE Movies
MODIFY FILE ( 
  NAME = 'Films', 
  NEWNAME = 'Movies' 
  );

-- Change the logical name of the log file
ALTER DATABASE Movies
MODIFY FILE ( 
  NAME = 'Films_log', 
  NEWNAME = 'Movies_log' 
  );

-- Change the physical name of the data file
ALTER DATABASE Movies
MODIFY FILE ( 
  NAME = 'Movies', 
  FILENAME = '/var/opt/mssql/data/Movies.mdf' 
  );

-- Change the physical name of the log file
ALTER DATABASE Movies
MODIFY FILE ( 
  NAME = 'Movies_log', 
  FILENAME = '/var/opt/mssql/data/Movies_log.ldf' 
  );  

-- Check it (View the list of database files and their locations)
USE master;
SELECT 
  name 'Logical Name', 
  physical_name 'File Location',
  size 'File Size'
FROM sys.master_files;

Be sure to backup the master database once you’ve renamed your database/s (here’s how to backup a database with T-SQL). You might also want to check/update any scripts you have to ensure they reference the new database name.