Advanced Relational Algebra & SQL (Part1 )

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)
Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 (Continued) The Relational Algebra and Calculus.
The Relational Algebra
CM036: Advanced Database Lecture 3 Relational Algebra and SQL.
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.
Chapter 3 Section 3.4 Relational Database Operators
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.
M Taimoor Khan Course Objectives 1) Basic Concepts 2) Tools 3) Database architecture and design 4) Flow of data (DFDs)
Relational Algebra - Chapter (7th ed )
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.
M Taimoor Khan Course Objectives 1) Basic Concepts 2) Tools 3) Database architecture and design 4) Flow of data (DFDs)
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 6- 1.
CS 370 Database Systems Lecture 11 Relational Algebra.
Fundamentals of Database Systems Fourth Edition El Masri & Navathe Instructor: Mr. Ahmed Al Astal Chapter 6 The Relational Algebra University Of Palestine.
The Relational Algebra and Calculus
From Relational Algebra to SQL CS 157B Enrique Tang.
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.
Slide 6- 1 CARTESIAN (or cross) Product Operation Defines a relation Q that is the concatenation of every tuple of relation R with every tuple of relation.
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.
Relational Algebra MBAD 613 R. Nakatsu. Relational Data Manipulation Language Query-by-Example; Query-by-Form Transform-Oriented Languages Relational.
The Relational Algebra. Slide 6- 2 Outline Relational Algebra Unary Relational Operations Relational Algebra Operations From Set Theory Binary Relational.
Presented By: Miss N. Nembhard. Relation Algebra Relational Algebra is : the formal description of how a relational database operates the mathematics.
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.
Relational Algebra COMP3211 Advanced Databases Nicholas Gibbins
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.
Ritu CHaturvedi Some figures are adapted from T. COnnolly
Database Systems Chapter 6
The Relational Algebra and Relational Calculus
Chapter (6) The Relational Algebra and Relational Calculus Objectives
The Relational Algebra and Calculus
COMP3017 Advanced Databases
Chapter # 6 The Relational Algebra and Calculus
Fundamental of Database Systems
Relational Algebra.
The Relational Algebra and Relational Calculus
Chapter 6: Relational Algebra Fall 2015 Dr. Abdullah Almutairi
Chapter 4 The Relational Algebra and Calculus
The Relational Algebra and Calculus
LECTURE 3: Relational Algebra
Lecture 3 Relational Algebra and SQL
The Relational Algebra and The Relational Calculus
Presentation transcript:

Advanced Relational Algebra & SQL (Part1 ) Chapter 1 Advanced Relational Algebra & SQL (Part1 ) Department of Computer Science, Umm Al-Qura University University year: 1435/1436 Advanced Database System

Outline Relational Algebra Relational Calculus Unary Relational Operations Relational Algebra Operations From Set Theory Binary Relational Operations Additional Relational Operations Examples of Queries in Relational Algebra Relational Calculus Tuple Relational Calculus Domain Relational Calculus Advanced Database System

Relational Algebra Overview Relational algebra is the basic set of operations for the relational model These operations enable a user to specify basic retrieval requests (or queries) The result of an operation is a new relation, which may have been formed from one or more input relations This property makes the algebra “closed” (all objects in relational algebra are relations) Advanced Database System

Relational Algebra Overview (continued) The algebra operations thus produce new relations These can be further manipulated using operations of the same algebra A sequence of relational algebra operations forms a relational algebra expression The result of a relational algebra expression is also a relation that represents the result of a database query (or retrieval request) Advanced Database System

Operators Basic operations: Additional operations: Selection ( (sigma)) Selects a subset of rows from relation. Projection ( (pi)) Deletes unwanted columns from relation. Cross-product ( (Cartesian product)) Allows us to combine two relations. Set-difference ( (minus)) Tuples in relation.1, but not in relation.2 Union ( ) Tuples in relation.1 and in relation. 2. Additional operations: Intersection ( ) Join ( ) division, renaming Since each operation returns a relation, operations can be composed. Advanced Database System

Unary Relational Operations: SELECT In general, the select operation is denoted by  <selection condition>(R) where the symbol  (sigma) is used to denote the select operator the selection condition is a Boolean (conditional) expression 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 Advanced Database System

Unary Relational Operations: SELECT The SELECT operation (denoted by  ) is used to select a subset of the tuples from a relation based on a selection condition. Examples: Select the EMPLOYEE tuples whose department number is 4:  DNO = 4 (EMPLOYEE) Select the employee tuples whose salary is greater than $30,000:  SALARY > 30,000 (EMPLOYEE) Advanced Database System

