I'm writing an application that uses SQLite to store court information obtained from a government database in a client table. I've set up a DB Fiddle here, which is a simplification of my application.
The first time a request is made to the database, the
clientstable will be populated with the information.When the user wants to check if there are any updates, a new request will be made and the results will be inserted into a
client_updatetableUpon insertion on the
client_updatetable, I will run a trigger that will update theclienttable if any of the columns ofclient_updatehave new information. If theclienttable is updated, the original row from theclienttable should be logged in alogtable
This question got me started, and I've replicated it in the context of my application. But I can't seem to find a good resource explaining how this actually works. I'll run through where I'm not understanding, taking the first insertion into client_update as an example:
A new row,
(213310109, 'Janice', True)is inserted on theclient_updatetable triggeringtr_client_update. The actions of this trigger happen after the insertion of the row, and before(213310110, 'Jane', True)is inserted.The
INSERTstatement intr_client_updatecompares the row(213310109, 'Janice', True)just inserted intoclient_updateagainst(213310109, 'John', True)inclientbecause they have the samea_number. Since thenamehas changed it logs the row fromclientinclient_log.Using the same logic, the
updatestatement updates theclienttable with the new information.
I am hoping a kind DB Admin can help me understand the logical steps that are happening here. I don't feel like my explanation is adequate. Why is the right statement for achieving this WHERE a_number = NEW.a_number AND (name IS NOT NEW.name OR docketed IS NOT NEW.docketed); and not something like WHERE a_number IS NOT NEW.a_number AND (name IS NEW.name OR docketed IS NEW.docketed);. This would seem to make sense because what I want to check is that that the a_number is the same in both tables, but either name or docketed was changed.