You change a SQL Server logical file name with the MODIFY FILE clause of ALTER DATABASE, using the current logical name to identify the file and the NEWNAME option to rename it. The physical file path stays untouched, so this isn't a file move. For example, ALTER DATABASE SalesDB MODIFY FILE (NAME = SalesDB_Data, NEWNAME = SalesDB_Primary) renames the logical reference only. The catch is that nothing rewrites automatically: code, backup scripts, and maintenance jobs that reference the old name keep working only until someone changes them, and in some cases they break immediately. Plan the rename around those dependencies.

Logical names are internal labels SQL Server uses to identify each data and log file inside a database. They appear in sys.database_files, in sys.master_files, in backup and restore statements that use MOVE, in DBCC commands, and anywhere a script names a file explicitly. Physical names are the operating-system paths on disk. Changing one doesn't change the other, and mixing them up is the most common way this task goes wrong.

What the logical file name actually controls

Every database has at least two files: one data file (.mdf by convention) and one log file (.ldf). Each carries a logical name assigned at creation, either by you or by SQL Server if you let it default. The default is usually the database name plus a suffix, but a database restored from another server often inherits the original server's naming, which is exactly why renaming comes up: a restored copy called SalesDB might still contain files logically named OldSales_Data and OldSales_Log.

The logical name is metadata, stored in the database's file catalog. It doesn't affect where bytes live, how the storage engine reads pages, or performance. What it affects is anything that resolves a file by name:

Backup and restore scripts using WITH MOVE 'LogicalName' TO 'path' ALTER DATABASE ... MODIFY FILE statements for growth, size, or path changes Filegroup and file targeting in some administrative scripts Monitoring and documentation that report file names

Because the reference is by name, a rename is safe for the database itself but risky for everything around it. The database keeps running; the scripts may not.

One more distinction worth fixing early: MODIFY FILE ... NEWNAME changes the logical name. MODIFY FILE ... FILENAME changes the physical path. They're separate operations and can be combined, but they answer different questions. This article covers the logical name only, with a note on the physical side where the two interact.

Prerequisites and a pre-change inventory

Before touching anything, confirm four things.

First, you need ALTER permission on the database, typically held by db_owner members or a sysadmin. Second, you need to know the exact current logical names, which you get from a query rather than from memory. Third, you need a recent backup, because even metadata changes deserve a rollback point. Fourth, you need to know what references the old name.

Run this against the target database:

type_desc tells you whether each row is ROWS (data) or LOG. Cross-check against the server-wide view if you want the full picture across databases:

Then search your estate for the old name. Query job steps in SQL Server Agent, check maintenance plans, grep your deployment scripts and SSIS packages, and look at backup jobs that use MOVE. A simple approach on the server is to search msdb for job commands containing the name:

That won't catch everything stored outside SQL Server, so also check source control and any documentation your team relies on. Do this inventory before the change, not after, because after the rename the old name no longer appears in the catalog and your search target is gone.

If the database is a secondary in an Always On availability group, or involved in replication, log shipping, or mirroring, stop and check the supported behavior for your version before proceeding. Metadata changes replicate through the normal mechanisms, but the timing and any restrictions on which replica you can modify from vary by configuration. Renaming on a primary is generally the right place, but verify against your topology rather than assuming.

The rename procedure, step by step

With the inventory done, the change itself is short. The order matters mainly because of what follows it.

1: Confirm current names. Re-run the sys.database_files query and note the exact string, including case and any spaces. Logical names are identifiers; a mismatch fails the statement.

2: Take or verify a backup. A full backup of the database is the rollback point for the metadata change. If a recent one exists and nothing has changed since, note it and move on.

3: Run the rename in the target database context. Single-user mode isn't required for a logical rename, and the operation is quick. The statement is:

You can rename data and log files in separate statements, or one at a time if you want to verify between steps. NEWNAME accepts a string up to 128 characters, and the new name must be unique within the database.

4: Verify the catalog. Run the sys.database_files query again. The name column should show the new value while physical_name is unchanged. That contrast is the success check: new logical label, same path.

5: Update dependencies. Edit the backup scripts, job steps, and deployment code found in the inventory. This is the step people skip, and it's the one that causes failures weeks later when a restore script can't find its MOVE target.

6: Test the dependent paths. Run one backup and one restore-to-a-scratch-database using WITH MOVE and the new logical names. A restore that succeeds against the renamed database proves the backup header carries the new names, which it does after the rename.

What success and failure look like

Success is quiet: the ALTER returns "The file 'NewLogicalName' has been modified in the database 'YourDatabase'" (wording varies slightly by version), the catalog shows the new name, and the database stays online throughout. No restart, no detach, no downtime for ordinary workloads.

Failure is usually one of three messages. "File 'OldLogicalName' does not exist in database" means the NAME value doesn't match the catalog exactly; re-check the string, including trailing spaces. "The file name 'NewLogicalName' is already in use" means another file in the database already has that logical name. A permissions error means you're not in the right role. All three are reversible in the sense that nothing changed; fix the input and rerun.

If the statement succeeds but a job later fails with an "invalid file name" or "cannot find file" error, the cause is almost always an un-updated dependency, not the rename itself.

Changing the logical name versus the physical file name

These two operations get conflated constantly, so it's worth separating them cleanly. The logical name is metadata inside SQL Server. The physical name is the file on disk, and changing it requires MODIFY FILE ... FILENAME plus taking the file offline and bringing it back, which is a heavier operation with a brief interruption for that file.

