Skip to content
Advertisement

Check for overlapped dates on any row of a table Oracle SQL

I have the following schema:

And the following trigger, that is supposed to raise an error whenever an EPOCA is inserted into the database and the period between DATA_FIM and DATA_INI is overlapped with other periods of other EPOCAS.

Think of it as: if I define the summer between june 1st and 30th of august I cannot define anyother period of the year with that period of time, nor can I update an existing period with those dates or nothing between june 1st and 30th august.

Right now I can insert any EPOCA with the same date as any other present in the table and I can update the date of any EPOCA with the dates of other EPOCAS and it allows me. What can I change?

Advertisement

Answer

I guess you would need a trigger like this one:

Note, the FOR EACH ROW clause is not given!

Otherwise the trigger performs only the currently inserted/updated row but does not compare to any existing data.

Consider also cases like this:

In the table you have a period from 1st to 30th of August, then you try to add period for 1st of May to 31th of December. Of course, such situations should be also blocked by the trigger. Thus you need only a statement-level trigger, i.e. a row level trigger which checks only the inserted/updated row is not sufficient.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement