Here we'll build the following relvars which we'll use throughout the documentation:
S
for Supplier detailsP
for Part detailsSP
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]
}
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}"],
}
Later, we'll also add views (derived relvars).
Here are some more things to note about variables in Ra:
- variable names beginning with an uppercase letter are global (so in a sub-routine they cannot be masked by a local variable)
- global relational variables (relvars) are persistent: their values survive Ra restarts
- global relvars stored in the same directory and are known as a schema
- global relvars in a schema are subject to relational constraints stored in the schema
Constraint
relvar
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}) |
Storage details
Ra persists global relvars to disk files, currently as <table>
s in HTML files in the schema directory (the one Ra was started in).
Although storing data in HTML files has some space overheads, there are some benefits to using HTML:
- the format solves many storage issues (e.g. cross-platform encoding)
- the format handles many structural requirements (e.g. nested tables, tables with no columns and no rows)
- a browser can be used to view the raw data which handles many internationalisation and accessibility issues (e.g. unicode, right-to-left scripts)
- diff tools can be used to track changes
- version control tools can be used to record change history and revert to old snapshots
- version control tools can be used to distribute and backup the data
- a browser can show tables on any device, handling the scrolling etc., and can show nested relations
- a browser can handle a variety of fonts and styles, which can be customised with css
- it's future-proof: it will likely provide support for new data types
- it's future-proof: your data will be accessible 100 years from now