Presentation is loading. Please wait.

Presentation is loading. Please wait.

An Algebraic Query Language

Similar presentations


Presentation on theme: "An Algebraic Query Language"— Presentation transcript:

1 An Algebraic Query Language
Relational Algebra An Algebraic Query Language Lu Chaojun, SJTU

2 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

3 Relational Algebra Algebra operands+operatorsexpressions
operands: relations relation = a set of tuples operators = set op + special relational op Lu Chaojun, SJTU

4 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

5 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

6 Set Operations Union: RS Intersection: RS Difference: RS
Insertion Duplicates elimination Intersection: RS Difference: RS 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

7 Example: Union R: S: RS: 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

8 Example: Intersection
B A B 1 2 2 3 A B 2 Lu Chaojun, SJTU

9 Example: Difference R: S: RS: A B A B   1 2   2 3 A B   1 9
Lu Chaojun, SJTU

10 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

11 Example: Projection R: A,C (R): A B C   10 20 30 40 1 2 A C A C  
= 11 Lu Chaojun, SJTU

12 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

13 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

14 Cartesian Product Combining tables: RS RR 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 RR is possible. It looks odd to glue unrelated tuples together, so  is not useful. Lu Chaojun, SJTU

15 Example: Cartesian Product
R: S: RS: 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

16 Theta-Join Join: pairing tuples that match.
joined tuple dangling tuple Special kind of product: R  S (RS) 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

17 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

18 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

19 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

20 Naming and Renaming Control the names of relations/attributes S(R)
S(A1,…,An)(R) S(expression) S(A1,…,An)(expression) Lu Chaojun, SJTU

21 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

22 Quotient T = RS Defined for R(X,Y) and S(Y) T(X)
T = {t | t  X(R) AND sS(ts  R)} Lu Chaojun, SJTU

23 Example: Quotient(1) R: S: RS: A B B A 1 2        1 2 3 4 6 23
Lu Chaojun, SJTU

24 Example: Quotient(2) R: S: RS: A B C D E D E    a    a b 1 3 a
24 Lu Chaojun, SJTU

25 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 RS = X(R)  X((X(R)  S)  R) Lu Chaojun, SJTU

26 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

27 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

28 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

29 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

30 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 RS =  Lu Chaojun, SJTU

31 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

32 Key Constraint Example: Student(sno,name,age,dept)
No two tuples agree on sno component. S1.sno=S2.sno AND S1.deptS2.dept(S1 S2) =  where S1 and S2 are the results of renaming Student: S1(Student) S2(Student) Similar for other attributes. Lu Chaojun, SJTU

33 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

34 End


Download ppt "An Algebraic Query Language"

Similar presentations


Ads by Google