Prepare Interview

Mock Exams

Make Homepage

Bookmark this page

Subscribe Email Address

MSSQL%20Interview%20Questions%20and%20Answers

Question: How to set database to be READ_ONLY in MS SQL Server?
Answer: Databases in SQL Server have two update options:

* READ_WRITE - Data objects are allowed to be queried and modified. This is the default.
* READ_ONLY - Data objects are allowed to be queried, but not allowed to be modified.

You can use the "ALTER DATABASE" to change database update options as shown in the tutorial below:

USE withoutbookDB
GO

INSERT Links (Name) VALUES ('www.withoutbook.com')
GO
(1 rows affected)

ALTER DATABASE withoutbookDB SET READ_ONLY
GO

INSERT Links (Name) VALUES ('www.withoutbook.com')
GO
Msg 3906, Level 16, State 1, Server SQLEXPRESS, Line 1
Failed to update database "withoutbookDB" because
the database is read-only.

SELECT * FROM Links
GO
Name
www.withoutbook.com

ALTER DATABASE withoutbookDB SET READ_WRITE
GO

INSERT Links (Name) VALUES ('www.withoutbook.com')
GO
(1 rows affected)

As you can see from the output, inserting data into a table is not allowed if the database is in READ_ONLY mode.
Is it helpful? Yes No

Most helpful rated by users:

©2024 WithoutBook