Advanced

Compose

It's often useful to remove the common attributes used by a join.

Compose

We can join two relations and then remove their common attributes by composing them with parentheses ( ).

S & SP
SNOSNAMESTATUSCITYPNOQTY
S1Smith20LondonP1300
S1Smith20LondonP2200
S1Smith20LondonP3400
S1Smith20LondonP4200
S1Smith20LondonP5100
S1Smith20LondonP6100
S2Jones10ParisP1300
S2Jones10ParisP2400
S3Blake30ParisP2200
S4Clark20LondonP2200
S4Clark20LondonP4300
S4Clark20LondonP5400
S(SP)
SNAMESTATUSCITYPNOQTY
Blake30ParisP2200
Clark20LondonP2200
Clark20LondonP4300
Clark20LondonP5400
Jones10ParisP1300
Jones10ParisP2400
Smith20LondonP1300
Smith20LondonP2200
Smith20LondonP3400
Smith20LondonP4200
Smith20LondonP5100
Smith20LondonP6100

Notice how the SNO attribute, the one used to join the relations, has been removed in the compose version.

Although compose can make some results tidier, it can be simpler to debug without it. Compose can also restrict the join ordering options, and not removing attributes too early can make complex expressions simpler to construct.

We can also use the compose syntax as a shorthand to compose with a relation created from scalar(s), e.g.

S(CITY:="Paris", STATUS:=30)
SNOSNAME
S3Blake

Which is short for:

(S & to_rel(tuple{CITY:="Paris", STATUS:=30}))~{STATUS, CITY}
SNOSNAME
S3Blake

And composition with dee, the relation with 1 tuple and 0 attributes, can be used to create a relation from scalar(s). The attribute types are inferred from the values, e.g.

dee(name:="This is a relation", value:=6.28)
namevalue
This is a relation6.28