SQL - 查找插入特定值的语句

Posted

技术标签:

【中文标题】SQL - 查找插入特定值的语句【英文标题】:SQL - Find statement that insert specific values 【发布时间】:2019-06-18 10:39:19 【问题描述】:

我正在使用 Oracle 数据库,通过 Oracle SQL Developer 和 R 与它进行交互。这个数据库由几个人共享,但由于某些公司规则,我们都使用同一个用户与之交互。

在我们的一个表中,我们发现了一种奇怪的行为,即不断插入一行而没有任何我们知道的顺序。

比如说,在 Table1 上,有时会有一行 value Name = "Test" 创建 >8K 次,我们不知道为什么会这样。

我想做的是创建某种自动化(触发器),当在 Table1 上插入带有 Name = "Test" 的行时,会保存执行它的查询和日期/时间。

【问题讨论】:

为什么有一个规则要求共享帐户?通常有一个规则禁止共享帐户,以防止此类情况。 【参考方案1】:

如果有搜索期间的存档日志。您必须使用实用程序 LogMiner。示例 1。

EXECUTE DBMS_LOGMNR.add_logfile(LOGFILENAME => '/oracle/app/oracle/product/11.2/admin/edcu/arc_redo_log/1_39306_769799469.dbf', OPTIONS => DBMS_LOGMNR.NEW); 
EXECUTE DBMS_LOGMNR.add_logfile(LOGFILENAME => '/oracle/app/oracle/product/11.2/admin/edcu/arc_redo_log/1_39307_769799469.dbf', OPTIONS => DBMS_LOGMNR.addfile); 
EXECUTE DBMS_LOGMNR.add_logfile(LOGFILENAME => '/oracle/app/oracle/product/11.2/admin/edcu/arc_redo_log/1_39308_769799469.dbf', OPTIONS => DBMS_LOGMNR.addfile); 
EXECUTE DBMS_LOGMNR.add_logfile(LOGFILENAME => '/oracle/app/oracle/product/11.2/admin/edcu/arc_redo_log/1_39309_769799469.dbf', OPTIONS => DBMS_LOGMNR.addfile); 
EXECUTE DBMS_LOGMNR.START_LOGMNR(OPTIONS => DBMS_LOGMNR.DICT_FROM_ONLINE_CATALOG);

SELECT 
to_char(timestamp,'DD-MM-YYYY HH24:MI:SS'),
operation,username,  
os_username,
machine_name,
session_info,
sql_redo

 FROM v$logmnr_contents
where 
seg_owner='MANAGER' and  seg_name='TEST2'

