Databases Reference
In-Depth Information
Now, one interesting thing about this example is that the corresponding delete rule is very
similar to the insert rule as just defined. To be more specific, if a tuple is deleted from SP, the
sole SPQ tuple for the pertinent supplier needs to be deleted too; then the remaining SP tuples (if
any) for that supplier can be used to compute a new tuple to be inserted into SPQ. So here's the
rule:
ON DELETE d FROM SP :
WITH ( t1 := SPQ MATCHING d ,
t2 := SP MATCHING d { SNO } ,
t3 := t2 GROUP ( { PNO , QTY } AS PQ ) ) :
DELETE t1 FROM SPQ ,
INSERT t3 INTO SPQ ;
What about updates on SPQ? Well, deletes are easy:
ON DELETE d FROM SPQ :
DELETE ( SP MATCHING d ) FROM SP ;
In fact I think inserts are easy too. By way of example, assume again that the current
values of relvars SP and SPQ are the relations shown in Fig. 12.1. Now consider what happens
if we try to insert the following tuple into relvar SPQ:
┌─────┬───────────────┐
│ SNO │ PQ │
├─────┼───────────────┤
│ │ ┌─────┬─────┐ │
│ S4 │ │ PNO │ QTY │ │
│ │ ├═════┼─────┤ │
│ │ │ P2 │ 200 │ │
│ │ │ P4 │ 300 │ │
│ │ │ P5 │ 400 │ │
│ │ └─────┴─────┘ │
└─────┴───────────────┘
Well, I think it's clear that what we need to do here is insert the following three tuples into
relvar SP:
┌─────┬─────┬─────┐ ┌─────┬─────┬─────┐ ┌─────┬─────┬─────┐
│ SNO │ PNO │ QTY │ │ SNO │ PNO │ QTY │ │ SNO │ PNO │ QTY │
├─────┼─────┼─────┤ ├─────┼─────┼─────┤ ├─────┼─────┼─────┤
│ S4 │ P2 │ 200 │ │ S4 │ P4 │ 300 │ │ S4 │ P5 │ 400 │
└─────┴─────┴─────┘ └─────┴─────┴─────┘ └─────┴─────┴─────┘
So here's the insert rule:
ON INSERT i INTO SPQ :
INSERT ( i UNGROUP ( PQ ) ) INTO SP ;
 
Search WWH ::




Custom Search