Use ON UPDATE CURRENT_TIMESTAMP when changing specific field

2

Example scenario

Table: usuarios

Fields: id , nome , sobrenome , senha , timestamp_alteracao

Question

The timestamp_alteração field should only be updated when the senha field changes.

Doubt

  • Is there a way to use the default value of the field, so that it does this as per the condition?
    • If not, would it have to do for update or even trigger ?
    • What other options?
asked by anonymous 28.11.2018 / 12:29

1 answer

2
  

Is there a way to use the default value of the field, so that it can perform this update as a condition?

Not all databases I know of, although technically it would be possible, but would probably be extension to the standard SQL. MySQL does not allow it.

  

If not, would it have to do update or even trigger ?

By update you can do what you want, but it does not seem to be what you want, because conditional could not be used alone, and it's not guaranteed.

triggers were created precisely for this scenario, every time a specific change is made one action must be done, ie one action triggers another under SGDB's own control.

Obviously this scenario requires a conditional, and the trigger must be powerful enough to support this kind of thing, so it's likely that something more than pure SQL is required, you probably need a programming language, based on SQL. MySQL has IF .

  

What other options?

Doing in the application, which has its advantages there, but also disadvantages, especially if you can not do it correctly, it is not guaranteed, the application must be correct (which for me is not a problem if the person knows what he is doing, it depends on the person having a bias of Dev or DBA).

    
28.11.2018 / 12:37