简体   繁体   中英

Find out when AllData table column's was updated the last time

I'm new to Oracle SQL (with me dealing with MS SQL, MySQL the most) and I am trying to figure out how to go about checking the database every second or 2 and see if anything in the tables column's have changed.

I currently have this type of schema ( AllData ):

ID | Script_name | Tester | DateLastMod | Tools 
-------------------------------------------------
1  | Script_1    | Bob    | 10-FEB-19   | Web
2  | Script_2    | Bob    | 10-FEB-19   | Web
3  | Script_3    | David  | 10-FEB-19   | Desktop
4  | Script_4    | Bill   | 10-FEB-19   | Web

I found something along the lines of this query:

SELECT * FROM all_tab_modifications WHERE TABLE_OWNER = 'ME';

Table_owner | Table_name | Inserts | Updates | Deletes | TimeStamp
------------------------------------------------------------------
Me          | Alldata    | null    | 6       | 0       | 11-Feb-19

But I am not sure if that is the correct query to use to see if Alldata table column have been changed since the last check?

The rough flow would be:

User changes ID 3 -> Tester to Sean (with date being Feb 11th).

 ID | Script_name | Tester | DateLastMod | Tools ------------------------------------------------- 1 | Script_1 | Bob | 10-FEB-19 | Web 2 | Script_2 | Bob | 10-FEB-19 | Web 3 | Script_3 | Sean | 11-FEB-19 | Desktop 4 | Script_4 | Bill | 10-FEB-19 | Web 

(checks for update)

Found updated table data so refresh data in jTable.

(checks for update)

 ID | Script_name | Tester | DateLastMod | Tools ------------------------------------------------- 1 | Script_1 | Bob | 10-FEB-19 | Web 2 | Script_2 | Bob | 10-FEB-19 | Web 3 | Script_3 | Sean | 11-FEB-19 | Desktop 4 | Script_4 | Bill | 10-FEB-19 | Web 

No change.

(checks for update)

 ID | Script_name | Tester | DateLastMod | Tools ------------------------------------------------- 1 | Script_1 | Bob | 10-FEB-19 | Web 2 | Script_2 | Bob | 10-FEB-19 | Web 3 | Script_3 | Sean | 10-FEB-19 | Desktop 4 | Script_4 | Bill | 10-FEB-19 | Web 

no change.

User changes ID 1 -> Script_name to Script_11 (with date being Feb 11th).

 ID | Script_name | Tester | DateLastMod | Tools ------------------------------------------------- 1 | Script_11 | Bob | 11-FEB-19 | Web 2 | Script_2 | Bob | 10-FEB-19 | Web 3 | Script_3 | Sean | 11-FEB-19 | Desktop 4 | Script_4 | Bill | 10-FEB-19 | Web 

(checks for update)

Found updated table data so refresh data in jTable.

(checks for update)

 ID | Script_name | Tester | DateLastMod | Tools ------------------------------------------------- 1 | Script_11 | Bob | 11-FEB-19 | Web 2 | Script_2 | Bob | 10-FEB-19 | Web 3 | Script_3 | Sean | 11-FEB-19 | Desktop 4 | Script_4 | Bill | 10-FEB-19 | Web 

no change.

etc etc....

So pretty much all I would need to check is the DataLastMod in order to see if the previous date changed. I can also see that I would need a Date + Time in order to get it correct for multiple changes in the same day.

So is the query I found what needs to be used or is there something else that would fit my issue?

No, you cannot use all_tab_modifications for this purpose. From the documentation :

These views are populated only for tables with the MONITORING attribute. They are intended for statistics collection over a long period of time. For performance reasons, the Oracle Database does not populate these views immediately when the actual modifications occur.

So you'll have to roll some solution of your own. Off the top of my head:

  1. As a_horse_with_no_name suggested, use change notification to watch for changes to this table and update some other table or field with the timestamp of the last update. Personally, I find CQN kind of confusing, so I avoid it. You could also do this in a trigger.
  2. Use MAX(DateLastMod) from your table. Depending on the size of the table and how many times you'll need to check this table for modifications, it may be helpful to index this column.

Solution #1 will serialize changes to your table, so that could have a performance impact.

If DateLastMod is actually stored as a VARCHAR2 and not just being displayed in DD-Mon-YY format, you should absolutely change it to a DATE , if possible. This will add a time component to the data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM