LECTURE THREE RELATIONAL ALGEBRA 11. Objectives  Meaning of the term relational completeness.  How to form queries in relational algebra. 22Relational.

Slides:



Advertisements
Similar presentations
Relational Algebra and Relational Calculus
Advertisements

Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
RELATIONAL ALGEBRA Lecture Relational Algebra Operations to manipulate relations. Used to specify retrieval requests (queries). Query results in.
Database Systems Chapter 6 ITM Relational Algebra The basic set of operations for the relational model is the relational algebra. –enable the specification.
Relational Algebra - Basic Operations CS263 Lecture 11.
Relational Algebra Relational Calculus. Relational Algebra Operators Relational algebra defines the theoretical way of manipulating table contents using.
Lesson II The Relational Model © Pearson Education Limited 1995, 2005.
1 Minggu 3, Pertemuan 5 Relational Algebra (Cont.) Matakuliah: T0206-Sistem Basisdata Tahun: 2005 Versi: 1.0/0.0.
Chapter 5 Relational Algebra and Relational Calculus.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
Database Systems: A Practical Approach to Design, Implementation and Management International Computer Science S. Carolyn Begg, Thomas Connolly Lecture.
1-1 Thomas Connolly and Carolyn Begg’s Database Systems: A Practical Approach to Design, Implementation, and Management Chapter 4 Part One: Relational.
Relational Algebra & Relational Calculus Dale-Marie Wilson, Ph.D.
RELATIONAL ALGEBRA Objectives
Relational Model & Relational Algebra. 2 Relational Model u Terminology of relational model. u How tables are used to represent data. u Connection between.
Chapter 3 Section 3.4 Relational Database Operators
Relational Query Languages. Languages of DBMS  Data Definition Language DDL  define the schema and storage stored in a Data Dictionary  Data Manipulation.
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.
Relational Algebra Chapter 4 CIS 458 Sungchul Hong.
Chapter 4 Relational Algebra & Relational Calculus.
CSE314 Database Systems The Relational Algebra and Relational Calculus Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson Ed Slide Set.
Relational Algebra References: Databases Illuminated by Catherine Ricardo, published by Jones and Bartlett in 2004 Fundamentals of Relational Databases.
M Taimoor Khan Course Objectives 1) Basic Concepts 2) Tools 3) Database architecture and design 4) Flow of data (DFDs)
Bayu Adhi Tama, ST., MTI. Introduction Relational algebra and relational calculus are formal languages associated with the relational.
C HAPTER 4 Relational Algebra and Relational Calculus Transparencies © Pearson Education Limited 1995,
Advanced Database Systems
1 Pertemuan > > Matakuliah: >/ > Tahun: > Versi: >
1.1 CAS CS 460/660 Introduction to Database Systems Relational Algebra.
The Relational Algebra and Calculus
From Relational Algebra to SQL CS 157B Enrique Tang.
Chapter 6 The Relational Algebra Copyright © 2004 Ramez Elmasri and Shamkant Navathe.
Chapter 5 Relational Algebra and Relational Calculus Pearson Education © 2009.
Chapter 5 Relational Algebra Pearson Education © 2014.
1 Geog 357 – Introduction to GIS The Relational Language.
Relational Algebra Operators
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
Relational Algebra MBAD 613 R. Nakatsu. Relational Data Manipulation Language Query-by-Example; Query-by-Form Transform-Oriented Languages Relational.
Advanced Relational Algebra & SQL (Part1 )
CSC271 Database Systems Lecture # 8. Summary: Previous Lecture  Relation algebra and operations  Selection (Restriction), projection  Union, set difference,
Views, Algebra Temporary Tables. Definition of a view A view is a virtual table which does not physically hold data but instead acts like a window into.
Presented By: Miss N. Nembhard. Relation Algebra Relational Algebra is : the formal description of how a relational database operates the mathematics.
1 Pertemuan > > Matakuliah: >/ > Tahun: > Versi: >
1 CS 430 Database Theory Winter 2005 Lecture 5: Relational Algebra.
CSC271 Database Systems Lecture # 7. Summary: Previous Lecture  Relational keys  Integrity constraints  Views.
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.
Query Processing – Implementing Set Operations and Joins Chap. 19.
Chapter 6 The Relational Algebra Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
CSCI 6315 Applied Database Systems Review for Midterm Exam I Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX 78539
April 20022CS3X1 Database Design Relational algebra John Wordsworth Department of Computer Science The University of Reading Room.
Chapter 5 Relational Algebra and Relational Calculus Pearson Education © 2014.
Relational Algebra COMP3211 Advanced Databases Nicholas Gibbins
Chapter 4 Relational Algebra Chapter 4 in Textbook.
Ritu CHaturvedi Some figures are adapted from T. COnnolly
Chapter (6) The Relational Algebra and Relational Calculus Objectives
Relational Algebra Lecture 2.
COMP3017 Advanced Databases
Relational Algebra and Relational Calculus
The Relational Algebra and Relational Calculus
DATABASE SYSTEM.
The Relational Algebra
Relational Algebra and Relational Calculus
Chapter 4 Relational Algebra
Database.
Presentation transcript:

LECTURE THREE RELATIONAL ALGEBRA 11

Objectives  Meaning of the term relational completeness.  How to form queries in relational algebra. 22Relational Algebra

Introduction  Relational algebra and relational calculus are formal languages associated with the relational model (form the basis for relational languages)‏  Informally, relational algebra is a (high-level) procedural language; can tell the DBMS how to build a new relations from other relations in the database. Relational calculus a non-procedural language; used to define a relation in terms of one or more database relations.  However, formally both are equivalent to one another; are a basis for other Data Manipulation Languages for relational databases-illustrates the basic operations required for any DML.  A language that produces a relation that can be derived using relational calculus is relationally complete. 33Relational Algebra

 Relational algebra operations work on one or more relations to define another relation without changing the original relations.  Both operands and results are relations, so output from one operation can become input to another operation.  Allows expressions to be nested, just as in arithmetic.  Relational Algebra is a relation-at-a-time (or set) language in which all tuples, possibly from several relations, are manipulated in one statement without looping 44Relational Algebra

 5 basic operations in relational algebra: Selection, Projection, Cartesian product, Union, and Set Difference.  These perform most of the data retrieval operations needed.  Also have Join, Intersection, and Division operations, which can be expressed in terms of 5 basic operations. 55Relational Algebra

Relational Algebra Operations cont’d  These operations can be categorised into two groups; unary and binary operations.  Unary: These operate on one relation. Examples of unary operations include; selection and projection  Binary; These work on pairs of relations. Examples include the; Cartesian product, union, set difference, join, intersections and division operations 6Relational Algebra

Relational Algebra Operations 77Relational Algebra

Relational Algebra Operations 88Relational Algebra

Selection (or Restriction)‏   predicate (R)‏ Works on a single relation R and defines a relation that contains only those tuples (rows) of R that satisfy the specified condition (predicate). More complex predicates can be generated using the logical operators; Λ (AND), ν (OR) and ~ (NOT)‏ 99Relational Algebra

10 Example - Selection (or Restriction)‏  List all staff with a salary greater than £10,000.  salary > (Staff)‏

Projection   col1,..., coln (R)‏ Works on a single relation R and defines a relation that contains a vertical subset of R, extracting the values of specified attributes and eliminating duplicates. 11 Relational Algebra

Example - Projection  Produce a list of salaries for all staff, showing only their name and salary details.  fName, lName, salary (Staff)‏ 12 Relational Algebra

Union  R  S Union of two relations R and S defines a relation that contains all the tuples of R, or S, or both R and S, duplicate tuples being eliminated. R and S must be union-compatible; both relations have the same number of attributes with each pair of corresponding attributes having the same domain..  If R and S have I and J tuples, respectively, union is obtained by concatenating them into one relation with a maximum of (I + J) tuples. 13 Relational Algebra

Example BRANCH PROPERTYFORRENT

Example - Union  List all cities where there is either a branch office or a property for rent.  city (Branch)   city (PropertyForRent)‏ 15 Relational Algebra

Set Difference  R – S Defines a relation consisting of the tuples that are in relation R, but not in S. R and S must be union-compatible. 16 Relational Algebra

Example - Set Difference  List all cities where there is a branch office but no properties for rent.  city (Branch) –  city (PropertyForRent)‏ 17 Relational Algebra

Intersection  R  S Defines a relation consisting of the set of all tuples that are in both R and S. R and S must be union-compatible.  Expressed in terms of the set difference operation: R  S = R – (R – S)‏ 18 Relational Algebra

Example - Intersection  List all cities where there is both a branch office and at least one property for rent.  city (Branch)   city (PropertyForRent)‏ 19 Relational Algebra

Cartesian product  R X S Defines a relation that is the concatenation of every tuple of relation R with every tuple of relation S; relation with all possible pairs of tuples from the two relations. If relation R has I tuples and N attributes, and relation S has J tuples and M attributes, then the cartesian product will have (I*J) tuples and (N+M) attributes. In case the relations have attributes with the same names, these are prefixed with the relation name to maintain the uniqueness of attribute names in the resulting relation. 20 Relational Algebra

