Data

Restrict, Project and Rename and Extend

We can effectively re-shape relations by removing tuples. Also by adding or removing attributes.

Restrict

We can restrict a relation to only include certain tuples using [ ].

S[STATUS=30]
SNOSNAMESTATUSCITY
S3Blake30Paris
S5Adams30Athens

We can build comparisons using = != < <= > >= + - * / ( ) and combine them using and, or, not.

S[STATUS<30 and STATUS>=10]
SNOSNAMESTATUSCITY
S1Smith20London
S2Jones10Paris
S4Clark20London

Project

We can project (keep) certain attributes of a relation, using { }.

S{SNO, SNAME, STATUS}
SNOSNAMESTATUS
S1Smith20
S2Jones10
S3Blake30
S4Clark20
S5Adams30

We can also project away certain attributes of a relation using ~{ }, i.e. project all but the ones given.

S~{SNO, STATUS}
SNAMECITY
AdamsAthens
BlakeParis
ClarkLondon
JonesParis
SmithLondon

If we remove the unique attributes, no duplicate entries will appear: a relation is always a set of tuples and a set has no duplicates. In the relational model, a tuple is a statement about the world and saying it twice adds nothing new.

S
SNOSNAMESTATUSCITY
S1Smith20London
S2Jones10Paris
S3Blake30Paris
S4Clark20London
S5Adams30Athens
S{CITY}
CITY
Athens
London
Paris

Notice how there are only three tuples in this projection expression.

Rename

We can rename attributes using := inside { }.

S{SN:=SNO}
SN
S1
S2
S3
S4
S5

Adding * means keep all existing attributes.

S{*, SCITY:=CITY}
SNOSNAMESTATUSSCITY
S1Smith20London
S2Jones10Paris
S3Blake30Paris
S4Clark20London
S5Adams30Athens

We can use two renames to swap attributes too.

S{SNAME:=CITY, CITY:=SNAME}
SNAMECITY
AthensAdams
LondonClark
LondonSmith
ParisBlake
ParisJones

Extend

We can also add attributes (extend) using :=.

S{*, NEW_STATUS:=STATUS/10}
NEW_STATUSSNOSNAMESTATUSCITY
1S2Jones10Paris
2S1Smith20London
2S4Clark20London
3S3Blake30Paris
3S5Adams30Athens

All together

And we can restrict and project and rename and extend in a single expression.

S[STATUS=20]{SNO, STATUS, SCITY:=CITY, NEW_STATUS:=STATUS/10}
NEW_STATUSSNOSTATUSSCITY
2S120London
2S420London

Each of these operations (restrict, project/rename/extend) returns a relation.