14-12-2017 09:40:20 DDL     MANAGER DeminDV KRW\IVC-PTK-DEMIN   login_username=MANAGER client_info= OS_username=DeminDV Machine_name=KRW\IVC-PTK-DEMIN  create table test2 (p1 number);
14-12-2017 09:40:47 INSERT  MANAGER DeminDV KRW\IVC-PTK-DEMIN   login_username=MANAGER client_info= OS_username=DeminDV Machine_name=KRW\IVC-PTK-DEMIN  insert into "MANAGER"."TEST2"("COL 1") values (HEXTORAW('c117'));
14-12-2017 09:40:53 INSERT  MANAGER DeminDV KRW\IVC-PTK-DEMIN   login_username=MANAGER client_info= OS_username=DeminDV Machine_name=KRW\IVC-PTK-DEMIN  insert into "MANAGER"."TEST2"("COL 1") values (HEXTORAW('c119'));
14-12-2017 09:40:57 INSERT  MANAGER DeminDV KRW\IVC-PTK-DEMIN   login_username=MANAGER client_info= OS_username=DeminDV Machine_name=KRW\IVC-PTK-DEMIN  insert into "MANAGER"."TEST2"("COL 1") values (HEXTORAW('c137'));
14-12-2017 09:41:01 INSERT  MANAGER DeminDV KRW\IVC-PTK-DEMIN   login_username=MANAGER client_info= OS_username=DeminDV Machine_name=KRW\IVC-PTK-DEMIN  insert into "MANAGER"."TEST2"("COL 1") values (HEXTORAW('c20219'));
14-12-2017 09:41:45 DDL     MANAGER DeminDV KRW\IVC-PTK-DEMIN   login_username=MANAGER client_info= OS_username=DeminDV Machine_name=KRW\IVC-PTK-DEMIN  alter table test2 add (p2 varchar2(200));
14-12-2017 09:42:12 INSERT  MANAGER DeminDV KRW\IVC-PTK-DEMIN   login_username=MANAGER client_info= OS_username=DeminDV Machine_name=KRW\IVC-PTK-DEMIN  insert into "MANAGER"."TEST2"("COL 1","COL 2") values (HEXTORAW('c20219'),HEXTORAW('746573743220746573743120'));
14-12-2017 09:42:24 INSERT  MANAGER DeminDV KRW\IVC-PTK-DEMIN   login_username=MANAGER client_info= OS_username=DeminDV Machine_name=KRW\IVC-PTK-DEMIN  insert into "MANAGER"."TEST2"("COL 1","COL 2") values (HEXTORAW('c2021a'),HEXTORAW('746573743420746573743420'));
14-12-2017 09:46:24 INSERT  MANAGER DeminDV KRW\IVC-PTK-DEMIN   login_username=MANAGER client_info= OS_username=DeminDV Machine_name=KRW\IVC-PTK-DEMIN  insert into "MANAGER"."TEST2"("COL 1","COL 2") values (HEXTORAW('c2021a'),HEXTORAW('746573743420746573743420'));
14-12-2017 09:46:25 INSERT  MANAGER DeminDV KRW\IVC-PTK-DEMIN   login_username=MANAGER client_info= OS_username=DeminDV Machine_name=KRW\IVC-PTK-DEMIN  insert into "MANAGER"."TEST2"("COL 1","COL 2") values (HEXTORAW('c2021a'),HEXTORAW('746573743420746573743420'));
14-12-2017 09:46:26 INSERT  MANAGER DeminDV KRW\IVC-PTK-DEMIN   login_username=MANAGER client_info= OS_username=DeminDV Machine_name=KRW\IVC-PTK-DEMIN  insert into "MANAGER"."TEST2"("COL 1","COL 2") values (HEXTORAW('c2021a'),HEXTORAW('746573743420746573743420'));
14-12-2017 09:46:27 INSERT  MANAGER DeminDV KRW\IVC-PTK-DEMIN   login_username=MANAGER client_info= OS_username=DeminDV Machine_name=KRW\IVC-PTK-DEMIN  insert into "MANAGER"."TEST2"("COL 1","COL 2") values (HEXTORAW('c2021a'),HEXTORAW('746573743420746573743420'));
14-12-2017 09:46:28 INSERT  MANAGER DeminDV KRW\IVC-PTK-DEMIN   login_username=MANAGER client_info= OS_username=DeminDV Machine_name=KRW\IVC-PTK-DEMIN  insert into "MANAGER"."TEST2"("COL 1","COL 2") values (HEXTORAW('c2021a'),HEXTORAW('746573743420746573743420'));
14-12-2017 09:54:37 DDL     MANAGER DeminDV KRW\IVC-PTK-DEMIN   login_username=MANAGER client_info= OS_username=DeminDV Machine_name=KRW\IVC-PTK-DEMIN   comment on table test2 is  'test';
14-12-2017 10:16:36 DDL     MANAGER DeminDV KRW\IVC-PTK-DEMIN   login_username=MANAGER client_info= OS_username=DeminDV Machine_name=KRW\IVC-PTK-DEMIN  alter table test2 add  (p3 varchar2(100));
14-12-2017 10:17:07 INSERT  MANAGER DeminDV KRW\IVC-PTK-DEMIN   login_username=MANAGER client_info= OS_username=DeminDV Machine_name=KRW\IVC-PTK-DEMIN  insert into "MANAGER"."TEST2"("P1","P2","P3") values ('125','test6','test4 ');
14-12-2017 10:17:08 INSERT  MANAGER DeminDV KRW\IVC-PTK-DEMIN   login_username=MANAGER client_info= OS_username=DeminDV Machine_name=KRW\IVC-PTK-DEMIN  insert into "MANAGER"."TEST2"("P1","P2","P3") values ('125','test6','test4 ');
'

示例 2. 使用闪回功能

   update manager.test2 set p2='test10' where p1=125;
    commit;

    SELECT versions_xid XID, versions_startscn START_SCN,
      versions_endscn END_SCN, versions_operation OPERATION,
      p1, p2, p3 FROM manager.test2
    VERSIONS BETWEEN SCN MINVALUE AND MAXVALUE;

XID                                         START_SCN           END_SCN       OPERATION       P1     P2     P3                                                                                                   
---------------------- ------------------------ ---------------------- --------------- ---------- -------- ------
08000E000DD70900        1374388524564                                                 U          125 test10   test4                                                                                                
08000E000DD70900        1374388524564                                                 U          125 test10   test4                                                                                                
08000E000DD70900        1374388524564                                                 U          125 test10                                                                                                            
08000E000DD70900        1374388524564                                                 U          125 test10                                                                                                                       
                                                               1374388524564                     125 test4 test4    
                                                               1374388524564                     125 test4 test4    
                                                               1374388524564                     125 test6 test4 
                                                               1374388524564                     125 test6 test4      



            SELECT  xid, start_scn , commit_scn COMMIT,operation OP, logon_user ,undo_sql FROM flashback_transaction_query
                   WHERE xid = HEXTORAW('08000E000DD70900');

    XID                      START_SCN  COMMIT                OP    LOGON_USER                           UNDO_SQL                                                                                             
