Advanced

Multiple assignment

We can perform multiple assignments in a single statement. This allows us to set inter-dependent values, since any constraints are only checked after all the assignments in a statement have been made. We can assign multiple values to multiple variables using , to separate the assignments.

If any constraints fail to be satisfied, all the assignments in the statement are undone. You can check the error relvar to see any constraint-failure messages. A common pattern might be to check, after every assignment that could fail, whether empty(error) is true (if error is not empty, then the last assignment statement failed and was undone).

For example, say we want to remove a supplier from S and also from SP, using two assignments. If we use a multiple-assignment then the integrity constraints will be checked only after both of the assignments are complete and so will be satisfied.

Here's a reminder of the constraints in effect:

Constraint
namerule
Constraint_keykey(Constraint, {name})
P_SP_FKSP{PNO} ⊆ P{PNO}
P_keykey(P, {PNO})
SP_keykey(SP, {SNO,PNO})
S_SP_FKSP{SNO} ⊆ S{SNO}
S_keykey(S, {SNO})

So now the removal of "S4":

S := S - S[SNO="S4"], SP := SP - SP[SNO="S4"]

if not empty(error) then begin print("Removing S4 failed\n«error»") end

There were no errors. If we'd tried to do the assigments one at a time, with the S assigmment first, we would have caused constraint S_SP_FK to fail. e.g.

S := S - S[SNO="S3"]

if not empty(error) then begin print("Removing S3 failed\n«error»") end
Removing S3 failed
┌─────────────────────────────┐
│ message                     │
├─────────────────────────────┤
│ constraint 'S_SP_FK' failed │
└─────────────────────────────┘
S
SNOSNAMESTATUSCITY
S1Smith20London
S2Jones10Paris
S3Blake30Paris
S5Adams30Athens
SP
SNOPNOQTY
S1P1300
S1P2200
S1P3400
S1P4200
S1P5100
S1P6100
S2P1300
S2P2400
S3P2200

And here's the view we defined earlier. Supplier `S4` is no longer in here.

S20
SNOSNAMESTATUSCITYPNOQTY
S1Smith20LondonP1300
S1Smith20LondonP2200
S1Smith20LondonP3400
S1Smith20LondonP4200
S1Smith20LondonP5100
S1Smith20LondonP6100