Relational Algebra Relational Operators – Set operations –Special relational operators Relational Algebra Expressions PROJECT (SELECT FROM (A TIMES B)…
Relational Algebra Relational Algebra Set of operators that can be applied to database relations Set operatorsSpecial relational operators *Union *Difference Intersection *Cross-product *Selection *Projection Joins Operators signed by (*) are called primitive. It means that any one of them can not be expressed by any combination of other operators. The result of any operator of relational algebra is a relation. Thus relational algebra is shut. It means, we can build any number of nested relational expressions. For example, perform projection of union, selection of joins, joins of a result of some selections.
TNOTNAMETITLECITYSUPNO T1BlakeProfLondonNull T2SmithPhDGlasgowT1 T3JonesProfLondonT1 T4ClarkPhDLiverpoolT3 T5AdamsMScBristolT4 TEACHERS An example of tables for the database School1
SNOSNAMESYEARCITY S1Henry1975London S2Jones1980Davos S3Johnson1983Dublin S4Higgins1984London S5Ford1990Bristol S6Hopkins1990Adelaide STUDENTS An example of tables for the database School1
TNOSNOHOURS T1S164 T1S264 T2S2100 T2S3120 T2S4120 T3S132 T3S236 T3S360 T3S572 T4S196 T4S396 T4S596 T5S632 An example of tables for the database School1 TS
Set Operations - Union Definition Two relations are said to be compatible if the following conditions hold: –they have the same degree n, (the same number of the fields), and –corresponding fields, taken in order from left to right, have the same domains. R UNION S (where R and S are compatible): returns a relation instance containing all tuples that occur in either relation instance R or relation instance S (or both) Examples R – Set of teachers teaching student S5 S - Set of teachers teaching student S6 R UNION S – Set of teachers who teach student S5 or student S6 R S R UNION S If R = (r1, r2, r3) S=(r1,r2,r3.r4) R U S= (r1, r2, r3, r4)
Set Operations - Difference R MINUS S (where R and S are compatible): returns a relation instance containing all tuples that occur in R but not in S. Examples R – Set of teachers teaching student S5 S - Set of teachers teaching student S6 R MINUS S – Set of teachers who teach student S5 but do not teach student S6 R S R MINUS S R - S If R = (r1, r2, r3, r5) S=(r1,r2,r3.r4) R - S= r5 S – R= r4
Set Operations - Intersection R INTERSECT S (where R and S are compatible): returns a relation instance containing all tuples that occur in both R and S. Examples R – Set of teachers teaching student S5 S - Set of teachers teaching student S6 R INTERSECT S – Set of teachers who teach both students S5 and student S6 R S R ∩ S If R = (r1, r2, r3, r5) S=(r1,r2,r3.r4) R ∩ S= r1, r2, r3 S ∩ R= r1, r2, r3 Note that Intersection is not a primitive operator, because: R ∩ S = R – (R – S) = S – (S – R)
Set Operations – Cross-Product R TIMES S : returns a relation instance whose schema contains all the fields of R ( in the same order as they appear in R) followed by all fields of S ( in the same order as they appear in S). The result of R X S contains one tuple (r,s) for each pair of tuples r Є R, s Є S. The cross-product operation is sometimes called Cartesian product. The degree of the new relation resulting from the operation R X S is the sum of degrees of two relations R and S Examples R – Set of all identifiers of teachers S - Set of all identifiers of students R TIMES S – Set of all pairs (ident-teachers, ident-students): {(T1,S1), (T1,S2),…,(T1,S6), (T2,S1),…(T2,S6),…(T5,S1),…,(T5,S6)} ABCABC XyXy AX AY BX BY CX CY product R TIMES S R S
Set Operations – Cross-Product Cross-product using the convention that the fields of R X S inherit names from the corresponding fields of R and S. It is possible for both R and S to contain one or more fields having the same names. This situation creates a naming conflict. E.g. The result of this operation: TEACHERS TIMES STUDENTS have two attributes with the same name CITY. In this situation we use the convention using names of their relations: TEACHERS.CITY, STUDENTS.CITY Therefore, the schema of the relation TEACHERS TIMES STUDENTS has the form: (TNO, TNAME, TITLE, TEACHERS.CITY, SUPNO, SNO, SNAME, SYEAR, STUDENTS.CITY) For better legibility, we can use this convention even if there is no naming conflict, e.g.: (TEACHERS.TNO, TEACHERS.TNAME,…)
Special Relational Operators: Projection How to extract only specified attributes from a relation? For this aim, we use projection operator (PROJECT): A1A2A3B1B A1A2B C PROJECT C OVER A1,A2,B1 D
Special Relational Operators: Projection ABCDE Let A 1, A 2, …, A k be attributes of the relation R PROJECT R OVER A 1, A 2, …, A k : Contains all tuples (A 1,A 2,…A k ) such, that in the relation R a tuple t exists, which as a value of A 1 has a 1, as a value of A 2 has a 2, …, as a value of A k has a k R PROJECT R OVER B, D “vertical” subset from the relation R Note that, as the result of PROJECTION definition, the duplicate tuples are eliminated e.g. PROJECT TEACHERS OVER CITY CITY London Glasgow Liverpool Bristol
Special Relational Operators: SELECTION How to extract only specified tuples from a relation? For this aim, we use selection operator (SELECT): A1A2A3B1B C SELECT FROM C WHERE A1 < 12 A1 A2A3B1B E
Special Relational Operators: Selection ABCDE SELECT R WHERE condition contains all tuples t from the relation R, such that for all t the condition has the value TRUE R SELECT R WHERE… “horizontal” subset from the relation R Example SELECT STUDENTS WHERE SYEAR > 1983 In general, the selection condition is a boolean combination using one of the following operators: =, or > SNOSNAMESYEARCITY S4Higgins1984London S5Ford1990Bristol S6Hopkins1990Adelaide
Special Relational Operators: Joins A1A2A B1B A1A2A3B1B How to combine information from two or more relations? E.g. how to add “Names” of “Clients” to the relation “Ordering”? For this aim, we use JOIN operator (JOIN) A JOIN B ON A1=B2 A B C
Special Relational Operators: Joins TNOTNAMETITLETEACHERS. CITY SUPNOSNOSNAMESYEA R STUDENTS. CITY T1BlakeProf.LondonNullS4Higgins1984London T4ClarkPhDLondonT1S4Higgins1984London T5AdamsMScBristolT4S5Fords1990Bristol R JOIN S WHERE condition: contains all tuples t, such that t is concatenation of the tuple r from relation R and tuple s from relation S, and for each tuple t, the condition has TRUE value. JOIN can be also defined as a cross-product followed by selection and projections R JOIN S WHERE condition= SELECT (R TIMES S) WHERE condition Example TEACHERS JOIN STUDENTS WHERE TEACHERS.CITY = STUDENTS.CITY AND SYEAR > 1983
Special Relational Operators: Joins JOINS can be used for more tables: R JOIN S JOIN T JOIN … WHERE condition= SELECT (R TIMES S TIMES T TIMES …) WHERE condition Usually, joins are used together with projection, otherwise the result could consist of a large number of columns When the join condition consists solely of equalities of the form R.name1 = S.name2, that is equalities between two fields in R and S, there is some redundancy in retaining both attributes in the result. For join conditions that contain only such equalities, the joint operation is refined by doing an additional projection in which S.name2 is dropped. The joint operation with this refinement is called equijoin. The schema of the result of an equijoin contains the fields of R followed by the fields of S that do not appear in the join condition.
PROJECT (TEACHERS JOIN STUDENTS WHERE TEACHERS.CITY =STUDENTS.CITY AND SYEAR > 1983) OVER TNO, TNAME, TITLE, TEACHERS.CITY, SUPNO, SNO, SNAME, SYEAR TNOTNAMETITLETEACHERS. CITY SUPNOSNOSNAMESYEA R T1BlakeProf.LondonNullS4Higgins1984 T4ClarkPhDLondonT1S4Higgins1984 T5AdamsMScBristolT4S5Fords1990 Example
Special Relational Operators: Joins TS.SNOSNAMEHOURS S1Henry32 S2Jones36 S3Johnson60 S5Ford72 Example PROJECT (TS JOIN STUDENTS WHERE TS.SNO = STUDENTS.SNO AND TS.TNO = “T3”) OVER TS.SNO, SNAME, HOURS Joins according to equality of such attributes are called equijoin Equijoin allow complete – linked together- information from different relations The result shows students, who had lectures with the teacher T3. The attributes TS.SNO, STUDENTS.SNO have a role of a connecting link between relations TS and STUDENTS (TS.SNO is a foreign key related to the relation STUDENTS).
TS.TNOTS.SNOHOURSSTUDENTS.SNOSNAMESYEARCITY … … T3 S132S1Henry1975London T3 S236S2Jones1980Davos T3 S360S3Johnson1983Dublin T3 S572S5Ford1990Bristol... … TS.SNO = STUDENTS.SNO TS.TNO=T3
Special Relational Operators: Joins Example SELECT (PROJECT (TS JOIN STUDENTS JOIN TEACHERS WHERE TS.SNO=STUDENTS.SNO AND TS.TNO=TEACHERS.TNO) OVER TS.SNO, SNAME, TS.TNO, TNAME, HOURS) WHERE HOURS > 95 TS.SNOSNAMETS.TNOTNAMEHOURS S2JonesT2Smith100 S3JohnsonT2Smith120 S4HigginsT2Smith120 S1HenryT4Clark96 S3JohnsonT4Clark96 S5FordT4Clark96 Result shows all pairs (student, teacher) such, that student and teacher have spent together on subjects more than 95 hours. Moreover, number of spent hours is shown.
TNOTNAMETITLECITYSUPNO T1BlakeProfLondonNull T2SmithPhDGlasgo w T1 T3JonesProfLondonT1 T4ClarkPhDLiverpo ol T3 T5AdamsMScBristolT4 TNOSNOHOURS T1S164 T1S264 T2S2100 T2S3120 T2S4120 T3S132 T3S236 T3S360 T3S572 T4S196 T4S396 T4S596 T5S632 SNOSNAMESYEARCITY S1Henry1975London S2Jones1980Davos S3Johnson1983Dublin S4Higgins1984London S5Ford1990Bristol S6Hopkins1990Adelaide = = TEACHE RS.TNO TNAMETITLETEACHE RS.CITY SUPNOTS.TNOTS.SNOHOURSSTUDEN TS.SNO SNAMESYEARSTUDEN TS.CITY T1BLAKEPROFLONDONNULLT1S164S1HENRY1975LONDON
Joins, Projection and Selection - example SELECT FROM ( PROJECT (Clients JOIN Ordering ON Clients.NO = Ordering.Client) OVER NO, Name, No-Ord, Date) WHERE Date >= ’ ’ NONameNo-OrdDate Ahmad Ahmad Said Said Ahmad Kemal Kemal SELECT NO,Name, No-Ord, Date FROM Clients, Ordering WHERE Clients.No = Ordering.Client AND Date >=’ ’’ SELECT NO, Name, No-Ord, Date FROM Clients JOIN Ordering ON Clients.No = Ordering.Client WHERE Date >= ’
Relational database - example NoNameStatus Ahmad Said Ali Kemal90 Ord- No DateClientWholes SMALL NEW ONLY HERE SMALL ONLY HERE ABDATE ABC AAA Clients Ordering An example of tables Clients and Ordering from the relations of the previous database NONameNo-OrdDate Ahmad Ahmad Said Said Ahmad Kemal Kemal From previous slide