-------------------------------- -------------------------------- -------------------------------------- --------------------------------- ------------------------------------                                                        
        08000E000DD70900    1374388524537   1374388524564   UPDATE  MANAGER update "MANAGER"."TEST2" set "P2" = 'test6' where ROWID = 'AAA2wgAAbAAAAEsAAM';
        08000E000DD70900    1374388524537   1374388524564   UPDATE  MANAGER update "MANAGER"."TEST2" set "P2" = 'test6' where ROWID = 'AAA2wgAAbAAAAEsAAL';
        08000E000DD70900    1374388524537   1374388524564   UPDATE  MANAGER update "MANAGER"."TEST2" set "P2" = 'test4 test4 ' where ROWID = 'AAA2wgAAbAAAAEsAAK';
        08000E000DD70900    1374388524537   1374388524564   UPDATE  MANAGER update "MANAGER"."TEST2" set "P2" = 'test4 test4 ' where ROWID = 'AAA2wgAAbAAAAEsAAJ';
        08000E000DD70900    1374388524537   1374388524564   UPDATE  MANAGER update "MANAGER"."TEST2" set "P2" = 'test4 test4 ' where ROWID = 'AAA2wgAAbAAAAEsAAI';
        08000E000DD70900    1374388524537   1374388524564   UPDATE  MANAGER update "MANAGER"."TEST2" set "P2" = 'test4 test4 ' where ROWID = 'AAA2wgAAbAAAAEsAAH';
        08000E000DD70900    1374388524537   1374388524564   UPDATE  MANAGER update "MANAGER"."TEST2" set "P2" = 'test4 test4 ' where ROWID = 'AAA2wgAAbAAAAEsAAG';
        08000E000DD70900    1374388524537   1374388524564   UPDATE  MANAGER update "MANAGER"."TEST2" set "P2" = 'test4 test4 ' where ROWID = 'AAA2wgAAbAAAAEsAAF';
        08000E000DD70900    1374388524537   1374388524564   BEGIN   MANAGER 

【讨论】:

【参考方案2】:

您可以使用 oracle 的 审计 功能。

-- to start auditing on table
AUDIT INSERT, UPDATE, DELETE ON <Schemaname>.<tablename> BY ACCESS WHENEVER SUCCESSFUL;

-- after auditing is started, execute below query to get the audit data
-- Modify the where condition according to your need
SELECT
    OS_USER,
    USERHOST,
    DB_USER,
    TO_CHAR(EXTENDED_TIMESTAMP, 'DD-MON-RRRR HHAM:MI:SS'),
    OBJECT_SCHEMA,
    OBJECT_NAME,
    STATEMENT_TYPE,
    SQL_TEXT,
    SQL_BIND
FROM
    DBA_COMMON_AUDIT_TRAIL
WHERE OBJECT_SCHEMA = <Schemaname>
  AND OBJECT_NAME = <tablename>
ORDER BY
    4 DESC;

可以使用以下参数在数据库级别设置审核选项:

AUDIT_TRAIL=NONE 或 FALSE|操作系统| DB 或 TRUE| DB_EXTENDED| XML |XML_EXTENDED

以下列表提供了每个值的说明:

NONE 或 FALSE -> 审核被禁用。 Oracle 10g 之前的默认设置。 DB 或 TRUE -> 启用审计,所有审计记录都存储在数据库审计跟踪 (AUD$) 中。 Oracle 11g 的默认设置。 DB_EXTENDED –> 与 DB 相同,但也填充了 SQL_BIND 和 SQL_TEXT 列。 XML-> 审计已启用,所有审计记录都存储为 XML 格式的操作系统文件。 XML_EXTENDED –> 与 XML 相同,但也填充了 SQL_BIND 和 SQL_TEXT 列。 OS -> 审计已启用,所有审计记录都指向由 AUDIT_FILE_DEST 指定的操作系统文件。

希望,这对你有用。

干杯!!

【讨论】:

以上是关于SQL - 查找插入特定值的语句的主要内容,如果未能解决你的问题,请参考以下文章

如何从 SQL *PLUS 中的批量插入中查找失败的插入语句

PL SQL - 使用动态 SQL 生成删除语句

查找第二大值的最简单的 SQL 查询是啥?

编写 SQL 语句通过 join id 查找行,并将连接行中的值插入到父表中

sql查找ID并在没有找到结果时插入

批量执行时如何查找失败的sql语句