0

Let's say we have a database table named Users and a column named CreatedAt. When a new user is inserted into Users, the value of column CreatedAt is set to the current timestamp.

Now let's say the value of CreatedAt should never be allowed to change. Afterall, it's the date the user joined, it's basically their anniversary date.

Does H2 Database support the ability to prevent a column from being modified? In this case, we want to prevent any modification of the CreatedAt column.

Seems MySQL supports this feature via Triggers, for example:

CREATE TRIGGER my_trig BEFORE UPDATE ON Users
FOR EACH ROW BEGIN
  SET NEW.CreatedAt = OLD.CreatedAt
END

Thanks for your help.

fire_water
  • 1,380
  • 1
  • 19
  • 33
  • 1
    You can implement triggers as well. In H2 they are declared in SQL and implemented in Java/JDBC. See [triggers](https://stackoverflow.com/questions/29993308/h2-how-to-create-a-database-trigger-that-log-a-row-change-to-another-table). – The Impaler Jan 25 '23 at 17:58

0 Answers0