Example - Cartesian Product  List the names and comments of all clients who have viewed a property for rent. (  clientNo, fName, lName (Client)) X (  clientNo, propertyNo,comment (Viewing))‏ 21 Relational Algebra

22 Example - Cartesian Product cont’d Relational Algebra22

Example - Cartesian Product and Selection  Use selection operation to extract those tuples where Client.clientNo = Viewing.clientNo. σ Client.clientNo = viewing.clientNo ((Π clientNo,fName,lName (Client))  (Π clientNo,propertyNo,comment (Viewing)))‏ 23  Cartesian product and Selection can be reduced to a single operation called a Join. 23Relational Algebra

Join Operations  This is one of the essential operations in relational algebra; a join operation combines two relations to form a new one.  Join is a derivative of Cartesian product; Equivalent to performing a Selection, using join predicate as selection formula, over Cartesian product of the two operand relations (Cartesian product that satisfies a given condition(s)).  One of the most difficult operations to implement efficiently in an RDBMS and one reason why RDBMSs have intrinsic performance problems. 24 Relational Algebra

Join Operations  Various forms of join operation Theta join Equijoin (a particular type of Theta join)‏ Natural join Outer join Semijoin 25 Relational Algebra

Theta join (  -join)‏  R F S Defines a relation that contains tuples satisfying the predicate F from the Cartesian product of R and S. The predicate F is of the form R.a i  S.b i where  may be one of the comparison operators (, , =,  ). 26 Relational Algebra

Theta join (  -join)‏ 27  Degree of a Theta join is sum of degrees of the operand relations R and S; as the Cartesian product.  If predicate F contains only equality (=), the term Equijoin is used.  Can rewrite Theta join using basic Selection and Cartesian product operations. R F S =  F (R  S)‏ 27Relational Algebra

Example - Equijoin  List the names and comments of all clients who have viewed a property for rent. (  clientNo,fName,lName (Client)) Client.clientNo = Viewing.clientNo (  clientNo,propertyNo,comment (Viewing))‏ 28 Relational Algebra

Natural Join  R S An Equijoin of the two relations R and S over all common attributes x. One occurrence of each common attribute is eliminated from the result. The degree of a natural join is the sum of the degrees of the relations R and S less the number of attributes in x. 29 Relational Algebra

Example - Natural Join  List the names and comments of all clients who have viewed a property for rent. (  clientNo,fName,lName (Client)) (  clientNo,propertyNo,comment (Viewing))‏ 30 Relational Algebra

Outer join  To display rows in the result that do not have matching values in the join column, use Outer join.  Advantage: preservation of information (that would otherwise be lost).  There are basically three types of outer join operations  Left (natural) Outer join  Right Outer join  Full Outer join 31 Relational Algebra

Left Outer join ( )  R S (Left) outer join is join in which tuples from R that do not have matching values in common columns of S are also included in result relation. The missing values in the second relation are set to null. Relational Algebra32

Example - Left Outer join  Produce a status report on property viewings.  propertyNo,street,city (PropertyForRent) Viewing 33 Relational Algebra

Outer joins cont’d  Right Outer Join: This join keeps all the tuples in the right-hand relation in the result.  Full Outer Join: This join keeps all the tuples in both relations, padding tuples with nulls when no matching tuples are found. Relational Algebra34

Semijoin  R F S Defines a relation that contains the tuples of R that participate in the join of R with S. The Semijoin operation performs a join of the two relations and then projects over the attributes of the first operand. 35  Can rewrite Semijoin using Projection and Join: R F S =  A (R F S)‏ 35Relational Algebra

Example

Example - Semijoin  List complete details of all staff who work at the branch in Glasgow. Staff Staff.brancNo = Branch.branchNo and branch.city = ‘Glasgow’ Branch 37 Relational Algebra

Division (R  S)  Defines a relation over the attributes C that consists of set of tuples from R (attribute set A) that match combination of every tuple in S (attribute set B). Note: C is the set of attributes of R that are not attributes of S (A-B)  Expressed using basic operations: T 1   C (R)‏ T 2   C ((S X T 1 ) – R)‏ T  T 1 – T 2 38 Relational Algebra

Example VIEWING PROPERTYFORRENT

Example - Division  Identify all clients who have viewed all properties with three rooms. (  clientNo,propertyNo (Viewing))  (  propertyNo (  rooms = 3 (PropertyForRent)))‏ 40 Relational Algebra