Neither the SQL standard nor ANY OF the major database vendors provide an ON COMMIT trigger (i.e. a trigger that fires WHEN a TRANSACTION commits). The ONLY EXCEPTION (AFAIK) IS Firebird

I wonder why that IS. I think this kind OF trigger would be very useful, specially TO CHECK that the modifications performed during the TRANSACTION do NOT violate ANY OF the integrity CONSTRAINTS OF the SCHEMA. AND I’m not talking about complex business rules, even to enforce some association multiplicity constraints we should be able to check the database state at the end of the transaction, do it in-between is not good enough. Moreover, an on commit trigger could avoid the the too-common “mutating tables” error typically arising when using triggers that query the same tables we are modifying (as usually happens when we want to check that the modifications are consistent with the constraints).

Let me just give you an example, assume that we need to generate the database schema for this UML class diagram

where we are assuming that all students have to be enrolled in at least one course. How do you enforce this constraint at the database level? What kind of trigger do you use? An AFTER DELETE trigger on the EnrolledIn table is only a partial solution since it checks that existing students do not drop out of his/her courses but it does not check that new students enroll at least in one course (an AFTER INSERT trigger on Student cannot be used for this purpose since the trigger will execute immediately after inserting a new student and thus it cannot check whether before committing the transaction we have enrolled the student in some courses).

Of course, there are workarounds, e.g. enforcing the constraint at the application level instead of relying on the database. However, I prefer to put as many constraints as possible in the database (IMHO it’s safer, WHEN checking CONSTRAINTS IN the application logic we need TO remember TO include the evaluation OF ALL relevant CONSTRAINTS every TIME we CREATE a new MODULE)

The absence OF this kind OF trigger IS even MORE surprising WHEN considering that database vendors (e.g. Oracle) do provide the OPTION OF deferring the evaluation OF PRIMARY, FOREIGN, UNIQUE AND CHECK CONSTRAINTS until the END OF the TRANSACTION. An ON-COMMIT trigger would be exactly the same concept but WITH MORE expressiveness power.

Anyway, if you were wondering why my UMLtoDB generation service ignores multiplicity CONSTRAINTS now you know.

Want to build better software faster?

Want to build better software faster?

Read about the latest trends on software modeling and low-code development

You have Successfully Subscribed!

Pin It on Pinterest

Share This