Week2 DB Algebra
Week2 DB Algebra
Jianxin Li
id of type CHAR(8)
unit of type CHAR(8)
grade of type INT
id name gender
12345678 Ebenezer Scrooge M
12345682 Jane Austen F
12345689 JMartin Chuzzlewit M
id unit grade
12345678 CITS1402 88
12345678 CITS2211 75
12345682 CITS1402 91
12345682 CITS2211 71
12345689 CITS1402 55
The result is another relation with the same schema, but fewer rows.
id unit grade
12345678 CITS1402 88
12345682 CITS1402 91
This goes through each row, and only keeps the specified columns.
The result is another relation with fewer columns but in this case the
same number of rows.
id
12345678
12345682
12345689
Grade
In MySQL this is not a legal expression, and we must explicitly state that we
want all the columns from a table.
id (grade>80 Grade)
This first selects only the rows with grade > 80 and then projects onto the
id column only.
id Grade
should produce
id
12345678
12345682
12345689
In relational algebra, the projection can pick out any number of columns
id,name Student
Student Grade
What we really want is for each row to combine the Student information
and the Grade information for the same student.
In relational algebra
This forms the Cartesian product, and then selects only the rows where the
two occurrences of id match.
In relational algebra, the natural join automatically matches all columns with
the same name, and then removes one of each duplicate pair.
In relational algebra
Student ./ Grade
will give a relation with five columns and the five rows corresponding to each
student/unit combination.
R1 R2 R1 R2 R1 R2
In the SQL standard, these are specified with the keywords UNION,
INTERSECT and EXCEPT.
But MySQL does not implement the full SQL standard, and in particular does
not have INTERSECT or EXCEPT.