Presentation is loading. Please wait.

Presentation is loading. Please wait.

Jun-Ki Min. 2  Procedural language ◦ The user instructs the system to perform a sequence of operations on the database to compute the desired result.

Similar presentations


Presentation on theme: "Jun-Ki Min. 2  Procedural language ◦ The user instructs the system to perform a sequence of operations on the database to compute the desired result."— Presentation transcript:

1 Jun-Ki Min

2 2  Procedural language ◦ The user instructs the system to perform a sequence of operations on the database to compute the desired result  what & how ◦ Relational algebra  Nonprocedural language ◦ The user describes the information desired without a specific procedure for obtaining the information  what ◦ Relational calculus  1.Tuple Relational Calculus  2.Domain Relational Calculus  Relational Algebra and Relational Calculus have same expression/computing power

3 Slide 6- 3  Relational algebra is the basic set of operatio ns for the relational model  These operations enable a user to specify bas ic retrieval requests (or queries)  The result of an operation is a new relation, w hich may have been formed from one or more input relations ◦ This property makes the algebra “closed” (all object s in relational algebra are relations)

4 Slide 6- 4  The algebra operations thus produce new rela tions ◦ These can be further manipulated using operations of the same algebra  A sequence of relational algebra operations f orms a relational algebra expression ◦ The result of a relational algebra expression is also a relation that represents the result of a da tabase query (or retrieval request)

5 5  Relational Algebra consists of several groups of operations ◦ Relational Algebra Operations From Set Theory  UNION (  )  INTERSECTION (  )  DIFFERENCE (or MINUS, – )  CARTESIAN PRODUCT ( x ) ◦ Relational Operations  Unary Relational Operations  SELECT (symbol:  (sigma))  PROJECT (symbol:  (pi))  RENAME (symbol:  (rho))  Binary Relational Operations  JOIN (several variations of JOIN exist, )  DIVISION ( ÷ ) ◦ Additional Relational Operations  OUTER JOINS,  OUTER UNION  AGGREGATE FUNCTIONS (These compute summary of information: for example, SUM, COUNT, AVG, MIN, MAX)  closure property ◦ Operand and operation results are relation ◦ Support nested expressions

6 6 ⅰ. union,∪ R∪S = { t | t∈R ∨ t∈S } |R∪S| ≤ |R| + |S| ⅱ. intersect, ∩ R∩S = { t | t∈R ∧ t∈S } |R∩S| ≤ min{ |R|, |S| } ⅲ. difference,- R­S = { t | t∈R ∧ t  S } |R­S| ≤ |R| ⅳ. Cartesian product,× R×S = { r·s | r∈R ∧ s∈S } · : concatenation |R×S| = |R|×|S| degree = R’s degree + S’s degree

7 7  Note ◦ The two operand relations R and S must be “UNION compatible”  ∪, ∩, -  R and S must have same number of attributes  Each pair of corresponding attributes must be type compatible (have same or compatible domains)  Notice that ∪, ∩, × are commutative operations; that is ◦ R  S = S  R, and R  S = S  R  ∪, ∩, × can be treated as n-ary operations applicable to any number of relations as both are associative operations; that is ◦ R  (S  T) = (R  S)  T ◦ (R  S)  T = R  (S  T)  The minus operation is not commutative; that is, in general ◦ R – S ≠ S – R

8 8  Relation : R(X) = R(A 1,..., A n )  R’s tuple r : R={r | r = } ◦ a i : tuple r’s attribute A i value ◦ a i = r.A i = r[A i ]  In general, ◦ = = r[A 1, A 2, … A n ] = r[X]

9 Slide 6- 9  The SELECT operation (denoted by  (sigma)) is used to select a s ubset of the tuples from a relation based on a selection condition. ◦ The selection condition acts as a filter ◦ Keeps only those tuples that satisfy the qualifying condition ◦ Tuples satisfying the condition are selected whereas the oth er tuples are discarded (filtered out)  horizontal subset  A  v (R) = { r | r∈R ∧ r.Aθv }  A  B (R) = { r | r∈R ∧ r.Aθr.B } where, θ(theta) :, ≤, ≥, =, ≠  Examples: ◦ Select the EMPLOYEE tuples whose department number is 4:  DNO = 4 (EMPLOYEE) ◦ Select the employee tuples whose salary is greater than $30,00 0:  SALARY > 30,000 (EMPLOYEE)

10 Slide 6- 10 ◦ In general, the select operation is denoted by  (R) where  the symbol  (sigma) is used to denote the select oper ator  the selection condition is a Boolean (conditional) expre ssion specified on the attributes of relation R  tuples that make the condition true are selected  appear in the result of the operation  tuples that make the condition false are filtered out  discarded from the result of the operation

