We can assign relation values to relation variables (relvars), using :=
to declare and assign.
Here we'll build the following relvars which we'll use throughout the documentation:
S
for Supplier details
P
for Part details
SP
for Supplier-Part stock details
S := {SNO:str SNAME:str STATUS:int CITY:str}{
["S1", "Smith", 20, "London"]
["S2", "Jones", 10, "Paris"]
["S3", "Blake", 30, "Paris"]
["S4", "Clark", 20, "London"]
["S5", "Adams", 30, "Athens"]
}
P := {PNO:str PNAME:str COLOR:str WEIGHT:int CITY:str}{
["P1", "Nut", "Red", 12, "London"]
["P2", "Bolt", "Green", 17, "Paris"]
["P3", "Screw", "Blue", 17, "Rome"]
["P4", "Screw", "Red", 14, "London"]
["P5", "Cam", "Blue", 12, "Paris"]
["P6", "Cog", "Red", 19, "London"]
}
SP := {SNO:str PNO:str QTY:int}{
["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]
["S4", "P2", 200]
["S4", "P4", 300]
["S4", "P5", 400]
}
Then we can add some integrity constraints to ensure our data remains valid. We'll go over these in a later section.
Constraint := {name:str rule: str}{
["Constraint_key", "key(Constraint, {name})"],
["S_key", "key(S, {SNO})"],
["P_key", "key(P, {PNO})"],
["SP_key", "key(SP, {SNO,PNO})"],
["S_SP_FK", "SP{SNO} ⊆ S{SNO}"],
["P_SP_FK", "SP{PNO} ⊆ P{PNO}"],
}
We can display the relvar values, in this case as HTML tables, but note that the left-to-right ordering is arbitrary as is the top-to-bottom order of the tuples. We'll introduce operators to specify both of these orderings later, purely for output purposes, because after that point we'll no longer be dealing with relations. Again, relations have no attribute or tuple order to them.
S
SNO | SNAME | STATUS | CITY |
S1 | Smith | 20 | London |
S2 | Jones | 10 | Paris |
S3 | Blake | 30 | Paris |
S4 | Clark | 20 | London |
S5 | Adams | 30 | Athens |
P
PNO | PNAME | COLOR | WEIGHT | CITY |
P1 | Nut | Red | 12 | London |
P2 | Bolt | Green | 17 | Paris |
P3 | Screw | Blue | 17 | Rome |
P4 | Screw | Red | 14 | London |
P5 | Cam | Blue | 12 | Paris |
P6 | Cog | Red | 19 | London |
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 |
S4 | P2 | 200 |
S4 | P4 | 300 |
S4 | P5 | 400 |
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}) |