Resolving date conflicts

Suppose a table in a contact management system has a column holding the most recent contact with each customer.

One representative talks with a customer on a Friday, but does not upload his changes to the consolidated database until the next Monday. Meanwhile, a second representative meets the customer on the Saturday, and updates the changes that evening.

There is no conflict when the Saturday UPDATE is replicated to the consolidated database, but when the Monday UPDATE arrives it finds the row already changed.

By default, the Monday UPDATE would proceed, leaving the column with the incorrect information that the most recent contact occurred on Friday.

Update conflicts on this column should be resolved by inserting the most recent date in the row.

Implementing the solution

The following RESOLVE UPDATE trigger chooses the most recent of the two new values and enters it in the database.

CREATE TRIGGER contact_date RESOLVE UPDATE
ON Contacts
REFERENCING OLD AS old_name
NEW AS new_name
FOR EACH ROW
BEGIN
   IF new_name.contact_date <
         old_name.contact_date THEN
      SET new_name.contact_date
            = old_name.contact_date
   END IF
END;

If the value being updated is later than the value that would replace it, the new value is reset to leave the entry unchanged.