Exploration on getting the timestamp of data changes in oracle (to be updated)

In the near future, for a BI project, the data in each business system needs to be analyzed, then the problem comes. There are newly inserted data in the existing database, and there are also operations to change the existing data. Then these new additions and changes should be reflected in the BI system.

There may be no timestamp field in the table of the existing production database that can identify the change. If you add a timestamp field to an existing table, it is of course very easy to do at the database level, a simple "alter table add xxx column..." is enough. But there will be a huge amount of transformation in terms of business procedures.

  • When inserting, you need to add an additional timestamp field
  • When updating, you need to update the timestamp field at the same time

(developers should be crazy)

A timestamp that will be updated automatically can be added under the MySQL database. (This syntax is not supported in oracle)

alter table  t2   add COLUMN T_MODIFY_TM  timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT  'is_timestamp';

How to do in oracle?

Pseudo column ORA_ROWSCN

Thinking about it, since Oracle has pseudo-columns such as rownum and rowid, will there be pseudo-columns for scn of rows? . Sure enough oracle has ---- ORA_ROWSCN

SQL> select * from t2;

        ID
----------
         0



SQL> select ora_rowscn , id from t2;

ORA_ROWSCN         ID
---------- ----------
   3489228          0


SQL> insert into t2 values (2);

1 row created.

SQL> commit;

Commit complete.

SQL>  select ora_rowscn , id from t2;

ORA_ROWSCN         ID
---------- ----------
   3489228          0
   3489004          2

For easier reading, you can also convert the number of scn into time. I have a few Alibaba Cloud lucky coupons to share with you. There will be special surprises for purchasing or upgrading Alibaba Cloud products with the coupons! Take all the lucky coupons for the products you want to buy! Hurry up, it's about to be sold out.

SQL> select to_char(scn_to_timestamp(ora_rowscn),'YYYY-MM-DD HH24:MI:SS') , id from t2;

TO_CHAR(SCN_TO_TIME         ID
------------------- ----------
2018-02-26 10:40:28          0
2018-02-28 10:35:45          2

Convert Scn to time:

select to_char(scn_to_timestamp(3489228),'YYYY-MM-DD HH24:MI:SS') from dual;

Convert time to scn:

select timestamp_to_scn(to_date('2011-04-14 11:10:25','YYYY-MM-DD HH24:MI:SS')) from dual;

Simple test, the results are very happy, after all, there is no need to change the program. (My DBA who contributes to everyone)

Start validating in the business test library.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324445587&siteId=291194637