CREATE DATABASE AUDIT SPECIFICATION (Transact-SQL)

適用於:SQL ServerAzure SQL 受控執行個體

使用 SQL Server Audit 功能建立資料庫稽核規格物件。 如需詳細資訊,請參閱 SQL Server 稽核 (資料庫引擎)

Transact-SQL 語法慣例

Syntax

CREATE DATABASE AUDIT SPECIFICATION audit_specification_name  
{  
    FOR SERVER AUDIT audit_name   
        [ { ADD ( { <audit_action_specification> | audit_action_group_name } )   
      } [, ...n] ]  
    [ WITH ( STATE = { ON | OFF } ) ]  
}  
[ ; ]  
<audit_action_specification>::=  
{  
      action [ ,...n ]ON [ class :: ] securable BY principal [ ,...n ]  
}  

注意

若要檢視 SQL Server 2014 (12.x) 和舊版的 Transact-SQL 語法,請參閱 舊版檔

引數

audit_specification_name
這是稽核規格的名稱。

audit_name
這是套用此規格的稽核名稱。

audit_action_specification
這是主體針對安全性實體所進行之動作的規格,而這些動作應該記錄在稽核中。

action
這是一或多個資料庫層級可稽核的動作名稱。 如需稽核動作的清單,請參閱 SQL Server Audit 動作群組和動作

audit_action_group_name
這是一或多個資料庫層級可稽核的動作群組名稱。 如需稽核動作群組的清單,請參閱 SQL Server Audit 動作群組和動作

class
這是安全性實體上的類別名稱 (適用的話)。

securable
這是資料庫中套用稽核動作或稽核動作群組的資料表、檢視表或其他安全性實體物件。 如需相關資訊,請參閱 Securables

principal
這是套用稽核動作或稽核動作群組的資料庫主體名稱。 若要稽核所有資料庫主體,請使用資料庫主體 public。 如需詳細資訊,請參閱主體 (資料庫引擎)

WITH ( STATE = { ON | OFF } )
啟用或停用從這個稽核規格收集而來之記錄的稽核。

備註

資料庫稽核規格是位於給定資料庫內的非安全性實體物件。 當建立資料庫稽核規格之後,它就會處於停用狀態。

權限

具有 ALTER ANY DATABASE AUDIT 權限的使用者可以建立資料庫稽核規格,並將其繫結至任何稽核。

建立資料庫稽核規格之後,具有CONTROL SERVER 權限的使用者或或 sysadmin 帳戶,就能檢視此規格。

範例

A. 針對任何資料庫主體,稽核資料表的 SELECT 和 INSERT

下列範例會針對 AdventureWorks2022 資料庫中的 HumanResources.EmployeePayHistory 資料表,依序建立名為 Payrole_Security_Audit 的伺服器稽核,以及名為 Payrole_Security_Audit,會以任何 public 資料庫角色成員身分,稽核 SELECTINSERT 陳述式的資料庫稽核規格。 因為每位使用者恆為 public 角色的成員,因此每位成員都會受到稽核。

USE master ;  
GO  
-- Create the server audit.  
CREATE SERVER AUDIT Payrole_Security_Audit  
    TO FILE ( FILEPATH =   
'D:\SQLAudit\' ) ;  -- make sure this path exists
GO  
-- Enable the server audit.  
ALTER SERVER AUDIT Payrole_Security_Audit   
WITH (STATE = ON) ;  
GO  
-- Move to the target database.  
USE AdventureWorks2022;  
GO  
-- Create the database audit specification.  
CREATE DATABASE AUDIT SPECIFICATION Audit_Pay_Tables  
FOR SERVER AUDIT Payrole_Security_Audit  
ADD (SELECT , INSERT  
     ON HumanResources.EmployeePayHistory BY public )  
WITH (STATE = ON) ;  
GO  

B. 針對特定的資料庫角色,稽核 sales 結構描述中「所有」 物件的任何 DML (INSERT、UPDATE 或 DELETE)

下列範例會針對 AdventureWorks2022 資料庫中的 Sales 資料表,建立稱為 DataModification_Security_Audit 的伺服器稽核,然後建立由具新資料庫角色 SalesUK 使用者稽核 INSERTUPDATEDELETE 陳述式的資料庫稽核規格,其稱為 Audit_Data_Modification_On_All_Sales_Tables

USE master ;  
GO  
-- Create the server audit.
-- Change the path to a path that the SQLServer Service has access to. 
CREATE SERVER AUDIT DataModification_Security_Audit  
    TO FILE ( FILEPATH = 
'D:\SQLAudit\' ) ;  -- make sure this path exists
GO  
-- Enable the server audit.  
ALTER SERVER AUDIT DataModification_Security_Audit   
WITH (STATE = ON) ;  
GO  
-- Move to the target database.  
USE AdventureWorks2022;  
GO  
CREATE ROLE SalesUK
GO
-- Create the database audit specification.  
CREATE DATABASE AUDIT SPECIFICATION Audit_Data_Modification_On_All_Sales_Tables  
FOR SERVER AUDIT DataModification_Security_Audit  
ADD ( INSERT, UPDATE, DELETE  
     ON Schema::Sales BY SalesUK )  
WITH (STATE = ON) ;    
GO  

另請參閱

CREATE SERVER AUDIT (Transact-SQL)
ALTER SERVER AUDIT (Transact-SQL)
DROP SERVER AUDIT (Transact-SQL)
CREATE SERVER AUDIT SPECIFICATION (Transact-SQL)
ALTER SERVER AUDIT SPECIFICATION (Transact-SQL)
DROP SERVER AUDIT SPECIFICATION (Transact-SQL)
CREATE DATABASE AUDIT SPECIFICATION (Transact-SQL)
ALTER DATABASE AUDIT SPECIFICATION (Transact-SQL)
DROP DATABASE AUDIT SPECIFICATION (Transact-SQL)
ALTER AUTHORIZATION (Transact-SQL)
sys.fn_get_audit_file (Transact-SQL)
sys.server_audits (Transact-SQL)
sys.server_file_audits (Transact-SQL)
sys.server_audit_specifications (Transact-SQL)
sys.server_audit_specification_details (Transact-SQL)
sys.database_audit_specifications (Transact-SQL)
sys.database_audit_specification_details (Transact-SQL)
sys.dm_server_audit_status (Transact-SQL)
sys.dm_audit_actions (Transact-SQL)
建立伺服器稽核與伺服器稽核規格