11 Slide 6- 11  SELECT Operation Properties ◦ The SELECT operation  (R) produces a relation S th at has the same schema (same attributes) as R ◦ SELECT  is commutative:   (  (R)) =  (  (R)) ◦ Because of commutativity property, a cascade (sequence) of SELEC T operations may be applied in any order:   (  (  (R)) =  (  (  ( R))) ◦ A cascade of SELECT operations may be replaced by a single select ion with a conjunction of all the conditions:   (  (  (R)) =  AND AND (R))) ◦ The number of tuples in the result of a SELECT is less than (or equal to) the number of tuples in the input relation R ◦ Selectivity

12 12 σ dept=' 컴퓨터 ' (student) σ sno=300 ∧ cno='C312' (enroll) σ mid < final (enroll) snosnameyeardept 100 300 400 나수영 정기태 송병길 414414 컴퓨터 컴퓨터 컴퓨터 snocnogrademidfinal 300C312A9095 snocnogrademidfinal 100 300 400 400 400 C413 C312 C312 C413 E412 AAABCAAABC 90 90 90 80 65 95 95 95 85 75

13 Slide 6- 13  PROJECT Operation is denoted by  (pi)  In Relation R(X), if Y⊆X and Y={B 1,B 2, …,B m },  Y (R) = { | r∈R }  vertical subset  This operation keeps certain columns (attributes) from a relation and discards the other columns. ◦ PROJECT creates a vertical partitioning  The list of specified columns (attributes) is kept in each tuple  The other attributes in each tuple are discarded  Example: To list each employee’s first and last na me and salary, the following is used:  LNAME, FNAME,SALARY (EMPLOYEE)

14 Slide 6- 14  The general form of the project operation is:  (R) ◦ (pi) is the symbol used to represent the project op eration ◦ is the desired list of attributes from relation R.  The project operation removes any duplicate t uples ◦ This is because the result of the project operation must be a set of tuples  Mathematical sets do not allow duplicate elements.

15 Slide 6- 15  PROJECT Operation Properties ◦ The number of tuples in the result of projection  (R) is always less or equal to the number of tuples in R  If the list of attributes includes a key of R, then the nu mber of tuples in the result of PROJECT is equal to the number of tuples in R ◦ PROJECT is not commutative   (  (R) ) =  (R) as long as co ntains the attributes in

16 16 sname,dept (student) cname,pname (course)   snamedept 나수영 이찬수 정기태 송병길 박종화 컴퓨터 전기 컴퓨터 컴퓨터 컴퓨터 cnamepname 프로그래밍 자료구조 화일구조 데이타베이스 반도체 김성국 황수관 이규찬 이일로 홍봉진

17 Slide 6- 17  JOIN Operation (denoted by ) ◦ The sequence of CARTESIAN PRODECT followed by SEL ECT is used quite commonly to identify and select rela ted tuples from two relations ◦ A special operation, called JOIN combines this sequen ce into a single operation ◦ This operation is very important for any relational dat abase with more than a single relation, because it allo ws us combine related tuples from various relations ◦ The general form of a join operation on two relations R(A1, A2,..., An) and S(B1, B2,..., Bm) is: R S ◦ where R and S can be any relations that result from ge neral relational algebra expressions.

18 Slide 6- 18  Consider the following JOIN operation: ◦ R(A1, A2,..., An) S(B1, B2,..., Bm) R.Ai=S.Bj ◦ Result is a relation Q with degree n + m attributes:  Q(A1, A2,..., An, B1, B2,..., Bm), in that order. ◦ The resulting relation state has one tuple for each co mbination of tuples—r from R and s from S, but only i f they satisfy the join condition r[Ai]=s[Bj] ◦ Hence, if R has n R tuples, and S has n S tuples, then the join result will generally have less than n R * n S tuples. ◦ Only related tuples (based on the join condition) will a ppear in the result

19 19 For R(X), S(Y), A∈X, B∈Y, R AθB S = { r · s | r∈R ∧ s∈S ∧ ( r.Aθs.B) } ◦ A, B : join attribute ◦ θ can be any general boolean expression on the attributes of R and S; for example:  R.Ai<S.Bj AND (R.Ak=S.Bl OR R.Ap<S.Bq)

20 20  In theta join, when θ is " = " R A=B S = { r·s | r∈R ∧ s∈S ∧ ( r.A = s.B ) } ◦ The most common use of join involves join conditions with equality comparisons only  Example student sno=sno enroll student.sn o snameyeardeptenroll.snocnograd e midfinal 100 100 200 300 300 300 400 400 400 400 500 나수영 나수영 이찬수 정기태 정기태 정기태 송병길 송병길 송병길 송병길 박종화 4431114444244311144442 컴퓨터 컴퓨터 전기 컴퓨터 컴퓨터 컴퓨터 컴퓨터 컴퓨터 컴퓨터 컴퓨터 산공 100 100 200 300 300 300 400 400 400 400 500 C413 E412 C123 C312 C324 C413 C312 C324 C413 E412 C312 AABACAAABCBAABACAAABCB 90 95 85 90 75 95 90 95 80 65 85 95 95 80 95 75 95 95 90 85 75 80

