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
SNO | SNAME | STATUS | CITY | PNO | QTY |
---|---|---|---|---|---|
S1 | Smith | 20 | London | P1 | 300 |
S1 | Smith | 20 | London | P2 | 200 |
S1 | Smith | 20 | London | P3 | 400 |
S1 | Smith | 20 | London | P4 | 200 |
S1 | Smith | 20 | London | P5 | 100 |
S1 | Smith | 20 | London | P6 | 100 |
S2 | Jones | 10 | Paris | P1 | 300 |
S2 | Jones | 10 | Paris | P2 | 400 |
S3 | Blake | 30 | Paris | P2 | 200 |
S4 | Clark | 20 | London | P2 | 200 |
S4 | Clark | 20 | London | P4 | 300 |
S4 | Clark | 20 | London | P5 | 400 |
S(SP)
SNAME | STATUS | CITY | PNO | QTY |
---|---|---|---|---|
Blake | 30 | Paris | P2 | 200 |
Clark | 20 | London | P2 | 200 |
Clark | 20 | London | P4 | 300 |
Clark | 20 | London | P5 | 400 |
Jones | 10 | Paris | P1 | 300 |
Jones | 10 | Paris | P2 | 400 |
Smith | 20 | London | P1 | 300 |
Smith | 20 | London | P2 | 200 |
Smith | 20 | London | P3 | 400 |
Smith | 20 | London | P4 | 200 |
Smith | 20 | London | P5 | 100 |
Smith | 20 | London | P6 | 100 |
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)
SNO | SNAME |
---|---|
S3 | Blake |
Which is short for:
(S & to_rel(tuple{CITY:="Paris", STATUS:=30}))~{STATUS, CITY}
SNO | SNAME |
---|---|
S3 | Blake |
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)
name | value |
---|---|
This is a relation | 6.28 |