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
name | rule |
---|---|
Constraint_key | key(Constraint, {name}) |
P_SP_FK | SP{PNO} ⊆ P{PNO} |
P_key | key(P, {PNO}) |
SP_key | key(SP, {SNO,PNO}) |
S_SP_FK | SP{SNO} ⊆ S{SNO} |
S_key | key(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
SNO | SNAME | STATUS | CITY |
---|---|---|---|
S1 | Smith | 20 | London |
S2 | Jones | 10 | Paris |
S3 | Blake | 30 | Paris |
S5 | Adams | 30 | Athens |
SP
SNO | PNO | QTY |
---|---|---|
S1 | P1 | 300 |
S1 | P2 | 200 |
S1 | P3 | 400 |
S1 | P4 | 200 |
S1 | P5 | 100 |
S1 | P6 | 100 |
S2 | P1 | 300 |
S2 | P2 | 400 |
S3 | P2 | 200 |
And here's the view we defined earlier. Supplier `S4` is no longer in here.
S20
SNO | SNAME | STATUS | CITY | PNO | QTY |
---|---|---|---|---|---|
S1 | Smith | 20 | London | P1 | 300 |
S1 | Smith | 20 | London | P2 | 200 |
S1 | Smith | 20 | London | P3 | 400 |
S1 | Smith | 20 | London | P4 | 200 |
S1 | Smith | 20 | London | P5 | 100 |
S1 | Smith | 20 | London | P6 | 100 |