21 Slide 6- 21  NATURAL JOIN Operation ◦ Another variation of JOIN called NATURAL JOIN — denoted by N — wa s created to get rid of the second (superfluous) attribute in an EQUIJOI N condition.  because one of each pair of attributes with identical values is superfluou s  natural join: N ) If R(X), S(Y)’s join attribute is Z(=X∩Y) R N S = { [X∪Y] | r∈R∧s∈S∧r[Z] = s[Z] } =  X∪Y (  Z=Z (R×S)) =  X∪Y (R Z=Z S) ◦ The standard definition of natural join requires that the two join attrib utes, or each pair of corresponding join attributes, have the same nam e in both relations

22  example: Q  R(A,B,C,D) N S(C,D,E) ◦ The implicit join condition includes each pair of attributes with the same name, “AND”ed together:  R.C=S.C AND R.D.S.D ◦ Result keeps only one attribute of each such pair:  Q(A,B,C,D,E)

23 23 studnet N enroll snosnameyeardeptcnograd e midfinal 100 100 200 300 300 300 400 400 400 400 500 나수영 나수영 이찬수 정기태 정기태 정기태 송병길 송병길 송병길 송병길 박종화 4431114444244311144442 컴퓨터 컴퓨터 전기 컴퓨터 컴퓨터 컴퓨터 컴퓨터 컴퓨터 컴퓨터 컴퓨터 산공 C413 E412 C123 C312 C324 C413 C312 C324 C413 E412 C312 AABACAAABCBAABACAAABCB 90 95 85 90 75 95 90 95 80 65 85 95 95 80 95 75 95 95 90 85 75 80

24 24  DIVISION Operation ◦ The division operation is applied to two relations R(X), S(Y) ◦ R÷S = { t | t∈  D (R) ∧ t · s∈R for all s∈S }, where Y  X. Let D = X-Y(and hence X =D  Y); that is, let D be the set of attributes of R that are not attributes of S. ◦ The result of DIVISION is a relation T(Y) that includes a tuple t if tuples t R appear in R with t R [Y] = t, and with  t R [X] = t s for every tuple t s in S. ◦ For a tuple t to appear in the result T of the DIVISION, the values in t must appear in R in combination with every tuple in S.  Note : ((R ÷ S) × S) ⊆ R

25 25  example (Sno)(Cno) 100C413 100E412 200C123 300C312 300C324 300C413 400C312 400C324 400C413 400E412 500C312 과목번호 (Cno) C413 과목번호 (Cno) C312 C413 과목번호 (Cno) C312 C413 E412 학번 (Sno) 100 300 400 학번 (Sno) 300 400 학번 (Sno) 400 Stud_course(SC) 과목 1(C1) 과목 2(C2) 과목 3(C3) SC ÷ C1SC ÷ C2SC ÷ C3

26 Slide 6- 26  The RENAME operator is denoted by  (rho)  In some cases, we may want to rename the at tributes of a relation or the relation name or both ◦ Useful when a query requires multiple ope rations ◦ Necessary in some cases (see JOIN operati on later)

27 Slide 6- 27  The general RENAME operation  can be expr essed by any of the following forms: ◦ S (B1, B2, …, Bn ) (R) changes both:  the relation name to S, and  the column (attribute) names to B1, B1, …..Bn ◦ S (R) changes:  the relation name only to S ◦ (B1, B2, …, Bn ) (R) changes:  the column (attribute) names only to B1, B1, …..Bn

28 Slide 6- 28  For convenience, we also use a shorthand for ren aming attributes in an intermediate relation: ◦ If we write: RESULT   FNAME, LNAME, SALARY (DEP5_EMPS) RESULT will have the same attribute names as DEP 5_EMPS (same attributes as EMPLOYEE) If we write: RESULT (F, M, L, S, B, A, SX, SAL, SU, DNO)    RESU LT (F.M.L.S.B,A,SX,SAL,SU, DNO) (DEP5_EMPS) The 10 attributes of DEP5_EMPS are renamed to F, M, L, S, B, A, SX, SAL, SU, DNO, respectively

29 Slide 6- 29  The set of operations including SELECT , PR OJECT , UNION , DIFFERENCE , RENAME , and CARTESIAN PRODUCT X is called a comp lete set because any other relational algebra e xpression can be expressed by a combination of these five operations.  For example: ◦ R  S = (R  S ) – ((R  S)  (S  R)) ◦ R S =  (R X S)

