Since relations are comprised of tuples, we can extract a tuple from a relation, providing it has exactly one tuple in it.
The print function displays the tuple horizontally on the console.
print($S[STATUS=10].tuple)
┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄ SNO SNAME STATUS CITY S2 Jones 10 Paris ┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄
And we can go the other way: given a tuple, we can create a relation from it using to_rel().
to_rel(tuple{SNO:="S2", SNAME:="Jones", STATUS:=10, CITY:="Paris"})
| SNO | SNAME | STATUS | CITY |
|---|---|---|---|
| S2 | Jones | 10 | Paris |
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 single-tuple relation using .tuple.name, e.g.
$S[STATUS=10]{CITY}.tuple.CITY
Paris
Given a single-tuple relation 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
Limitations
When using .tuple, or the shorthand .., if the relation does not have exactly one tuple then an error will be given.
Likewise if .tuple.* or .. is used, if the relation does not also have exactly one attribute then an error will be given, e.g.
print($S[STATUS=0].tuple)
::1:20: `tuple` only allowed on relations with exactly 1 tuple: relation expected to have a single tuple, got none
print($S[STATUS>10].tuple)
::1:21: `tuple` only allowed on relations with exactly 1 tuple: relation expected to have a single tuple, got more than one
$S[STATUS=10]..
::1:14: `tuple.*` (or `..`) only allowed on tuples with exactly 1 attribute: 4 (tuple{SNO:str,SNAME:str,STATUS:int,CITY:str})
Generally, tuple extraction should be avoided or left until the final presentation layer: working with relations affords more flexibility to combine data without having to check for the special case of exactly one tuple.