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.

Slides:



Advertisements
Similar presentations
Chapter 6 The Relational Algebra
Advertisements

Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 The Relational Algebra.
พีชคณิตแบบสัมพันธ์ (Relational Algebra) บทที่ 3 อ. ดร. ชุรี เตชะวุฒิ CS (204)321 ระบบฐานข้อมูล 1 (Database System I)
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 (Continued) The Relational Algebra and Calculus.
Chapter 6 Additional Relational Operations Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
The Relational Algebra
Oct 28, 2003Murali Mani Relational Algebra B term 2004: lecture 10, 11.
Database Systems Chapter 6 ITM Relational Algebra The basic set of operations for the relational model is the relational algebra. –enable the specification.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 6- 1.
The Relational Algebra and Calculus. Relational Algebra Overview Relational algebra is the basic set of operations for the relational model These operations.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 The Relational Algebra and Calculus.
Relational Algebra Example Database Application (COMPANY) Relational Algebra –Unary Relational Operations –Relational Algebra Operations From Set Theory.
Final Review Dr. Bernard Chen Ph.D. University of Central Arkansas.
Relational Algebra - Chapter (7th ed )
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 6- 1.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 The Relational Algebra and Calculus.
CS 380 Introduction to Database Systems Chapter 7: The Relational Algebra and Relational Calculus.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 The Relational Algebra.
CSE314 Database Systems The Relational Algebra and Relational Calculus Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson Ed Slide Set.
The Relational Algebra. 1 RELATIONAL ALGEBRARELATIONAL ALGEBRA 2 UNARY RELATIONAL OPERATIONS * SELECT OPERATIONSELECT OPERATION * PROJECT OPERATIONPROJECT.
METU Department of Computer Eng Ceng 302 Introduction to DBMS The Relational Algebra by Pinar Senkul resources: mostly froom Elmasri, Navathe and other.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 6- 1.
Oracle DML Dr. Bernard Chen Ph.D. University of Central Arkansas.
The Relational Algebra and Calculus
October 9, Announcements Exam Tuesday in class –Closed book Program 2 due Friday –Turn in using D2L dropbox –Only turn in the 4 files specified.
Relational Algebra. 2 Outline  Relational Algebra Unary Relational Operations Relational Algebra Operations from Set Theory Binary Relational Operations.
Chapter 6 The Relational Algebra Copyright © 2004 Ramez Elmasri and Shamkant Navathe.
Chapter 5 Relational Algebra and Relational Calculus Pearson Education © 2009.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 6- 1.
DatabaseIM ISU1 Fundamentals of Database Systems Chapter 6 The Relational Algebra.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 The Relational Algebra.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 6- 1.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 Part a The Relational Algebra and Relational Calculus Hours 1,2.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
Al-Maarefa College for Science and Technology INFO 232: Database systems Chapter 3 “part 2” The Relational Algebra and Calculus Instructor Ms. Arwa Binsaleh.
Advanced Relational Algebra & SQL (Part1 )
The Relational Algebra and Calculus. Slide 6- 2 Chapter Outline Relational Algebra Unary Relational Operations Relational Algebra Operations From Set.
The Relational Algebra. Slide 6- 2 Outline Relational Algebra Unary Relational Operations Relational Algebra Operations From Set Theory Binary Relational.
Slide 6- 1 Additional Relational Operations Aggregate Functions and Grouping A type of request that cannot be expressed in the basic relational algebra.
Chapter 6 The Relational Algebra and Calculus Copyright © 2004 Ramez Elmasri and Shamkant Navathe.
1 CS 430 Database Theory Winter 2005 Lecture 5: Relational Algebra.
Announcements Today –Finish RDM (Chapter 5), begin relational algebra Reading –Sections Program 2 –Due Friday Exam –Tuesday Oct 16, in class –Closed.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
Dr. Mohamed Hegazi1 The Relational Algebra and Relational Calculus.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 6- 1.
Chapter 6 The Relational Algebra Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 6- 1.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus تنبيه : شرائح العرض.
Relational Algebra National University of Computer and Emerging Sciences Lecture # 6 June 30,2012.
CSE202 Database Management Systems
Database Systems Chapter 6
The Relational Algebra
The Relational Algebra and Relational Calculus
The Relational Algebra and Relational Calculus
Chapter (6) The Relational Algebra and Relational Calculus Objectives
The Relational Algebra and Calculus
Chapter # 6 The Relational Algebra and Calculus
Fundamental of Database Systems
The Relational Algebra and Relational Calculus
Chapter 6: Relational Algebra Fall 2015 Dr. Abdullah Almutairi
Chapter 4 The Relational Algebra and Calculus
CS4222 Principles of Database System
The Relational Algebra and Calculus
The Relational Algebra and Relational Calculus
The Relational Algebra
The Relational Algebra and The Relational Calculus
Presentation transcript:

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  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

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)

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  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 ⅰ. 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  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  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]

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)

Slide ◦ 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

Slide  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 σ dept=' 컴퓨터 ' (student) σ sno=300 ∧ cno='C312' (enroll) σ mid < final (enroll) snosnameyeardept 나수영 정기태 송병길 컴퓨터 컴퓨터 컴퓨터 snocnogrademidfinal 300C312A9095 snocnogrademidfinal C413 C312 C312 C413 E412 AAABCAAABC

Slide  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)

Slide  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.

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

Slide  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.

Slide  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 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  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 나수영 나수영 이찬수 정기태 정기태 정기태 송병길 송병길 송병길 송병길 박종화 컴퓨터 컴퓨터 전기 컴퓨터 컴퓨터 컴퓨터 컴퓨터 컴퓨터 컴퓨터 컴퓨터 산공 C413 E412 C123 C312 C324 C413 C312 C324 C413 E412 C312 AABACAAABCBAABACAAABCB

Slide  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

 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 studnet N enroll snosnameyeardeptcnograd e midfinal 나수영 나수영 이찬수 정기태 정기태 정기태 송병길 송병길 송병길 송병길 박종화 컴퓨터 컴퓨터 전기 컴퓨터 컴퓨터 컴퓨터 컴퓨터 컴퓨터 컴퓨터 컴퓨터 산공 C413 E412 C123 C312 C324 C413 C312 C324 C413 E412 C312 AABACAAABCBAABACAAABCB

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  example (Sno)(Cno) 100C E C C C C C C C E C312 과목번호 (Cno) C413 과목번호 (Cno) C312 C413 과목번호 (Cno) C312 C413 E412 학번 (Sno) 학번 (Sno) 학번 (Sno) 400 Stud_course(SC) 과목 1(C1) 과목 2(C2) 과목 3(C3) SC ÷ C1SC ÷ C2SC ÷ C3

Slide  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)

Slide  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

Slide  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

Slide  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  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 ⅰ. 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 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  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, +

 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 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 ⅲ. 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 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  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  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))