設定SQLServer資料庫中某些表為唯讀多種方法分享_MsSql

來源:互聯網
上載者:User
一般情況下會有幾種情況需要你把資料庫設為唯讀:
1. Insert,Update,Delete 觸發器
2. Check 約束 和 Delete 觸發器
3. 設定資料庫為唯讀
4. 把表放到唯讀檔案組中
5. 拒絕對象層級許可權
6. 建立視圖
在開始之前,先建立一個資料庫及表作為樣本:
複製代碼 代碼如下:

create database MyDB
create table tblEvents
(
id int,
logEvent varchar(1000)
)
insert into tblEvents
values (1, 'Password Changed'), (2, 'User Dropped'), (3, 'Finance Data Changed')

nsert/Update/Delete觸發器:
請注意這裡使用的是INSTEADOF trigger,因為如果你使用了AFTER trigger,會在執行DELETE, UPDATE和INSERT語句時請求鎖,會對寫交易記錄和復原操作造成效能上的影響
複製代碼 代碼如下:

CREATE TRIGGER trReadOnly_tblEvents ON tblEvents
INSTEAD OF INSERT,
UPDATE,
DELETE
AS
BEGIN
RAISERROR( 'tblEvents table is read only.', 16, 1 )
ROLLBACK TRANSACTION
END

當使用者執行insert/update/delete時,將提示以下錯誤:
Msg 50000, Level 16, State 1, Procedure trReadOnly_tblEvents, Line 7tblEvents table is read only.Msg 3609, Level 16, State 1, Line 1The transaction ended in the trigger. The batch has been aborted.

使用 Check 約束和Delete 觸發器:
現在先在表中添加一個check 約束“1=0”,意味著總是失敗。它禁止你在任何行執行INSERT或者Delete操作。
首先,先禁用在上一步建立的觸發器:disable trigger trReadOnly_tblEvents on tblevents然後,添加約束:ALTER TABLE tblEvents WITH NOCHECK ADD CONSTRAINT chk_read_only_tblEvent CHECK( 1 = 0 )執行以後,無論你執行任何一個INSERT/UPDATE語句,都將提示以下錯誤資訊:
Msg 547, Level 16, State 0, Line 1
The UPDATE statement conflicted with the CHECKconstraint "chk_read_only_tblEvent". The conflict occurred indatabase "MyDB", table "dbo.tblEvents".
The statement has been terminated.
但是,該約束不會對DELETE操作造成影響,為此,需要再建立一個DDL觸發器:
複製代碼 代碼如下:

CREATE TRIGGER trReadOnlyDel_tblEvents ON tblEvents
INSTEAD OF
DELETE
AS
BEGIN
RAISERROR( 'tblEvents table is read only.', 16, 1 )
ROLLBACK TRANSACTION
END

設定資料庫為唯讀:
你可以設定資料庫為唯讀,這樣就禁止對整個資料庫的DDL/DML操作。可以使用以下語句:
複製代碼 代碼如下:

USE [master]
GO
ALTER DATABASE [MyDB] SET READ_ONLY WITH NO_WAIT
GO

把表放到唯讀檔案組:
可以在一個唯讀檔案組中建立一個表:
複製代碼 代碼如下:

USE [master]
GO
ALTER DATABASE [MyDB] ADD FILEGROUP [READ_ONLY_TBLS]
GO
ALTER DATABASE [MyDB] ADD FILE ( NAME = N'mydb_readonly_tables', FILENAME = N'C:\JSPACE\myDBReadOnly.ndf' , SIZE = 2048KB , FILEGROWTH = 1024KB ) TO FILEGROUP [READ_ONLY_TBLS]
GO
DROP table tblEvents
create table tblEvents
(
id int,
logEvent varchar(1000)
)
ON [READ_ONLY_TBLS]
ALTER DATABASE [MyDB] MODIFY FILEGROUP [READ_ONLY_TBLS] READONLY
任何對錶的DML操作都會被拒絕,並返回以下錯誤資訊:
Msg 652, Level 16, State 1, Line 1
The index "" for table "dbo.tblEvents" (RowsetId 72057594038845440) resides on a read-only filegroup ("READ_ONLY_TBLS"), which cannot be modified.

拒絕對象層級許可權
可以通過DCL命令控制使用者權限,但此步無法限制進階許可權使用者(如system admin,DatabaseOwner):
複製代碼 代碼如下:

DENY INSERT, UPDATE, DELETE ON tblEvents TO Jugal
DENY INSERT, UPDATE, DELETE ON tblEvents TO Public

建立視圖
為了替代直接存取表,可以使用視圖:
複製代碼 代碼如下:

create view vwtblEvents
as
select ID, Logevent from tblEvents
union all
select 0, '0' where 1=0

在這個視圖中,使用了UNION,只有在你確保有對應數量的列時才使用。在這個例子中,表有兩列,所以使用兩個輸出資料行。同時,你也應該確保資料類型也一致。
當一個使用者嘗試通過INSERT/UPDATE/DELETE操作資料時,將得到以下錯誤資訊:
Msg 4406, Level 16, State 1, Line 1Update or insert of view or function 'vwtblEvents1' failed because it contains a derived or constant field.Msg 4426, Level 16, State 1, Line 1View'vwtblEvents1' is not updatable because the definition contains a UNIONoperator.


最後一步:
確認是否有必要用這些步驟來設定表為唯讀。
如果一個表總是唯讀,那麼你應該放到唯讀檔案組中。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.