Active databases, hierarchical structure, write a set of triggers

44 Views Asked by At

Direct connections between locations are represented in a

  • Table DirectConn (IdSource, Id Target, Distance)
  • Table BestConn (IdSource, Id Target, MinDistance)

describes the minimum distance associated with the direct or multi-hop paths connecting each pair of (reachable) locations.

Write a set of triggers that react to insertions into DirectConn and update the BestConn table if needed.

This is the text of a past exam from my university. I really have no idea how to go about creating a working set of triggers.

Does anyone have any suggestions?

This is a solution i have tried

CREATE TRIGGER
AFTER INSERT ON DirectConn
for each row
BEGIN
INSERT INTO BestConn new values(new.IdSource, new.IdTarget
(select Distance
from DirectConn
where not exists (SELECT *
                  FROM BestConn
                  WHERE MinDistance = new.Distance AND IdSource = new.IdSource AND IdTarget = new.IdTarget)

DELETE FROM BestConn
WHERE IdSource = new.IdSource AND IdTarget = new.IdTarget

INSERT INTO BestConn new values(
SELECT D2.IdSource, D.IdTarget, sum(Distance)
FROM DirectConn D JOIN DirectConn D2
WHERE new.IdSource = D.IdTarget AND new.IdTarget = D2.IdSource)
END;
0

There are 0 best solutions below