30 30  primitive operations ◦ They are not replaced by other operation  ∪, ­, ×, , ,   composite operations ◦ relational algebra expression can be expressed by a combination of primitive operations.  intersect(∩), join( ), division(÷) R∩S = R­ (R­S) = S ­ (S­R) = (R∪S) ­ ( (R­S) ∪ (S­R) ) R AθB S =  AθB (R×S) R(Z,Y)÷S(Y) = R[Z] - ((R[Z]×S) - R)[Z] ◦ Enhance expression power rather than computing power

31 31 ⅰ. semijoin: ◦ R S: R’s tuples that can be natual join with S  Let R(X), S(Y)’s join attribute be Z(=X∩Y), R S = R N (  Z (S)) =  X (R N S)  Property ◦ R S ≠ S R ◦ R N S = (R S) N S = (S R) N R

32 32 R S R S  X∩Y (S) ( 자연 조인 ) ( 세미 조인 ) ABC a1 a2 a3 a4 b1 b2 c1 c2 c3 BCD b1 b2 c1 c3 d1 d2 d3 BC b1 b2 c1 c3 ABCD a1 a2 a4 b1 b2 c1 c3 d1 d2 d1 d2 d3 ABC a1 a2 a4 b1 b2 c1 c3 NNNN

33 33  The OUTER JOIN Operation ◦ In NATURAL JOIN and EQUIJOIN, tuples without a matching (or related) tuple are eliminated from the join result  Tuples with null in the join attributes are also eliminated  This amounts to loss of information. ◦ A set of operations, called OUTER joins, can be used when we want to keep all the tuples in R, or all those in S, or all those in both relations in the result of the join, regardless of whether or not they have matching tuples in the other relation. ⅱ. outerjoin, +

34  The left outer join operation keeps every tuple in the first or left relation R in R S; if no matching tuple is found in S, then the attributes of S in the join result are filled or “padded” with null values.  A similar operation, right outer join, keeps every tuple in the second or right relation S in the result of R S.  A third operation, full outer join, denoted by or +, keeps all tuples in both the left and the right relations when no matching tuples are found, padding them with null values as needed.

35 35 R S (Outer Join)(Natural Join) R S + ABC a1 a2 a3 a4 b1 b2 c1 c2 c3 BCD b1 b2 b3 c1 c3 d1 d2 d3 ABCD a1 a2 a3 a4 b1 b2 b3 c1 c2 c3 d1 d2 d1 d2 d3 ABCD a1 a2 a4 b1 b2 c1 c3 d1 d2 d1 d2 d3 N + N

36 36 ⅲ. outer-union, ∪ + ◦ The outer union operation was developed to take the union of tuples from two relations if the relations are not type compatible. ◦ This operation will take the union of tuples in two relations R(X, Y) and S(X, Z) that are partially compatible, meaning that only some of their attributes, say X, are type compatible. ◦ The attributes that are type compatible are represented only once in the result, and those attributes that are not type compatible from either relation are also kept in the result relation T(X, Y, Z).

37 37 R S ABC a1 a2 a3 a4 b1 b2 c1 c2 c3 BCD b1 b2 c1 c2 d1 d2 d3 ABCD a1 a2 a3 a4 b1 b2 b1 b2 c1 c2 c3 c1 c2 d1 d2 d3 ∪+∪+

38 38  A type of request that cannot be expressed in the basic relational algebra is to specify mathematical aggregate functions on collections of values from the database.  Examples of such functions include retrieving the average or total salary of all employees or the total number of employee tuples. ◦ These functions are used in simple statistical queries that summarize information from the database tuples.  Common functions applied to collections of numeric values include ◦ SUM, AVERAGE, MAXIMUM, and MINIMUM.  The COUNT function is used for counting tuples or values. ◦ AVG grade (enroll)  retrieves the average score value from the enroll relation ◦ GROUPyear(student)  Grouping student into subgroups with respect to year ◦ GROUP cno AVG score (enroll)  Retrieve the average score of each cno group of enroll ◦ General Form : G A F B (E)  E : Relational Algebra expression  F : aggregation fuction ( SUM, AVG, MAX, MIN, COUNT)  B : Aggregate attribute  G : GROUP Function  A :Group attribute

39 39  Retrieve all students’ name and dept  sname,dept (student)  Retrieve name and score of a studuent who register C413 course   sname,score (  cno='C413' (student N Register))  Retrieve name of a professor who teaches ‘database’   profname (  cname=‘database' (course))


Download ppt "Jun-Ki Min. 2  Procedural language ◦ The user instructs the system to perform a sequence of operations on the database to compute the desired result."

Similar presentations


Ads by Google