Kim
Kim

Reputation: 37

PLSQL: Trigger to maintain database integrity?

So I have read that triggers can be used for the Supplement declarative constraints to maintain database integrity.. I don't know how it can do that though. I have tried figuring this out a lot but failed. Can you please provide me an example for this? Thanks!

Upvotes: 0

Views: 116

Answers (2)

Ben
Ben

Reputation: 52923

Triggers provide no guarantees of data integrity because they only execute when triggered. Where the trigger to be created after the table exists or disabled for a time then data that was entered prior to the creation of the trigger or whilst it was disabled will not have been checked.

In general, even if you disable a constraint enabling it again will validate all data in the table. Of course there's a keyword that prevents this but at that point you're deliberately avoiding implementing your constraint.

Effectively, a trigger is executed code. A constraint enforces data integrity. A trigger can supplement the constraint, certainly, but it is no substitute for one and provides you no long-term guarantees.

Upvotes: 3

Littlefoot
Littlefoot

Reputation: 143143

Oracle suggests that you should use CONSTRAINTS to maintain database integrity whenever possible. Though, there are situations that constraints can't cover, so you need to use triggers. Those "situations" are usually complex rules that require some more code to be written.

Have you read Triggers in Oracle documentation? (Yes, you mentioned you read something, but didn't specify what.) It isn't that long, and mentions "integrity" quite a few times, so - have a look.

Upvotes: 2

Related Questions