An Algebraic Query Language Relational Algebra An Algebraic Query Language Lu Chaojun, SJTU
Operations on Data Recall: A data model provides structures operations constraints Relational model provides operations on relations, expressed in Relational algebra Relational calculus SQL "syntactically sugared" RA (+RC) Lu Chaojun, SJTU
Relational Algebra Algebra operands+operatorsexpressions operands: relations relation = a set of tuples operators = set op + special relational op Lu Chaojun, SJTU
Relational Algebra Expressions Relations as operands, represented by relation names, or constants (a list of tuples) Apply operators to relations/subexpressions Define the schema and set of tuples of the resulting relation! Query = relational algebra expression Lu Chaojun, SJTU
Simplest Query Our running example: Relation name itself is a query. Student(sno,name,age,dept) Course(cno,name,credit) SC(sno,cno,grade) Relation name itself is a query. E.g. Student returns the whole relation as the result Lu Chaojun, SJTU
Set Operations Union: RS Intersection: RS Difference: RS Insertion Duplicates elimination Intersection: RS Difference: RS Deletion Compatibility condition: R and S must have schemas with identical set of attribute:type pairs. Before computation, make the order of attributes the same for R and S. 6 Lu Chaojun, SJTU
Example: Union R: S: RS: Note that one copy of (,2) is eliminated. A B A B 1 2 2 3 A B 1 2 3 Note that one copy of (,2) is eliminated. 7 Lu Chaojun, SJTU
Example: Intersection B A B 1 2 2 3 A B 2 Lu Chaojun, SJTU
Example: Difference R: S: RS: A B A B 1 2 2 3 A B 1 9 Lu Chaojun, SJTU
Projection Projected on some columns: L(R) L is a list of attributes of R E.g. name,age(Student) Duplicates elimination E.g. age(Student) Lu Chaojun, SJTU
Example: Projection R: A,C (R): A B C 10 20 30 40 1 2 A C A C = 11 Lu Chaojun, SJTU
Selection Selection of some rows: C(R) Compute C(R): C is a conditional expression involving the attributes of R attributes, constants,=,<,>,AND,OR,NOT E.g. age>20 AND dept=‘CS’(Student) Compute C(R): Apply C to each tuple t of R by substituting t[A] for each A in C. If t makes C true, then t is in C(R). Lu Chaojun, SJTU
Example: Selection R: A=B ^ D>5 (R) A B C D 1 5 12 23 7 3 10 A B C D 1 23 7 10 13 Lu Chaojun, SJTU
Cartesian Product Combining tables: RS RR is possible. Union of attributes Use qualified name to avoid ambiguities e.g. Student.name and Course.name Pairing tuples in all possible combinations e.g. Student Course RR is possible. It looks odd to glue unrelated tuples together, so is not useful. Lu Chaojun, SJTU
Example: Cartesian Product R: S: RS: A B C D E 1 2 10 20 a b A B C D E 1 2 10 20 a b 15 Lu Chaojun, SJTU
Theta-Join Join: pairing tuples that match. joined tuple dangling tuple Special kind of product: R S (RS) Use to restrict possible pairing Example :-) name(Student Student.sno=SC.sno AND age>grade SC) Most often: is an equation involving shared attributes of R and S. Lu Chaojun, SJTU
Example: Theta-Join R: S: R A=C S: A B C D E 1 2 10 20 a b A 17 Lu Chaojun, SJTU
Natural Join Special kind of Theta-Join: R S Example Implicitly, is: equating shared attributes of R and S Only one copy of shared attributes is left in the resulting relation Example Student.name,Course.name,grade(Student SC Course) Lu Chaojun, SJTU
Example: Natural Join R: S: R S: A B C D B D E 1 2 4 a b 3 2 a c b A B C D E 1 2 a c 19 Lu Chaojun, SJTU
Naming and Renaming Control the names of relations/attributes S(R) S(A1,…,An)(R) S(expression) S(A1,…,An)(expression) Lu Chaojun, SJTU
Combining Operations Apply op. to subexpressions. More than one way to write a query query optimizer Operator precedence Unary ops have highest precedence: , , “Multiplicative” ops are next : , , “Additive” ops are lowest: , , Use parentheses ! Expression tree Lu Chaojun, SJTU
Quotient T = RS Defined for R(X,Y) and S(Y) T(X) T = {t | t X(R) AND sS(ts R)} Lu Chaojun, SJTU
Example: Quotient(1) R: S: RS: A B B A 1 2 1 2 3 4 6 23 Lu Chaojun, SJTU
Example: Quotient(2) R: S: RS: A B C D E D E a a b 1 3 a 24 Lu Chaojun, SJTU
Example: Quotient(3) Find those students who have selected all the courses. sno,cno(SC) cno(Course) Find those students who have selected at least the courses that S007 has taken. sno,cno(SC) cno(sno=‘S007’(SC)) Exercise 2.4.10 RS = X(R) X((X(R) S) R) Lu Chaojun, SJTU
Semijoin R S R’s tuples that agree with at least one tuple in S on all shared attributes Exercise 2.4.8 Equivalent expressions: (1) R(R S) (2) ? (3) ? Lu Chaojun, SJTU
Example: Semijoin R: S: R S: A B C D B D E 1 2 4 a b c 1 3 2 a c b A B C D 1 2 a c 27 Lu Chaojun, SJTU
Independent Set of Operators Some op. can be expressed in terms of others. Independent set of operators: {, , , , , } R S = R (R S) R C S = C (R S) R S = L (C (R S)) C: equating shared attributes L: eliminating duplicate attributes Lu Chaojun, SJTU
Assignment A linear notation for complex RA expressions R(a,b,c) := exp. R is a temporary relation Answer(...) for the resulting relation Example R(no,n,a,d) := dept=‘CS’ (Student) Answer(sno,name) := no,n(R) Lu Chaojun, SJTU
RA as a Constraint Language Two ways to express constraints equal-to-the-emptyset R = set-containment R S The two ways are equivalent: R = iff R R S iff RS = Lu Chaojun, SJTU
Referential Integrity Constraints The object/entity referenced by another object/entity must really exist. Example In SC(sno,cno,grade), if there’s a tuple ('001','CS123',90), then 'CS123' must exist in Course. We can express this constraint by cno(SC) cno(Course) Lu Chaojun, SJTU
Key Constraint Example: Student(sno,name,age,dept) No two tuples agree on sno component. S1.sno=S2.sno AND S1.deptS2.dept(S1 S2) = where S1 and S2 are the results of renaming Student: S1(Student) S2(Student) Similar for other attributes. Lu Chaojun, SJTU
Some Other Constraints Domain constraint: permitted values for an attribute. Example age<0 OR age>200(Student) = More complex constraints age<18 AND cno=‘C1’ (Student SC) = Lu Chaojun, SJTU
End