ALTER DATABASE (Transact-SQL)

Applies to: SQL Server 2022, SQL Server 2019, SQL Server 2017, SQL Server 2016, SQL Server 2014, SQL Server 2012, SQL Server 2008 R2, SQL Server 2008, SQL Server 2005

Modifies one or more properties of a database or its file(s). For databases that are not in SIMPLE or BULK_LOGGED recovery model, you can use ALTER DATABASE to take the database offline and bring it back online.

Syntax


ALTER DATABASE <database_name>
{
    MODIFY NAME = <new_database_name>
    | SET <option_name> [ ,...n ]
    | COLLATE <collation_name>
    | ... [ other options ] ...
}

Arguments

<database_name>

Is the name of the database to be modified.

MODIFY NAME = <new_database_name>

Changes the name of the database to <new_database_name>. The new name must be unique.

SET <option_name> [ ,...n ]

Specifies one or more database options to be set. Options include, but are not limited to:

COLLATE <collation_name>

Specifies the default collation for the database. If not specified, the database inherits the collation of the SQL Server instance.

... [ other options ] ...

This represents various other clauses that can be used with ALTER DATABASE, such as modifying file properties (MODIFY FILE), setting database filegroups, and more. For a complete list, please refer to the official Microsoft documentation.

Permissions

Requires ALTER ANY DATABASE permission on the server, or CREATE DATABASE permission, or membership in the db_owner fixed database role.

Examples

Example 1: Renaming a database


ALTER DATABASE MyDatabase
MODIFY NAME = MyNewDatabase;
            

Example 2: Setting a database to read-only


ALTER DATABASE MyDatabase
SET READ_ONLY = ON;
            

Example 3: Taking a database offline


ALTER DATABASE MyDatabase
SET OFFLINE WITH ROLLBACK IMMEDIATE;
            

See Also