TutorialD is an logic language designed by Chris Date for interacting with any relational algebra engine. Project:M36 supports this language for the purposes of learning about the relational algebra. TutorialD is not recommended for use in production systems.
It is presumed that the audience for this tutorial has at least some minimal background with an SQL-based DBMS.
From the project-m36 source directory, execute cabal run tutd
to start the interactive TutorialD interpreter. You will be greeted by the interactive prompt:
TutorialD (master/main):
"master" refers to the name of the branch to which the current transaction will be potentially committing.
TutorialD is strongly-typed. The basic built-in types are:
Type Name | Explanation | Example |
---|---|---|
char | arbitrary text | "The Old Man and the Sea" |
int | any integer | -4 |
datetime | timestamp UTC | "2015-02-02 04:05:02"::datetime |
double | floating point number | 3.1459 |
bool | boolean value | t |
bytestring | arbitrary-length string of bytes- input is base64-encoded | "dGVzdGRhdGE="::bytestring |
With regards to boolean values, be sure not to conflate t
or f
as a boolean value with true
and false
which are relation variables.
Project:M36 will complain loudly if the expected types do not match. Automatic type coercion does not exist.
TutorialD (master/main): :showexpr S:{more:=add(10,@sname)}
ERR: AtomFunctionTypeError "add" 2 IntAtomType StringAtomType
The integer "10" cannot be added to the string sname value.
Relation Variables are named cells which reference relations within a transaction state. The relation can be replaced with another relation through assignment.
Definition of a relation variable:
products :: {name char, age int}
This expression creates a new relation variable in the current context which can refer to any relation with the header of "name" of type "char" and "age" of type int.
Assignment to a relation variable:
products := relation{tuple{name "Mike",age 6},tuple{name "Sam",age 10}}
This expression assigns a relation containing two tuples to the previously-defined "products" relation.
tutd
includes two relations by default:
true
: the relation with no attributes and a body of one empty-attributed tuplefalse
: the relation with no attributes and a body of no tuples
Chris Date refers to these relations as "TABLE_DUM" and "TABLE_DEE" but such arbitrary names are not so recognizable. true
and false
can be easily remembered as the answer to the question: "Does this relation have any tuples?" which can be formulated in TutorialD as the projection of a relation asking for no attributes:
products{}
The examples in this tutorial can be executed after loading Chris Date's sample relations using:
:importexample date
Each section assumes a fresh instance of the Date examples. The relation variables created in the script are:
Relation Variable Name | Description |
---|---|
s | suppliers |
p | parts |
sp | contains mapping of which suppliers supply which parts |
A relational expression combines relational operators to create a new relation. tutd
can display the result of executing a relational expression with ":showexpr".
TutorialD (master/main): :showexpr p
ββββββββ¬ββββββ¬βββ¬ββββββ¬βββββββ
βcity βcolorβp#βpnameβweightβ
ββββββββΌββββββΌβββΌββββββΌβββββββ€
βLondonβRed βP6βCog β19 β
βParis βBlue βP5βCam β12 β
βLondonβRed βP1βNut β12 β
βLondonβRed βP4βScrewβ14 β
βOslo βBlue βP3βScrewβ17 β
βParis βGreenβP2βBolt β17 β
ββββββββ΄ββββββ΄βββ΄ββββββ΄βββββββ
To show all relation variables in the current session, use :showrelvars
:
TutorialD (master/main): :showrelvars
βββββββββββββββββββββββββββββββββββββββββββββββββββ¬βββββββββββ
βattributes::relation {attribute::Text,type::Text}βname::Textβ
βββββββββββββββββββββββββββββββββββββββββββββββββββΌβββββββββββ€
ββββββββββββββββββ¬βββββββββββ β"sp" β
ββattribute::Textβtype::Textβ β β
ββββββββββββββββββΌβββββββββββ€ β β
ββ"p#" β"Text" β β β
ββ"qty" β"Int" β β β
ββ"s#" β"Text" β β β
ββββββββββββββββββ΄βββββββββββ β β
ββββββββββββββββββ¬βββββββββββ β"true" β
ββattribute::Textβtype::Textβ β β
ββββββββββββββββββ΄βββββββββββ β β
ββββββββββββββββββ¬βββββββββββ β"p" β
ββattribute::Textβtype::Textβ β β
ββββββββββββββββββΌβββββββββββ€ β β
ββ"weight" β"Int" β β β
ββ"p#" β"Text" β β β
ββ"color" β"Text" β β β
ββ"city" β"Text" β β β
ββ"pname" β"Text" β β β
ββββββββββββββββββ΄βββββββββββ β β
ββββββββββββββββββ¬βββββββββββ β"s" β
ββattribute::Textβtype::Textβ β β
ββββββββββββββββββΌβββββββββββ€ β β
ββ"status" β"Int" β β β
ββ"s#" β"Text" β β β
ββ"sname" β"Text" β β β
ββ"city" β"Text" β β β
ββββββββββββββββββ΄βββββββββββ β β
ββββββββββββββββββ¬βββββββββββ β"false" β
ββattribute::Textβtype::Textβ β β
ββββββββββββββββββ΄βββββββββββ β β
βββββββββββββββββββββββββββββββββββββββββββββββββββ΄βββββββββββ
Relational operators generate new relations from existing relations. The operators form the closed algebra against relations.
The unary rename operator outputs a new relation with chosen attributes renamed. In SQL, the equivalent is to use "AS" in the column name list of SELECTS.
TutorialD (master/main): :showexpr s rename {city as town}
ββββ¬ββββββ¬βββββββ¬βββββββ
βs#βsnameβstatusβtown β
ββββΌββββββΌβββββββΌβββββββ€
βS3βBlakeβ30 βParis β
βS4βClarkβ20 βLondonβ
βS5βAdamsβ30 βAthensβ
βS1βSmithβ20 βLondonβ
βS2βJonesβ10 βParis β
ββββ΄ββββββ΄βββββββ΄βββββββ
Projection is applied by adding curly braces and attribute names following a relational expression. Projection in SQL is applied by adding a list of column names to a SELECT statement.
For example:
TutorialD (master/main): :showexpr p{color,city}
ββββββββ¬ββββββ
βcity βcolorβ
ββββββββΌββββββ€
βLondonβRed β
βParis βBlue β
βOslo βBlue β
βParis βGreenβ
ββββββββ΄ββββββ
Restriction is applied using a "where" clause, much like in SQL.
TutorialD (master/main): :showexpr p where color="Blue" and city="Paris"
βββββββ¬ββββββ¬βββ¬ββββββ¬βββββββ
βcity βcolorβp#βpnameβweightβ
βββββββΌββββββΌβββΌββββββΌβββββββ€
βParisβBlue βP5βCam β12 β
βββββββ΄ββββββ΄βββ΄ββββββ΄βββββββ
The restriction predicate can be built from "and", "not", and "or". Boolean atom functions can also appear in a restriction as long as they are preceded by "^". For example:
TutorialD (master/main): :showexpr s where ^lt(@status,20)
βββββββ¬βββ¬ββββββ¬βββββββ
βcity βs#βsnameβstatusβ
βββββββΌβββΌββββββΌβββββββ€
βParisβS2βJonesβ10 β
βββββββ΄βββ΄ββββββ΄βββββββ
Joins are binary operators and are applied with the "join" keyword. The equivalent in SQL would be a "NATURAL JOIN". To join attributes which are not identically-named, use the rename operator
TutorialD (master/main): :showexpr s join sp
ββββββββ¬βββ¬ββββ¬βββ¬ββββββ¬βββββββ
βcity βp#βQTYβs#βsnameβstatusβ
ββββββββΌβββΌββββΌβββΌββββββΌβββββββ€
βLondonβP6β100βS1βSmithβ20 β
βLondonβP3β400βS1βSmithβ20 β
βLondonβP5β400βS4βClarkβ20 β
βLondonβP1β300βS1βSmithβ20 β
βParis βP2β200βS3βBlakeβ30 β
βParis βP1β300βS2βJonesβ10 β
βLondonβP5β100βS1βSmithβ20 β
βLondonβP4β300βS4βClarkβ20 β
βParis βP2β400βS2βJonesβ10 β
βLondonβP2β200βS1βSmithβ20 β
βLondonβP4β200βS1βSmithβ20 β
ββββββββ΄βββ΄ββββ΄βββ΄ββββββ΄βββββββ
The binary union operator merges tuples from both relations if-and-only-if the attributes of both relations are identical. The SQL equivalent is the "UNION" operator. Unlike SQL, however, duplicate tuples are not tolerated, so there is no "UNION ALL" equivalent in the relational algebra.
TutorialD (master/main): :showexpr S union S
ββββββββ¬βββ¬ββββββ¬βββββββ
βcity βs#βsnameβstatusβ
ββββββββΌβββΌββββββΌβββββββ€
βParis βS3βBlakeβ30 β
βLondonβS4βClarkβ20 β
βAthensβS5βAdamsβ30 β
βLondonβS1βSmithβ20 β
βParis βS2βJonesβ10 β
ββββββββ΄βββ΄ββββββ΄βββββββ
The union of any relation with itself is itself.
The extend unary operator adds an attribute to a relation's header and body and is represented by a colon ":". The equivalent in SQL is a subselect in a SELECT column list. Typically, extend is used to add information derived from relation.
TutorialD (master/main): :showexpr s:{status2:=add(10,@status)}
ββββββββ¬βββ¬ββββββ¬βββββββ¬ββββββββ
βcity βs#βsnameβstatusβstatus2β
ββββββββΌβββΌββββββΌβββββββΌββββββββ€
βParis βS2βJonesβ10 β20 β
βAthensβS5βAdamsβ30 β40 β
βParis βS3βBlakeβ30 β40 β
βLondonβS1βSmithβ20 β30 β
βLondonβS4βClarkβ20 β30 β
ββββββββ΄βββ΄ββββββ΄βββββββ΄ββββββββ
The "@" is required in the above query in order to distinguish the attribute's name from a relation's name.
Supported atom functions include:
Function | Purpose |
---|---|
add(int,int) | Return the sum of two integers. |
not(bool) | Invert a boolean expression. |
lt(int,int) | Returns boolean true atom if he first argument is less than the second argument. |
lte(int,int) | Returns boolean true atom if he first argument is less than or equal to the second argument. |
gt(int,int) | Returns boolean true atom if he first argument is greater than the second argument. |
gte(int,int) | Returns boolean true atom if he first argument is greater than or equal to the second argument. |
The unary group operator groups the argument attributes into subrelations for each ungrouped set of attributes. SQL does not support tables as values, though SQL does support a notion of grouping by equal values. However, SQL's "GROUP BY" relies on tuple ordering in a table. A relations' tuples are never ordered.
Grouping is useful for aggregate operations (see below) or for summarizing data against a set of attributes; for example, "display all employees grouped by boss name".
TutorialD (master/main): :showexpr s group ({sname,status,s#} as subrel)
ββββββββ¬ββββββββββββββββββ
βcity βsubrel β
ββββββββΌββββββββββββββββββ€
βAthensβββββ¬ββββββ¬ββββββββ
β ββs#βsnameβstatusββ
β βββββΌββββββΌβββββββ€β
β ββS5βAdamsβ30 ββ
β βββββ΄ββββββ΄ββββββββ
βLondonβββββ¬ββββββ¬ββββββββ
β ββs#βsnameβstatusββ
β βββββΌββββββΌβββββββ€β
β ββS1βSmithβ20 ββ
β ββS4βClarkβ20 ββ
β βββββ΄ββββββ΄ββββββββ
βParis βββββ¬ββββββ¬ββββββββ
β ββs#βsnameβstatusββ
β βββββΌββββββΌβββββββ€β
β ββS3βBlakeβ30 ββ
β ββS2βJonesβ10 ββ
β βββββ΄ββββββ΄ββββββββ
ββββββββ΄ββββββββββββββββββ
The unary ungroup operator "unwraps" subrelations in a relation-valued attribute. There is no equivalent in SQL.
TutorialD (master/main): :showexpr (s group ({sname,status,s#} as subrel)) ungroup subrel
ββββββββ¬βββ¬ββββββ¬βββββββ
βcity βs#βsnameβstatusβ
ββββββββΌβββΌββββββΌβββββββ€
βParis βS3βBlakeβ30 β
βLondonβS4βClarkβ20 β
βAthensβS5βAdamsβ30 β
βLondonβS1βSmithβ20 β
βParis βS2βJonesβ10 β
ββββββββ΄βββ΄ββββββ΄βββββββ
While relational operators compose to relational expressions representing queries of the database, state operators change the state of the database.
The relation variable assignment mentioned above certainly changes the state of the database.
TutorialD (master/main): newrelvar:=relation{tuple{age 3}}
TutorialD (master/main): :showexpr newrelvar
βββββ
βageβ
βββββ€
β3 β
βββββ
All further operators are convenience operators and could be implemented with simple assignment. SQL has no equivalent to relational assignment; instead, one must issue "CREATE TABLE" commands and insert rows into the resultant tables.
To signal that a relation variable should be forgotten, undefine it.
TutorialD (master/main): undefine s
TutorialD (master/main): :showexpr s
ERR: RelVarNotDefinedError "s"
The insert operators accepts a relation variable and a relation of the same type (a relation having an identical header), and replaces the relation referenced by the relation variable with the union of the previous relation value and the argument's relation value.
TutorialD (master/main): insert s relation{tuple{city "Boston",s# "S10",sname "Gonzalez",status 10}}
TutorialD (master/main): :showexpr s
ββββββββ¬ββββ¬βββββββββ¬βββββββ
βcity βs# βsname βstatusβ
ββββββββΌββββΌβββββββββΌβββββββ€
βParis βS3 βBlake β30 β
βLondonβS4 βClark β20 β
βAthensβS5 βAdams β30 β
βLondonβS1 βSmith β20 β
βParis βS2 βJones β10 β
βBostonβS10βGonzalezβ10 β
ββββββββ΄ββββ΄βββββββββ΄βββββββ
This insertion operation is equivalent to:
s:=s union relation{tuple{city "Boston",s# "S10",sname "Gonzalez",status 10}}
The update operator accepts a relation argument and a predicate, optionally filters and creates new tuples based on the original relation.
TutorialD (master/main): update s where status=20 (sname:="Mr. Twenty")
TutorialD (master/main): :showexpr s
ββββββββ¬βββ¬βββββββββββ¬βββββββ
βcity βs#βsname βstatusβ
ββββββββΌβββΌβββββββββββΌβββββββ€
βParis βS3βBlake β30 β
βLondonβS1βMr. Twentyβ20 β
βAthensβS5βAdams β30 β
βParis βS2βJones β10 β
βLondonβS4βMr. Twentyβ20 β
ββββββββ΄βββ΄βββββββββββ΄βββββββ
This is logically equivalent to:
TutorialD (master/main): s:=(((s where status=20){city,s#,status}):{sname:="Mr. Twenty"}) union (s where not status=20)
TutorialD (master/main): :showexpr s
ββββββββ¬βββ¬βββββββββββ¬βββββββ
βcity βs#βsname βstatusβ
ββββββββΌβββΌβββββββββββΌβββββββ€
βParis βS3βBlake β30 β
βLondonβS1βMr. Twentyβ20 β
βAthensβS5βAdams β30 β
βParis βS2βJones β10 β
βLondonβS4βMr. Twentyβ20 β
ββββββββ΄βββ΄βββββββββββ΄βββββββ
This query means: "Drop the sname attribute from S filtered where status equals 20, extend the resultant relation by sname with value 'Mr. Twenty', then union the resultant relation with S filtered by status not equaling 20, and assign that to S."
The delete operator accepts a relation argument and a filtering predicate, creates a new relation with only the tuples which do not match the predicate, and stores the result in the relation variable argument.
TutorialD (master/main): delete sp where s#="S4"
TutorialD (master/main): :showexpr sp
ββββ¬ββββ¬βββ
βp#βQTYβs#β
ββββΌββββΌβββ€
βP3β400βS1β
βP4β200βS1β
βP2β400βS2β
βP2β200βS1β
βP6β100βS1β
βP1β300βS1β
βP2β200βS3β
βP1β300βS2β
βP5β100βS1β
ββββ΄ββββ΄βββ
This is logically equivalent to:
TutorialD (master/main): sp:=sp where not s#="S4"
Note the inverted predicate.
Database constraints are user-specified relational algebra predicates which must be true at all times. If a database expression attempts to violate a database constraint, then the DBMS must reject the expression and ensure that the previous, non-violating state remains.
Chris Date identified that all constraints can be represented as inclusion dependencies, but Project:M36 offers some convenience operators.
Uniqueness constraints ensure that a set of attributes' values are unique throughout a relational expression. For example, it is useful to ensure that all products' identifiers are unique. The equivalent in SQL is the UNIQUE expression found in table creation.
TutorialD (master/main): key s_key_constraint {s#} s
This expression creates a constraint named "S_key_constraint" and ensure that the values of the "s#" attribute are unique in the "S" relational expression.
Foreign keys are constraints which require that certain values appearing in one relation variable must appear in another. The equivalent in SQL is the FOREIGN KEY() construction in table creation.
TutorialD (master/main): foreign key s#_in_sp sp{s#} in s{s#}
This expression ensure that any sp{s#} must also appear as a value in s{s#}.
Unlike SQL databases, Project:M36 includes first-class support for functional dependencies. A functional dependency for an attribute set A
to attribute set B
ensures that for every unique appearance of a value for A
there is only one possible value for B
.
To create a functional dependency:
TutorialD (master/main): funcdep sname_status (sname) -> (status) s
In this example, the functional dependency is named "sname_status" and creates a constraint which ensures that the "sname" value is functionally determines "status" value in the relation variable "s", though the final argument can be any relational expression. Multiple attribute names are separated by a comma.
All other constraints can be represented by a set of inclusion dependencies.
TutorialD (master/main): constraint s_status_less_than_50 (s where ^lt(50,@status)){} in false
This expression ensures that the relation variable's relation can never include a tuple with status greater than 50.
Internally, all constraints are converted and stored as inclusion dependencies.
Aggregate queries in TutorialD are able to provide much more information than SQL queries due to the support for relation-valued attributes. For example, a single query can return aggregate results alongside its constituent tuples.
SQL supports aggregate queries using aggregate functions (SUM(), COUNT(), AVERAGE(), etc.) and GROUP BY while the aggregate functions in the relational algebra simply accept relation values as arguments.
TutorialD (master/main): :showexpr s group ({s#,sname,status} as subrel):{citycount:=count(@subrel)}
ββββββββ¬ββββββββββ¬ββββββββββββββββββ
βcity βcitycountβsubrel β
ββββββββΌββββββββββΌββββββββββββββββββ€
βParis β2 βββββ¬ββββββ¬ββββββββ
β β ββs#βsnameβstatusββ
β β βββββΌββββββΌβββββββ€β
β β ββS3βBlakeβ30 ββ
β β ββS2βJonesβ10 ββ
β β βββββ΄ββββββ΄ββββββββ
βLondonβ2 βββββ¬ββββββ¬ββββββββ
β β ββs#βsnameβstatusββ
β β βββββΌββββββΌβββββββ€β
β β ββS1βSmithβ20 ββ
β β ββS4βClarkβ20 ββ
β β βββββ΄ββββββ΄ββββββββ
βAthensβ1 βββββ¬ββββββ¬ββββββββ
β β ββs#βsnameβstatusββ
β β βββββΌββββββΌβββββββ€β
β β ββS5βAdamsβ30 ββ
β β βββββ΄ββββββ΄ββββββββ
ββββββββ΄ββββββββββ΄ββββββββββββββββββ
In this query result, we can simultaneously answer how many suppliers are located in each city and which suppliers they are. The expression is constructed by first grouping to isolate the city, then extending the query to add a attribute containing the tuple count for each subrelation.
Function Name | Description |
---|---|
count(relation{any tuple types}) | return the number of tuples in each relation value |
sum(relation{int}) | return the int sum of the relation's values |
max(relation{int}) | return the maximum int from all the relation's values |
min(relation{int}) | return the minimum int from all the relation's values |