Unary Relational Operations: PROJECT The general form of the project operation is: <attribute list>(R)  (pi) is the symbol used to represent the project operation <attribute list> is the desired list of attributes from relation R. The project operation removes any duplicate tuples This is because the result of the project operation must be a set of tuples Mathematical sets do not allow duplicate elements. Advanced Database System

Unary Relational Operations: PROJECT (cont) PROJECT Operation is denoted by  (pi) 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 name and salary, the following is used: LNAME, FNAME,SALARY(EMPLOYEE Advanced Database System

Examples of applying SELECT and PROJECT operations Advanced Database System

Binary Relational Operations: JOIN JOIN Operation (denoted by ) The sequence of CARTESIAN PRODECT followed by SELECT is used quite commonly to identify and select related tuples from two relations A special operation, called JOIN combines this sequence into a single operation This operation is very important for any relational database with more than a single relation, because it allows 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 <join condition>S where R and S can be any relations that result from general relational algebra expressions. Advanced Database System

Binary Relational Operations: JOIN (cont.) Example: Suppose that we want to retrieve the name of the manager of each department. To get the manager’s name, we need to combine each DEPARTMENT tuple with the EMPLOYEE tuple whose SSN value matches the MGRSSN value in the department tuple. We do this by using the join operation. DEPT_MGR  DEPARTMENT MGRSSN=SSN EMPLOYEE MGRSSN=SSN is the join condition Combines each department record with the employee who manages the department The join condition can also be specified as DEPARTMENT.MGRSSN= EMPLOYEE.SSN Advanced Database System

Example of applying the JOIN operation Advanced Database System

Natural Join Operator Natural join is a dyadic operator that is written as R S where R and S are relations. The result of the natural join is the set of all combinations of tuples in R and S that are equal on their common attribute names. Advanced Database System

Natural Join Example For an example, consider the tables Employee and Dept and their natural join: Advanced Database System

Renaming In relational algebra, a rename is a unary operation written as ρ a / b (R) where: a and b are attribute names R is a relation Examples: 1) ρemployee(Emp): Changes the name of Emp table to employee. 2) Renaming and Union Advanced Database System

Cartesian Product In mathematics, it is a set of all pairs of elements (x, y) such that x belongs to X and y to Y. It defines a relation that is the concatenation of every tuple of relation R with every tuple of relation S. Example: Advanced Database System

Theta (θ) join Theta joins combines tuples from different relations provided they satisfy the theta condition. A Cartesian product with a condition applied: R1 ⋈θ R2 Example: Advanced Database System

Semi join Operator The semi join is joining similar to the natural join and written as R S where R and S are relations. The result of the semi join is only the set of all tuples in R for which there is a tuple in S that is equal on their common attribute names. Example: consider the tables Employee and Dept and their semi join: Advanced Database System

Outer join Operator Left outer join R <R.primary_key = S.foreign_key> S All rows from R are retained and unmatched rows of S are padded with NULL Right outer join R <R.primary_key = S.foreign_key> S All rows from S are retained and unmatched rows of R are padded with NULL Advanced Database System

Right Outerjoin Example For an example consider the tables Students and Courses and their right outer join: Advanced Database System

Left Outer join Example For an example consider the tables Students and Courses and their left outer join: Advanced Database System

Union Takes the set of rows in each table and combines them, eliminating duplicates Participating relations must be compatible, ie have the same number of columns, and the same column names, domains, and data types Advanced Database System

Set Operations-Union Takes the set of rows in each table and combines them, eliminating duplicates Participating relations must be compatible, ie have the same number of columns, and the same column names, domains, and data types R ᴜ S R S A B a1 b1 a2 b2 a3 b3 A B a1 b1 a2 b2 A B a2 b2 a3 b3 Advanced Database System

Intersection Takes the set of rows that are common to each relation Participating relations must be compatible S R R ∩ S A B a1 b1 a2 b2 A B a2 b2 a3 b3 A B a2 b2 Advanced Database System

Difference Takes the set of rows in the first relation but not the second Participating relations must be compatible R S R - S A B a1 b1 a2 b2 A B a2 b2 a3 b3 A B a1 b1 Advanced Database System

Set vs Bag semantic A bag (or multiset ) is like a set, but an element may appear more than once. Example1: {1,2,1,3} is a bag. Example2: {1,2,3} is also a bag that happens to be a set. The multi-set {1, 2, 1, 3} is different to multi-set {1, 2, 3}. Advanced Database System

The relational algebra on multi-sets: examples Advanced Database System