You can use an abundance of
database options to refine the behavior of a database. These options
fall into the following categories, which are part of the option
specification:
Auto Options
Cursor Options
Database Availability Options
Date Correlation Optimization Options
External Access Options
Parameterization Options
Recovery Options
Service Broker Options
Snapshot Isolation Options
SQL Options
The following section focuses on the database options displayed on the Options page in SSMS.
The Database Options
You can access many of the
most common database options via the Options page of the Database
Properties dialog. To get to this dialog, you right-click a database in
the SSMS Object Explorer and select Properties. When the dialog appears,
you select the Options page from the list on the left side of the
Database Properties dialog. Figure 1 shows the Options page for the AdventureWorks2008
database. The options listed under Other Options can be listed
alphabetically or by category. The default display mode is by category.
The default settings for these
options suffice for most installations. However, some options deserve
special attention. The options listed under the Automatic category are
among these options. The Auto Close option could cause problems in prior
versions of SQL Server. This option is intended for desktop
implementations in which the database does not need to be online all the
time. When users are not accessing the database and this option is
selected, the database files are closed. When the first user accesses
the database, the database is brought back online. The problem in prior
versions was that the synchronous operation of opening and closing the
database files caused performance problems. This issue has been
addressed in SQL Server 2008 because the operations are now performed
asynchronously. The Auto Close option defaults to true only for SQL Server 2008 Express Edition and should generally be left set to false for all other versions.
The Auto Create
Statistics and Auto Update Statistics options also deserve special
attention in situations in which the creation or updating of statistics
is affecting performance. Generally, the creation or updating of
statistics improves performance. These statistics enable the Query
Optimizer to make the best decisions when determining the access path to
the data. In some rare circumstances, there may be performance problems
at the time statistics are created or updated automatically. When these
situations arise, you can turn off the Auto Statistics options and
schedule the statistics operations to occur during off-hours.
Enabling the Auto Shrink
option is a good idea for keeping a nonproduction database as small as
possible. This option automatically performs a database shrink operation
against the database files when more than 25% of a file contains unused
space. The default setting is false
because this option can cause performance problems (related to the
timing of the shrink operation) in a production database. Because the
operation is automatic, it can run at any time, including times when
there may be heavy production load.
The Page Verify option in the Recovery category was enhanced in SQL Server 2005. That enhancement came in the form of a new CHECKSUM option. This CHECKSUM
option is the default; it causes a checksum calculation to occur across
the entire database page. Prior to the availability of the CHECKSUM option, page verification was done with TORN_PAGE_DETECTION. Both of these options help detect damaged database pages, but CHECKSUM is the method that Microsoft recommends. The CHECKSUM
calculation can be complicated but it basically tells SQL Server to
calculate a number based on the contents of each data / index page. The CHECKSUM
value is stored in the page header when it is written to disk. When the
page is read from the disk, the checksum is computed again and compared
to the value in page header to help ensure that the contents of each
page are valid.
Database Read-Only and
Restrict Access are two other commonly used options in the State
category. You can set Database Read-Only to true
to prevent updates from occurring in the database. Databases used for
reference and not updated are perfect candidates for this option. The
Restrict Access options are handy when you’re executing system
maintenance or mass updates in which you want to restrict users from
accessing the database. Single User allows only one user to access the
database. The Restricted option allows only members of db_owner, dbcreator, and sysadmin to access the database. With the Restricted option, there is no limit on the number of users in these groups that can access the database.
You can easily set up the
options reviewed in this section as well as the other options mentioned
by using the Database Properties dialog. The current value for each
option is shown in the right-hand column. To set an option to another
value, you click the current value, and a drop-down arrow appears. When
you click the drop-down arrow, you can select from the list of valid
values for the option. After making all your option changes, you can
click OK for the changes to take effect immediately, or you can click
the Script button to generate the T-SQL code to change the options. The
T-SQL code used to change the options is discussed in the next section.
Using T-SQL to Set Database Options
If you prefer to use T-SQL, or if the option you need to set doesn’t appear in the Database Properties dialog, you can use the ALTER DATABASE command to set options. For example, the following command sets AUTO_UPDATE STATISTICS to OFF in the AdventureWorks2008 database:
ALTER DATABASE [AdventureWorks2008] SET AUTO_UPDATE_STATISTICS OFF WITH NO_WAIT
You can also change some of the options by using the system stored procedure sp_dboption.
This feature is scheduled to be removed in a future release of SQL
Server but is still available in this release. You might still be using
this procedure based on prior releases, and old habits are hard to
break. Following is an example of one of the sp_dboption commands many people have been using for years:
EXEC sp_dboption 'AdventureWorks2008', 'single user', 'TRUE'
This command sets the AdventureWorks2008
database to single-user mode. Setting a database to single-user mode is
useful when you’re performing certain database operations. For example,
you might use sp_dboption to set a database to single-user mode prior to renaming the database with the sp_renamedb system procedure. It is important to break old habits and move on to using the ALTER DATABASE command. The single-user option and database name change have both been integrated into the ALTER DATABASE syntax. The following example shows how to set the single-user mode option and change the database name by using ALTER DATABASE:
ALTER DATABASE [AdventureWorks2008] SET SINGLE_USER WITH NO_WAIT
GO
ALTER DATABASE [AdventureWorks2008] MODIFY NAME = [AdventureWorks2008_New]
GO
As you can see, using ALTER DATABASE is fairly straightforward and offers a consistent approach for modifying a database and its options.
Tip
Databases can be brought offline in SQL Server 2008 using SSMS or the T-SQL ALTER DATABASE
command. When databases are offline no one can access them and the
related database files can be moved. For example, you use the following
T-SQL command to take the AdventureWorks2008 database offline:
ALTER DATABASE [AdventureWorks2008] SET OFFLINE WITH NO_WAIT
You can also specify an option with the ALTER DATABASE
command that sets the database into an emergency state. This state
marks the database as read-only, logging is disabled, and access to the
database is limited to members of the sysadmin
fixed server role. This option quickly prevents normal users from
getting at the database but leaves the database available for inquiry
for administrators. This is particularly useful when a database had been
marked as suspect and is inaccessible. An example of setting a database
to the emergency state follows:
ALTER DATABASE [AdventureWorks2008] SET emergency WITH NO_WAIT.
The offline option and emergency options can be invaluable when you want to quickly prevent or limit access to you database.
Retrieving Option Information
You can retrieve
database settings by using several different methods. You can use the
Database Properties dialog in SSMS to display commonly accessed options. You can also use the DATABASEPROPERTYEX function or the sp_dboption system stored procedure to display individual database options. As mentioned previously, sp_dboption is slated for removal in a future release, so the DATABASEPROPERTYEX
function is preferred. This function accepts input values for the
database name and the option for which you want to retrieve the value.
The following is an example of a SELECT statement you can use to retrieve the Auto Shrink option for the AdventureWorks2008 database:
SELECT DATABASEPROPERTYEX ('AdventureWorks2008', 'IsAutoShrink')
This function returns a value of 1 or 0 for Boolean values—with 1 being “on” or “true”—and returns the actual value for non-Booleans. Table 1 lists the valid properties for the DATABASEPROPERTYEX function.
Table 1. DATABASEPROPERTYEX Properties
Property | Explanation |
---|
Collation | This is the default collation name for the database. |
ComparisonStyle | This is the Windows comparison style of the collation. |
IsAnsiNullDefault | The database follows SQL-92 rules for allowing null values. |
IsAnsiNullsEnabled | All comparisons to a null evaluate to unknown. |
IsAnsiPaddingEnabled | Strings are padded to the same length before comparison or insertion. |
IsAnsiWarningsEnabled | Error or warning messages are issued when standard error conditions occur. |
IsArithmeticAbortEnabled | Queries are ended when an overflow or divide-by-zero error occurs during query execution. |
IsAutoClose | The database shuts down cleanly and frees resources after the last user exits. |
IsAutoCreateStatistics | Existing statistics are automatically updated when the statistics become out-of-date because the data in the tables has changed. |
IsAutoShrink | Database files are candidates for automatic periodic shrinking. |
IsAutoUpdateStatistics | The AUTO_UPDATE_STATISTICS database option is enabled. |
IsCloseCursorsOnCommitEnabled | Cursors that are open when a transaction is committed are closed. |
IsFulltextEnabled | The database is full-text enabled. |
IsInStandBy | The database is online as read-only, with the restore log allowed. |
IsLocalCursorsDefault | Cursor declarations default to LOCAL. |
IsMergePublished | The tables in a database can be published for merge replication, if replication is installed. |
IsNullConcat | The null concatenation operand yields NULL. |
IsNumericRoundAbortEnabled | Errors are generated when loss of precision occurs in expressions. |
IsParameterizationForced | The PARAMETERIZATION database SET option is FORCED. |
IsPublished | The tables of the database can be published for snapshot or transactional replication, if replication is installed. |
IsQuotedIdentifiersEnabled | Double quotation marks can be used on identifiers. |
IsRecursiveTriggersEnabled | Recursive firing of triggers is enabled. |
IsSubscribed | The database is subscribed to a publication. |
IsSyncWithBackup | The
database is either a published database or a distribution database and
can be restored without disrupting transactional replication. |
IsTornPageDetectionEnabled | The SQL Server database engine detects incomplete I/O operations caused by power failures or other system outages. |
LCID | This is the Windows locale ID (LCID) for the collation. |
Recovery | This is the recovery model for the database. |
SQLSortOrder | This indicates the SQL Server sort order ID supported in earlier versions of SQL Server. |
Status | This is the database status. |
Updateability | This indicates whether data can be modified. |
UserAccess | This indicates which users can access the database. |
Version | This
is the internal version number of the SQL Server code with which the
database was created. It is for internal use only by SQL Server tools
and in upgrade processing. |
If
you would like to retrieve all the options set for a database, you have
a couple of choices. The option that has been around for a while is sp_helpdb.
You can pass to this system stored procedure the database name, and it
returns several pieces of information about the database, including the
options set. The database options are returned in the first result set
from sp_helpdb in a column named
Status. The database options are displayed in a comma-delimited format
in the Status column. All Boolean options that are set to ON are returned in the Status column, and all non-Boolean values are returned with the value to which they are set.
The syntax for sp_helpdb is as follows:
The sys.databases
catalog view is another good resource for displaying all the database
options. This catalog view has a separate column for each of the
database options and is much easier to read than the sp_helpdb
output. The view also has the added flexibility of allowing you to
choose a set of options to return. The following example shows a SELECT statement that uses the sys.databases catalog view to return a common set of options:
select name, is_auto_close_on, is_auto_shrink_on,
is_auto_create_stats_on, is_auto_update_stats_on
from sys.databases
where name = 'AdventureWorks2008'
The results from this SELECT statement return Boolean values in each column, indicating whether the option is set to on or off. The number of columns available for selection is extensive and similar to those options available with the DATABASEPROPERTYEX function.
Tip
Selecting the column you want from the sys.databases catalog view is easier when you use the Object Explorer. To set it, you go to the master database and expand the Views node, followed by the System Views node. When you see the sys.databases view listed under System Views, you expand the columns for the sys.databases
view to see a list of all the available columns. You can then drag the
options you want to view into a database query window for use in a SELECT statement.
You can also use the new IntelliSense feature available in the SQL Server 2008 query window. When creating a SELECT statement that retrieves from the sys.databases
catalog view (or any other catalog view), you are given a drop-down
list of available columns when you reference the view in the select
list.