search

大家对trigger可能比较熟悉,但Oracle还有一个叫FGA的功能,它的作用和trigger类似,但功能更强大.它的全称是Fine-Grained Audit ,是

大家对trigger可能比较熟悉,但Oracle还有一个叫FGA的功能,它的作用和trigger类似,但功能更强大.它的全称是Fine-Grained Audit ,是Audit的一种特殊方式.使用FGA只要调用Oracle的包DBMS_FGA.ADD_POLICY创建一些policy(审计策略)就行.每个policy只能针对一个表或视图.建好策略后所以对表或视图的DML操作(select,insert,update,delete都可以记录到,当然也可以添加一些筛选条件只监测某些特殊的操作.

补充:所谓审计就是记录你的任意操作,假如你的操作(执行DML语句)符合指定的条件,则你执行的sql语句,将被记录到sys用户下的一些表中,还会将你的其他信息,比如执行时间,用户名,通过什么工具,机器名等.FGA在oracle 9i中就有了,但在9i中只能审计select语句.从10g开始才能审计所有的DML操作

FGA与Triger的明显区别:

1.FGA使用自治的事务,即使DML操作被rollback了,它仍然照样执行不会rollback.而trigger是会被rollback的.

2.做更新操作时trigger可以记录更新前的旧值和更新后的新值,而FGA是不记录旧值.

1.包DBMS_FGA.ADD_POLICY的用法

创建审计策略的语法

DBMS_FGA.ADD_POLICY (

object_schema VARCHAR2, --schema的名字,表或视图的拥有者

object_name VARCHAR2, --对象名,表或视图的名字

policy_name VARCHAR2, --审计策略名字,它和数据库中其他对象一样,需要有一个不重复,唯一的名字

audit_condition VARCHAR2, --筛选条件比如可以选择哪些符合条件的操作被记录

audit_column VARCHAR2, --表中的某一列,可以只记录对表中某一列的操作.如果不指定表示审计所有的列

handler_schema VARCHAR2, --是下面的handler_module的拥有者,其实也只能是创建policy的用户,而上面的object_schema可以是任意用户

handler_module VARCHAR2,--可以是一个一个存储过程或函数,但监测到任何一条符合条件的操作时执行它.

enable BOOLEAN, --true 或false表示policy是开启或关闭状态,如果是false表示不进行审计

statement_types VARCHAR2, --表示哪些操作将被审计,可以填上select,insert,update,delete中的一个或几个

audit_trail BINARY_INTEGER IN DEFAULT,--有参数db,xml表示审计到的信息保存到数据库中或是以xml文件形式保存到磁盘上

audit_column_opts BINARY_INTEGER IN DEFAULT); --这个选项其实只有在audt_column中指定了某列时才起作用.它有any_columns,all_columns两个选项假如表中有eno,ename两列,并在audit_column中指定了这两列,那么选any_columns表示只要操作其中的任意一列都将被记录,而这里指定all_columns的话是说只有一个sql语句同时操作了这两列才被记录

 

举例,假如创建表:create table temp(eno int,ename varchar2(30));针对这个表创建policy

每个策略只能针对一个表或视图,而一个表或视图可能对应多个策略.当表被删除时策略也随之默认被删除

BEGIN
SYS.DBMS_FGA.ADD_POLICY (
object_schema => 'ARWEN'
,object_name => 'TEMP'
,policy_name => 'FGA_TEMP'

,audit_condition => NULL
,audit_column => eno,ename
,handler_schema => null
,handler_module => null

,enable => TRUE
,statement_types =>'SELECT,INSERT,UPDATE,DELETE'
,audit_trail => SYS.DBMS_FGA.DB+SYS.DBMS_FGA.EXTENDED

--DBMS_FGA.DB表示记录将被保存到数据库中,DBMS_FGA.EXTENDED表示如果sql语句中带有绑定变量也会被记录下来.

--如果是这样选audit_trail => SYS.DBMS_FGA.DB表示不会记录绑定变量

--SYS.DBMS_FGA.DB+SYS.DBMS_FGA.EXTENDED改成SYS.DBMS_FGA.XML+SYS.DBMS_FGA.EXTENDED表示记录保存成xml文件

--xml文件所在目录可以通过SHOW PARAMETER AUDIT_FILE_DEST查看,如果要更改目录ALTER SYSTEM SET AUDIT_FILE_DEST = directory_path DEFERRED;

,audit_column_opts => SYS.DBMS_FGA.ALL_COLUMNS)
END;

 

查看创建好的policy对象和符合审计条件时被记录的操作

我们可以通过select * from dba_objects查看table,view等对象,类似的policy创建好后我们可以通过SELECT * FROM DBA_AUDIT_POLICIES来查看.

如果我们对表temp执行了DML操作,那些信息将会被操作到sys用户下的表中,Select* from sys.dba_fga_audit_trail可以查找到.(注意这只有前面设置将记录信息

保存到数据库才能这样查,如果保存到xml文件中可以Select *from V$XML_AUDIT_TRAIL)

 

2.删除审计策略,假如删除上面创建的策略

begin

SYS.DBMS_FGA.DROP_POLICY (

object_schema => 'ARWEN'

,object_name => 'TEMP'

,policy_name => 'FGA_TEMP'

);

end;

3.使用handler_module

 

假如你想在审计到某些操作时还进行进一步的处理,比如把信息写到自己创建的日志中,或者发送邮件通知相关人员.就要在创建policy时使用handler_module的功能,指定一个存储过程去做相应的处理.假如我创建一个存储过程temp_handler

CREATE OR REPLACE PROCEDURE temp_handler

( v_object_schema VARCHAR2

, v_object_name VARCHAR2

, v_policy_name VARCHAR2

)

IS

v_temp varchar2(30);

begin

null;

end temp_handler;

--这里的存储过程有点特殊,它必须带v_object_schema VARCHAR2, v_object_name VARCHAR2, v_policy_name VARCHAR2这三个参数才行,如果直接写一个一般的存储过程就会出错的.在policy中调用存储过程就这样 写

 

BEGIN
SYS.DBMS_FGA.ADD_POLICY (
object_schema => 'ARWEN'
,object_name => 'TEMP'
,policy_name => 'FGA_TEMP'

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
MySQL: Essential Skills for Beginners to MasterMySQL: Essential Skills for Beginners to MasterApr 18, 2025 am 12:24 AM

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL: Structured Data and Relational DatabasesMySQL: Structured Data and Relational DatabasesApr 18, 2025 am 12:22 AM

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL: Key Features and Capabilities ExplainedMySQL: Key Features and Capabilities ExplainedApr 18, 2025 am 12:17 AM

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

The Purpose of SQL: Interacting with MySQL DatabasesThe Purpose of SQL: Interacting with MySQL DatabasesApr 18, 2025 am 12:12 AM

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

MySQL for Beginners: Getting Started with Database ManagementMySQL for Beginners: Getting Started with Database ManagementApr 18, 2025 am 12:10 AM

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA

MySQL's Role: Databases in Web ApplicationsMySQL's Role: Databases in Web ApplicationsApr 17, 2025 am 12:23 AM

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

MySQL: Building Your First DatabaseMySQL: Building Your First DatabaseApr 17, 2025 am 12:22 AM

The steps to build a MySQL database include: 1. Create a database and table, 2. Insert data, and 3. Conduct queries. First, use the CREATEDATABASE and CREATETABLE statements to create the database and table, then use the INSERTINTO statement to insert the data, and finally use the SELECT statement to query the data.

MySQL: A Beginner-Friendly Approach to Data StorageMySQL: A Beginner-Friendly Approach to Data StorageApr 17, 2025 am 12:21 AM

MySQL is suitable for beginners because it is easy to use and powerful. 1.MySQL is a relational database, and uses SQL for CRUD operations. 2. It is simple to install and requires the root user password to be configured. 3. Use INSERT, UPDATE, DELETE, and SELECT to perform data operations. 4. ORDERBY, WHERE and JOIN can be used for complex queries. 5. Debugging requires checking the syntax and use EXPLAIN to analyze the query. 6. Optimization suggestions include using indexes, choosing the right data type and good programming habits.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor