A
A
Andrey Strelkov2018-10-30 13:32:47
MySQL
Andrey Strelkov, 2018-10-30 13:32:47

How to get the difference of the same database table once per hour?

Good afternoon, tell me please.
There is a MariaDB database, it has a database in which there is an employees
table This table contains a list of all employees, its structure is about 10 fields, but for example, you can take only two, for example, this is PERSONNEL_NUMBER (personnel number) and FULL_NAME (full name)
Table approximately 15,000 lines long and is used to synchronize various applications with the employees of the organization, i.e. applications take information from it, but this table has one minus, it does not have (and there is no way to do it, let's take it for granted) the CHANGED_DATE field, i.e. the date when the employee was changed (i.e. when any of his fields was changed) and therefore, third-party applications have to run through all records during synchronization.
The question is, how can you make it so that, in addition to this table, periodically, for example, once an hour, the same one is created, or maybe some views that would contain exactly the same information, only plus one more CHANGED_DATE column , and its value would be filled based on state comparison.
Those. as I roughly see, there is an original table and a copy of it clone_employees, then every hour they are compared in some way, and all rows that do not match are inserted into the third table diff_employees, which also has a CHANGED_DATE field where the current time is written
After that, the contents of the original table are copied to clone_employees (to remember the current state, so to speak, for the next comparison)
Perhaps the method is very strange, but alas, there is only one original table that you can work with, and initially it is not possible to store the date in it for some reason last change

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Edouard Faber, 2018-10-30
@eduha

Why not create a field with the modification date in this table (of course, NULL) and fill its value with a trigger?
Something like

DELIMITER //
 CREATE TRIGGER bu_employees BEFORE UPDATE ON employees
     FOR EACH ROW
     BEGIN
     SET NEW.CHANGED_DATE = NOW();
     END
     //

A
Artyom Karetnikov, 2018-10-30
@art_karetnikov

You don't need to make a full copy of the table. By what logic it is impossible to add a field - I will explain. The dudes who worked in front of you used select * from employees... and put it in everywhere. Therefore now if you add one more field - everything where there is an insertion of all fields will fall.
So, you need a satellite table. Four fields. Id of this table, Id_employeer, Change_Date, Change_type.
Next, you catch the change or addition of the main table and insert data into the satellite table, which employee is changed and what happened to him in Change_type: Insert, Update, Delete. You can also use the Id of the person who made the change.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question