Data

Compare relations and extract tuples

We can compare relations. Since the restrict operation returns a relation, we can compare two filtered results.

S[STATUS=20]
SNOSNAMESTATUSCITY
S1Smith20London
S4Clark20London
S[CITY="London"]
SNOSNAMESTATUSCITY
S1Smith20London
S4Clark20London

The equality comparison, using = will return true for this case.

S[STATUS=20] = S[CITY="London"]
true

We can also test whether one relation is a subset of another, using <=. This symbol is meant to resemble the subset symbol, (which you can use instead, if your keyboard can create it).

S[STATUS=20] <= S[CITY="London"]
true
S[STATUS=20] <= S
true
S[STATUS=20] <= S[STATUS=30]
false

There is also a superset operator: >= (or ). And there are pure subset and pure superset operators (i.e. excluding the cases when both are equal): < and > (or and )

We can add a boolean attribute to a relation (or use an existing one). Then we can use the all or exists (or or if your keyboard can create them) aggregate operators to check if all or any tuples have the attribute set true.

exists(S{*, test:=(STATUS=20)}, test)
true
all(S{test:=(STATUS=20)})
false
all(S{test:=(STATUS<100)})
true

exists and all should be more efficient than counting the matches since they can short-circuit and finish early without necessarily checking all the tuples.

Since relations are comprised of tuples, we can extract a tuple from a relation, providing it has only 1 tuple in it. The print function displays the tuple horizontally.

print(S[STATUS=10].tuple)
┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄
  SNO   SNAME   STATUS   CITY   
  S2    Jones       10   Paris  
┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄

And we can go the other way: given a tuple, we have a shorthand to create a relation from it: to_rel().

to_rel(tuple{SNO:="S2", SNAME:="Jones", STATUS:=10, CITY:="Paris"})
SNOSNAMESTATUSCITY
S2Jones10Paris

We can compare tuples with other tuples, such as tuple literals, again using =.

S[STATUS=10].tuple = tuple{SNO:="S2", SNAME:="Jones", STATUS:=10, CITY:="Paris"}
true

And we can check whether a tuple is a member of a relation using <=. Or using (if your keyboard can create it).

S[STATUS=10].tuple <= S
true
S[STATUS=10].tuple ∈ S
true

There is also a >= operator to check whether a relation contains a tuple (or ).

We can extract an attribute from a tuple using .tuple.name, e.g.

S[STATUS=10]{CITY}.tuple.CITY
Paris

Given a tuple with a single attribute, we can extract that attribute using .tuple.* or the shorthand ..

S[STATUS=10]{CITY}.tuple.*
Paris
S[STATUS=10]{CITY}..
Paris