A side-by-side view:

Aspect Logical name (NEWNAME) Physical name (FILENAME)
What changes Metadata label in the catalog Path or file name on disk
Database stays online Yes No, the file is taken offline and brought back
Requires file move on disk No Yes, you move the file yourself
Affects backup MOVE targets Yes Indirectly, through path references
Reversible easily Yes, rename back Yes, but repeat the offline cycle

You can do both in one statement if you're relocating a file, but there's no requirement to. Most logical-name renames happen precisely because someone wants the catalog to look right without touching storage.

Physical renames also carry a failure mode the logical rename doesn't: if the file isn't actually moved to the new path before you bring it back online, the database can't start that file and you're in recovery territory. Logical renames have no equivalent risk because nothing on disk moves.

Dependencies that break and how to catch them early

The rename is metadata-safe, but the ecosystem around it isn't self-healing. The dependencies fall into a few predictable buckets.

Backup and restore scripts. A restore script with MOVE 'OldSales_Data' TO 'D:\Data\SalesDB.mdf' fails after the rename because the backup header now carries the new logical name. The fix is to update the MOVE clauses to the new names. When you restore a backup taken before the rename, the old names are still in that backup's header, so old backup files remain restorable with old scripts. That's a subtle point: the rename applies going forward, and pre-rename backups keep their original logical names inside them.

Agent jobs and maintenance plans. Jobs that issue ALTER DATABASE ... MODIFY FILE (NAME = OldName, ...) for growth or size changes break. The msdb search above finds these.

DBCC and diagnostic scripts. DBCC SHRINKFILE (OldLogicalName) fails with the old name. Any shrink or file-targeted maintenance job needs updating.

Documentation and runbooks. Lower stakes but real: an on-call engineer following a runbook with the old name hits a failure during an incident, which is the worst time to discover it.

A practical way to catch all of these before they bite is to run the dependency inventory as a checklist item in the change itself, then run a full backup and a test restore immediately after the rename. The backup-restore cycle exercises the most common broken path, the MOVE clause, and proves the new names are live in the backup header.

Renaming when the database is restored or copied

The most common real-world trigger for this task is a restored or copied database. SQL Server's restore process preserves the logical names stored in the backup, so a database restored from a production server keeps production's file names even when the database is renamed to something like SalesDB_Test. On a server hosting several restored copies, you end up with duplicate logical names across databases, which is legal (they're scoped per database) but confusing, and it makes scripts that assume names are unique across the server fragile.

The fix is the same MODIFY FILE statement, run once per file after the restore, followed by updating the restore job's MOVE targets for future refreshes. A cleaner long-term pattern is to have the restore script map logical names explicitly with MOVE clauses so the restored copy lands with the intended names in the first place, and then the post-restore rename becomes unnecessary.

There's a version consideration here without a specific claim to make: the exact wording of messages and any edge cases around renaming files on read-only filegroups or in contained databases are worth verifying against the documentation for your SQL Server version (2016 through 2022, and Azure SQL Database behaves differently because you don't manage files directly). The core MODIFY FILE ... NEWNAME syntax has been stable for many versions, and the operation is online for ordinary databases.

Rolling back and re-running safely

If the rename was wrong, the rollback is the same command in reverse: ALTER DATABASE YourDatabase MODIFY FILE (NAME = NewLogicalName, NEWNAME = OldLogicalName);. Because it's metadata, rollback is instant and doesn't need the backup unless something else went wrong simultaneously. Re-running a rename with the same names is harmless; re-running with the old name after a successful rename fails with "file does not exist," which is the expected guard against double application.

The riskier rollback scenario is a rename followed by dependency updates followed by a decision to revert. Then you're reverting two things, and a partial revert (old logical name, updated scripts) recreates the original mismatch. If a rename is likely to be contested, do it in a maintenance window and treat dependency updates as part of the same change unit, not a follow-up task.

For databases under active replication or availability groups, test the rollback path on a non-production copy first. The rename itself is small; the coordination around it isn't.

A worked example and a quick checklist

Suppose a database called Inventory was restored from a server where the files were named INV_PROD_DATA and INV_PROD_LOG. You want them to read Inventory_Data and Inventory_Log, and a nightly restore job uses MOVE 'INV_PROD_DATA' to a staging path.

The sequence: query sys.database_files to confirm the two names; run ALTER DATABASE Inventory MODIFY FILE (NAME = 'INV_PROD_DATA', NEWNAME = 'Inventory_Data'); and the equivalent for the log; verify the catalog; then edit the restore job's MOVE clauses to the new names. Notice the quoting: logical names with no special characters work unquoted, but quotes are harmless and guard against spaces.

If you forget the job edit, the next nightly restore fails with "Logical file name 'INV_PROD_DATA' was not found," which is exactly the symptom the pre-change inventory was designed to prevent. The fix is a one-line job edit, and the database itself is unaffected.

A compact checklist for the whole task: back up, inventory the old name across jobs and scripts, run MODIFY FILE per file, verify sys.database_files shows new names with unchanged physical paths, update dependencies, then prove it with a backup and a MOVE-based test restore. Each step maps to a failure mode described above, and skipping the last one is how renames pass a spot check and still break the next restore.

For teams that rename often, wrapping the rename and the catalog verification in a small script that also checks msdb for the old name turns a multi-step procedure into a single reviewed change. That's the difference between a rename that holds and one that gets discovered months later by a failed job.