適用於:SQL Server
Azure SQL 受控執行個體
使用 SQL Server Audit 功能建立資料庫稽核規格物件。 如需詳細資訊,請參閱 SQL Server 稽核 (資料庫引擎)。
語法
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 ]
}
引數
audit_specification_name
稽核規格的名稱。
audit_name
套用此規格的稽核名稱。
audit_action_specification
應記錄在稽核中之主體的安全性實體動作規格。
action
一或多個資料庫層級可稽核動作的名稱。 如需稽核動作的清單,請參閱 SQL Server Audit 動作群組和動作。
audit_action_group_name
一或多個資料庫層級可稽核動作群組的名稱。 如需稽核動作群組的清單,請參閱 SQL Server Audit 動作群組和動作。
class
安全性實體上的類別名稱(如果適用的話)。
securable
要套用稽核動作或稽核動作群組之資料庫中的數據表、檢視或其他安全性實體物件。 如需相關資訊,請參閱 Securables。
principal
要套用稽核動作或稽核動作群組的資料庫主體名稱。 若要稽核所有資料庫主體,請使用 公用 資料庫主體。 如需詳細資訊,請參閱主體 (資料庫引擎)。
WITH ( STATE = { ON |OFF }
啟用或停用從這個稽核規格收集而來之記錄的稽核。
備註
資料庫稽核規格是位於給定資料庫內的非安全性實體物件。 資料庫稽核規格在建立之後就會處於停用狀態。
權限
具有 ALTER ANY DATABASE AUDIT 權限的使用者可以建立資料庫稽核規格,並將其繫結至任何稽核。
建立資料庫稽核規格之後,具有 CONTROL SERVER 許可權或 sysadmin 帳戶的使用者就可以檢視它。
範例
本文中的程式碼範例使用AdventureWorks2025了 or AdventureWorksDW2025 範例資料庫,你可以從 Azure Data SQL 範例倉庫的 GitHub 倉庫下載。
A. 審計 SELECT 以及 INSERT 任何資料庫主體的表格
下列範例會針對 數據表,建立名為 Payroll_Security_Audit 的伺服器稽核,然後建立稱為 Payroll_Security_Audit 的資料庫稽核規格,SELECT該INSERT規格會由任何公用資料庫角色成員稽核和HumanResources.EmployeePayHistory語句。 每位用戶都會稽核,因為每個使用者一律是公用角色的成員。
USE master;
GO
-- Create the server audit.
CREATE SERVER AUDIT Payroll_Security_Audit
TO FILE (FILEPATH = 'D:\SQLAudit\'); -- make sure this path exists
GO
-- Enable the server audit.
ALTER SERVER AUDIT Payroll_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 Payroll_Security_Audit ADD (
SELECT, INSERT ON HumanResources.EmployeePayHistory BY PUBLIC
)
WITH (STATE = ON);
GO
B. 稽核特定資料庫角色架構中所有物件的任何數據修改
下列範例會針對架構中的所有DataModification_Security_Audit物件,建立名為 Audit_Data_Modification_On_All_Sales_Tables 的伺服器稽核,然後建立名為的資料庫稽核規格,由INSERTUPDATE使用者稽DELETE核 、SalesUK、 和 Sales 語句。
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 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)
目錄檢視和 DMV:
- 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)