It is not a solution for the update trigger, but maybe good for the CreatedEm:
(may vary slightly from SQL dialect to another, but almost all allow such thing)
When creating the table use:
CREATE TABLE T (
..definicao dos campos..
CriadoEm datetime DEFAULT CURRENT_TIMESTAMP
ModificadoEm datetime DEFAULT CURRENT_TIMESTAMP
So, for every new record, the CreatedEm and ModifiedEm field will get the record creation date if you do not enter any value.
As for future updates, there are still two possibilities:
-
Use same trigger
-
Or, if your DB allows, insert null into the modifiedEm field (and set it to NOT NULL) so that it takes the default value (only testing on your specific DB to see if it works) . In MySQL, for example, you should use type TIMESTAMP
to make this happen.
MySQL-specific Solution to keep ModifiedEm up to date without trigger:
CREATE TABLE t1 (
..definição dos outros campos..
ModificadoEm TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
It remains to see if the SQL you are using has something like that.