0% found this document useful (0 votes)
13 views6 pages

9.0 Database States

Database states in sql server

Uploaded by

stanza8886
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views6 pages

9.0 Database States

Database states in sql server

Uploaded by

stanza8886
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Database State

A SQL Server database state specifies the current running mode of that
database.

A database is always in one specific state.

There are seven main states in which a SQL Server database can exit.

Methods to determine the status of a SQL Server database


To verify the current state of a database, select the state_desc column in
the sys.databases catalog view OR

The Status property in the DATABASEPROPERTYEX function.

1 - Sys.databases catalog view

--Name and state for all databases hosted in the current SQL Server instance
SELECT * FROM sys.databases

SELECT name, State_desc FROM sys.databases

[OR]

2: DATABASEPROPERTYX function
The DATABASEPROPERTYX function only allows you to see one element at
a time.

-- can see the Status for the master database by issuing the below query

SELECT DATABASEPROPERTYEX('master', 'Status')

SELECT DATABASEPROPERTYEX('master', 'Status') AS DBStatus


Database State Definitions
The following table defines the database states.

DATABASE STATE DEFINITIONS


State Definition
ONLINE Database is available for user access and functioning normally.

The primary filegroup is online, although the undo phase of recovery may not have
been completed.

ALTER DATABASE [DBName] SET ONLINE

OFFLINE Database is unavailable. A database becomes offline by explicit user action and
remains offline until additional user action is taken.

It make your database unavailable to use and also release any resources (like memory)
it is consuming.

The best part of the offline database is that as the database stay visible in SSMS, we
can bring it back online very quickly.

For example:
1) The database may be taken offline in order to move a file to a new disk. The
database is then brought back online after the move has been completed.

2) To prevent users from accessing the db.

ALTER DATABASE [DBName] SET OFFLINE

RESTORING One or more files of the primary filegroup are being restored, or one or more
secondary files are being restored offline. The database is unavailable.

RECOVERING Database is being recovered. The recovering process is a transient state;

The database will automatically become online if the recovery succeeds.

If the recovery fails, the database will become suspect.

The database is unavailable.

RECOVERY SQL Server has encountered a resource-related error during recovery.


PENDING
The database is not damaged, but files may be missing or system resource
limitations may be preventing it from starting.
DATABASE STATE DEFINITIONS
State Definition
The database is unavailable.

Additional action by the user is required to resolve the error and let the recovery
process be completed.

SUSPECT At least the primary filegroup is suspect and may be damaged.

The database cannot be recovered during start-up of SQL Server.

The database is unavailable.

Additional action by the user is required to resolve the problem.

EMERGENCY User has changed the database and set the status to EMERGENCY.

The database is in single-user mode and may be repaired or restored.


The database is marked READ_ONLY, logging is disabled, and access is limited to
members of the sysadmin fixed server role.
EMERGENCY is primarily used for troubleshooting purposes.

For example, a database marked as suspect can be set to the EMERGENCY state.
This could permit the system administrator read-only access to the database. Only
members of the sysadmin fixed server role can set a database to the EMERGENCY
state.

ALTER DATABASE SuspectDBDemo SET EMERGENCY

Read only databases:


Databases whose data is not required to be changed should be
considered to be set as READ ONLY.
Databases can be set to READ ONLY mode and back using T-
SQL and SSMS.

When it is generally required to put the databases to read only mode?

-> Data migration activities


-> historical reporting purposes

--Verify that DB is READ ONLY or not


-- A value of 1 corresponds to READ ONLY state

SELECT name, is_read_only

FROM sys.databases

WHERE name = 'AdventureWorks'

---Make Database Read Only

USE [master]

GO

ALTER DATABASE [DBNAME] SET READ_ONLY WITH NO_WAIT

---Make Database Read_Write

USE [master]
GO

ALTER DATABASE [DBNAME] SET READ_WRITE WITH NO_WAIT

GUI:

Once a database is changed to READ ONLY nothing can change


in it.

So certain changes should be made to optimize the


performance of a READ ONLY database.

1) Permissions cannot be edited from a READ ONLY database.


2) Users cannot be added or removed from a READ ONLY
database.
3) Statistics will not be automatically updated (not required)
and you would not be able to update statistics of a READ ONLY
database.
4) READ ONLY databases will not shrink automatically neither
manually
5) You cannot create indexes on a READ ONLY database.
6) You cannot defragment indexes of a READ ONLY database
Hence we should complete such tasks before we set the
database to READ ONLY mode.

The statuses are pretty self-explanatory:

ONLINE = Database is available for query.

OFFLINE = Database was explicitly taken offline.

RESTORING = Database is being restored.

RECOVERING = Database is recovering and not yet ready for queries.

SUSPECT = Database did not recover.

EMERGENCY = Database is in an emergency, read-only state. Access is


restricted to sysadmin members

https://www.concurrency.com/blog/november-2018/creating-a-suspect-sql-
database?feed=Blog

You might also like