To remove a database, use DROP DATABASE.
To rename a database, use the MODIFY NAME = new_database_name option with ALTER DATABASE.
To decrease the size of a database, use DBCC SHRINKDATABASE.
You cannot add or remove a file while a BACKUP statement is running.
A maximum of 32,767 files and 32,767 filegroups can be specified for each database.
The ALTER DATABASE statement must run in autocommit mode (the default transaction management mode) and is not allowed in an explicit or implicit transaction. For more information, see Autocommit Transactions.
In SQL Server 2005, the state of a database file (for example, online or offline), is maintained independently from the state of the database. For more information, see File States. The state of the files within a filegroup determines the availability of the whole filegroup. For a filegroup to be available, all files within the filegroup must be online. If a filegroup is offline, any try to access the filegroup by an SQL statement will fail with an error. When you build query plans for SELECT statements, the query optimizer avoids nonclustered indexes and indexed views that reside in offline filegroups. This enables these statements to succeed. However, if the offline filegroup contains the heap or clustered index of the target table, the SELECT statements fail. Additionally, any INSERT, UPDATE, or DELETE statement that modifies a table with any index in an offline filegroup will fail.
When a database is in the RESTORING state, most ALTER DATABASE statements will fail. The exception is setting database mirroring options. A database may be in the RESTORING state during an active restore operation or when a restore operation of a database or log file fails because of a corrupted backup file. For more information, see Responding to SQL Server Restore Errors Caused by Damaged Backups.
Setting Options
To retrieve current settings for database options, use the sys.databases catalog view or DATABASEPROPERTYEX. For a list of default values assigned to the database when it is first created, see Setting Database Options.
After you set a database option, the modification takes effect immediately.
To change the default values for any one of the database options for all newly created databases, change the appropriate database option in the model database.
Not all database options use the WITH <termination> clause or can be specified in combination with other options. The following table lists these options and their option and termination status.
|
Options category
|
Can be specified with other options
|
Can use the WITH <termination> clause
|
|---|
|
<db_state_option>
|
Yes
|
Yes
|
|
<db_user_access_option>
|
Yes
|
Yes
|
|
db_update_option>
|
Yes
|
Yes
|
|
<external_access_option>
|
Yes
|
No
|
|
<cursor_option>
|
Yes
|
No
|
|
<auto_option>
|
Yes
|
No
|
|
<sql_option>
|
Yes
|
No
|
|
<recovery_option>
|
Yes
|
No
|
|
<database_mirroring_option>
|
No
|
No
|
|
ALLOW_SNAPSHOT_ISOLATION
|
No
|
No
|
|
READ_COMMITTED_SNAPSHOT
|
No
|
Yes
|
|
<service_broker_option>
|
Yes
|
No
|
|
DATE_CORRELATION_OPTIMIZATION
|
Yes
|
Yes
|
|
<parameterization_option>
|
Yes
|
Yes
|
The plan cache for the instance of SQL Server is cleared by setting one of the following options:
|
OFFLINE
|
READ_WRITE
|
|
ONLINE
|
MODIFY FILEGROUP DEFAULT
|
|
MODIFY_NAME
|
MODIFY FILEGROUP READ_WRITE
|
|
COLLATE
|
MODIFY FILEGROUP READ_ONLY
|
|
READ_ONLY
|
|
Clearing the plan cache causes a recompilation of all subsequent execution plans and can cause a sudden, temporary decrease in query performance. In SQL Server 2005 Service Pack 2, for each cleared cachestore in the plan cache, the SQL Server error log contains the following informational message: "SQL Server has encountered %d occurrence(s) of cachestore flush for the '%s' cachestore (part of plan cache) due to some database maintenance or reconfigure operations". This message is logged every five minutes as long as the cache is flushed within that time interval.
Moving Files
In SQL Server 2005, you can move system or user-defined data and log files by specifying the new location in FILENAME. This may be useful in the following scenarios:
-
Failure recovery. For example, the database is in suspect mode or shutdown caused by hardware failure.
-
Planned relocation.
-
Relocation for scheduled disk maintenance.
For more information, see Moving Database Files.
Initializing Files
By default, data and log files are initialized by filling the files with zeros when you perform one of the following operations:
-
Create a database.
-
Add files to an existing database.
-
Increase the size of an existing file.
-
Restore a database or filegroup.
In SQL Server 2005, data files can be initialized instantaneously. This enables for fast execution of these file operations. For more information, see Database File Initialization.
Changing the Database Collation
Before you apply a different collation to a database, make sure that the following conditions are in place:
-
You are the only one currently using the database.
-
No schema-bound object depends on the collation of the database.
If the following objects, which depend on the database collation, exist in the database, the ALTER DATABASE database_name COLLATE statement will fail. SQL Server will return an error message for each object blocking the ALTER action:
-
User-defined functions and views created with SCHEMABINDING.
-
Computed columns.
-
CHECK constraints.
-
Table-valued functions that return tables with character columns with collations inherited from the default database collation.
-
Changing the database collation does not create duplicates among any system names for the database objects.
The following namespaces may cause the failure of a database collation change if duplicate names result from the changed collation:
-
Object names such as a procedure, table, trigger, or view.
-
Schema names
-
Principals such as a group, role, or user.
-
Scalar-type names such as system and user-defined types.
-
Full-text catalog names.
-
Column or parameter names within an object.
-
Index names within a table.
Duplicate names resulting from the new collation will cause the change action to fail, and SQL Server will return an error message specifying the namespace where the duplicate was found.
Viewing Database Information
You can use catalog views, system functions, and system stored procedures to return information about databases, files, and filegroups. For more information, see Viewing Database Metadata.