How to make a table Read Only in SQL Server?

前端 未结 3 1094
天涯浪人
天涯浪人 2020-12-08 08:02

I am updating some set of records in that table, after that I need to make this table read only.

So how to make a table Read Only in SQL Server?

3条回答
  •  执笔经年
    2020-12-08 08:21

    A simple alternative that would block update and insert on a specific table but still allowing delete:

    ALTER TABLE mytable WITH NOCHECK ADD CONSTRAINT chk_read_only CHECK( 1 = 0 )
    

    Be aware: this avoids INSERTs and UPDATEs, but allows DELETEs.

    If you really need a table to be truly read only you could also either:

    a) put it in its own database or
    b) put it on a file group and mark that read only, here’s how:

    USE [master]
    
    GO
    
    ALTER DATABASE [csvtosp] ADD FILEGROUP [READONLYTABLES]
    
    GO
    
    ALTER DATABASE [csvtosp] ADD FILE ( NAME = N'mydb_readonly_tables', FILENAME = N'G:\SQL2005DATA\mydb_readonly_tables.ndf' , SIZE = 3072KB , FILEGROWTH = 1024KB ) TO FILEGROUP [READONLYTABLES]
    
    GO
    
    USE csvtosp
    
    GO
    
    DROP TABLE mytable
    
    CREATE TABLE mytable (
    
        somedata    char(8000) not null
    
    ) ON READONLYTABLES
    
    GO
    

    For more details on this subject, go here:

    How to make a table Read Only in SQL Server

提交回复
热议问题