relational algebra division
TRANSCRIPT
![Page 1: Relational Algebra Division](https://reader035.vdocument.in/reader035/viewer/2022072008/55cf8ffe550346703ba21571/html5/thumbnails/1.jpg)
336 -‐ Division
By Riham ELSAADANY
1
![Page 2: Relational Algebra Division](https://reader035.vdocument.in/reader035/viewer/2022072008/55cf8ffe550346703ba21571/html5/thumbnails/2.jpg)
Ques;on: select sid of the students that are enrolled in all courses.
In order to solve this ques;on, let’s first reason about it as we would do without a computer, then we’ll try to formulate our reasoning into Rela;onal Algebra (RA). So our reasoning will be like: 1. We’ll look up all course names and cid in Courses table:
336,550,660,770. Here, cid alone will get us those info (gets all 4 of them).
2. We’ll look up which students (sid) are associated with every instance of cid in Enrollement table: 101, 102 each is associated with all of 336,550,660,770.
cid cname Dpt
336 DB CS
550 OS CS
660 Network CS
770 Security CS
cid sid semester
336 101 S2015
336 102 S2015
550 101 S2012
550 102 S2013
550 103 S2015
660 101 S2015
660 102 S2015
770 101 S2015
770 102 S2015
Enrollement
Cources
2
![Page 3: Relational Algebra Division](https://reader035.vdocument.in/reader035/viewer/2022072008/55cf8ffe550346703ba21571/html5/thumbnails/3.jpg)
Ques;on: select sid of the students that are enrolled in all courses (Division in RA).
Now let’s translate our reasoning into (RA):
1. To get all cid values: we project on cid colomn: πcid Courses. 2. To get the students (sid) that are associated with
all instances of cid: we project on both colomns in Enrollement table:
πcid,sid Enrollement. 3. Then we divide both projec;ons: the division will simplify the values of
the colomns, to get rid of the sid colomn (common field):
πcid,sid Enrollement/πcid Courses =πsid Enrollement The result will get us only the sid colomn but with the exact values that are enrolled in all courses: 101, 102.
3
![Page 4: Relational Algebra Division](https://reader035.vdocument.in/reader035/viewer/2022072008/55cf8ffe550346703ba21571/html5/thumbnails/4.jpg)
Division Concept • Keywords: ALL, Every. • We need a subset of column X (sid) from table E (Enrollement) that’s related to the
en;re instances (all) of column Y (cid) from table C (Courses). • We start backwards, start where the keyword “All” is located:
1. Get the en;re (all) Y colomn from table C, so we project on that Y column.
πyC 2. Get X (the sid we want) from table E, but to relate the 2 tables C and E: we need to get the
common field Y (cid) , which will happen to be the foreign key in E table, along with X (sid) : so project on X,Y. πx,yE
3. Divide 2./1., so divide both projec;ons of XY/Y = X (Y is simplified, so it doesn’t appear in the result, and X values that have associa;on with all Y is what we end up with).
πx,yE/ πyC
• So Division involves dividing 2 projec;ons, to simplify the common colomn (the en;re colomn following the keyword “all” or “every”, and ending up with the instances of the result having associa;on with all instances of the commun colomn. Let’s implement what we learnt so far in a real life ques;on, as in exams.
4
![Page 5: Relational Algebra Division](https://reader035.vdocument.in/reader035/viewer/2022072008/55cf8ffe550346703ba21571/html5/thumbnails/5.jpg)
Ques;on as in real life situa;ons: select the names of the students that are enrolled in all courses.
sid sname login gpa
101 Mike M44 3.9
102 Suzan Sxx9 3.8
103 Andy Addd 3.7
cid cname Dpt
336 DB CS
550 OS CS
660 Network CS
770 Security CS
cid sid semester
336 101 S2015
336 102 S2015
550 101 S2012
550 102 S2013
550 103 S2015
660 101 S2015
660 102 S2015
770 101 S2015
770 102 S2015
Students Enrollement
Courses
5
![Page 6: Relational Algebra Division](https://reader035.vdocument.in/reader035/viewer/2022072008/55cf8ffe550346703ba21571/html5/thumbnails/6.jpg)
Ques;on: select the names of the students that are enrolled in all courses.
• So now we want the names of those students, not just their sid: 1. So work backwards as we did to perform the division, so
we’re done with associa;ng the Courses to the Enrollement tables: this will end us up with the sid of the students that are enrolled in all courses: 101, 102. (as we did so far)
2. Then we’ll try to associate those sids in the result to the student table sids, in order to check the names of the students related to those sids.
3. So what we’re trying to do is associate some results in the form of (1 colomn having 2 values: 101. 102) to a table. In order to do that, we’ll add those results into another table; let’s call it: Tmp.
4. Associate the table Tmp to Students table.
6
![Page 7: Relational Algebra Division](https://reader035.vdocument.in/reader035/viewer/2022072008/55cf8ffe550346703ba21571/html5/thumbnails/7.jpg)
Final Answer So we’ll select all the results from the division and put them into Tmp table: the selec;on here is equivalent to “select *” without a condi;on, which gets everything from the result (the result has only one colomn containing 2 values: 101, 102).
σ (Tmp, (πx,yE / πyC) ) Then we’ll associate that Tmp to Student table: so we join both tables (remember: join will omit the repe;;on of common colomns, that will get us all columns from the Students table, corresponding to sid 101 and 102). But we only need sname for those resul;ng students: so we project on that colomn:
πsname (Tmp ⋈ Students) Both formulas (wriken on separate lines) represent the answer.
7