The Relational Algebra. 1 Relational Algebra Operations From Set Theory * UNIONUNION * INTERSECTIONINTERSECTION * MINUSMINUS * CARTESIAN OPERATIONCARTESIAN.

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.
Relational Algebra Dashiell Fryer. What is Relational Algebra? Relational algebra is a procedural query language. Relational algebra is a procedural query.
The Relational Algebra
Relational Databases - Amit Bhawnani & Nimesh Shah.
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.
The Relational Database Model. 2 Objectives How relational database model takes a logical view of data Understand how the relational model’s basic components.
The Relational Algebra and Calculus. Relational Algebra Overview Relational algebra is the basic set of operations for the relational model These operations.
Databases Illuminated
1 The Relational Data Model, Relational Constraints, and The Relational Algebra.
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.
DBSQL 3-1 Copyright © Genetic Computer School 2009 Chapter 3 Relational Database Model.
Relational Model Concepts. The relational model represents the database as a collection of relations. Each relation resembles a table of values. A table.
Chapter 2 Adapted from Silberschatz, et al. CHECK SLIDE 16.
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.
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.
CIS552Relational Model1 Structure of Relational Database Relational Algebra Extended Relational-Algebra-Operations Modification of the Database.
Lecture 15: Relational Algebra
The Relational Algebra. 1 RELATIONAL ALGEBRARELATIONAL ALGEBRA 2 UNARY RELATIONAL OPERATIONS * SELECT OPERATIONSELECT OPERATION * PROJECT OPERATIONPROJECT.
Relational Algebra A presentation for CS 457 By Dawn Haddan.
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)
Relational Algebra – Part 2
The Relational Algebra and Calculus
From Relational Algebra to SQL CS 157B Enrique Tang.
CS424 Relational Data Manipulation Relational Data Manipulation Relational tables are sets. Relational tables are sets. The rows of the tables can be considered.
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.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 6- 1.
Relational Algebra Operators
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 )
Presented By: Miss N. Nembhard. Relation Algebra Relational Algebra is : the formal description of how a relational database operates the mathematics.
1 CS 430 Database Theory Winter 2005 Lecture 5: Relational Algebra.
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.
Chapter 6 The Relational Algebra Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
LECTURE THREE RELATIONAL ALGEBRA 11. Objectives  Meaning of the term relational completeness.  How to form queries in relational algebra. 22Relational.
Ritu CHaturvedi Some figures are adapted from T. COnnolly
CSE202 Database Management Systems
Database Systems Chapter 6
Chapter (6) The Relational Algebra and Relational Calculus Objectives
Module 2: Intro to Relational Model
Fundamental of Database Systems
Relational Algebra.
Lecture 2 The Relational Model
Chapter 2: Intro to Relational Model
Chapter 4 The Relational Algebra and Calculus
Chapter 3 The Relational Database Model
The Relational Algebra and Calculus
The Relational Algebra and Relational Calculus
Data Manipulation using Relational Algebra
Chapter 2: Intro to Relational Model
Chapter 2: Intro to Relational Model
Chapter 2: Intro to Relational Model
Example of a Relation attributes (or columns) tuples (or rows)
Chapter 2: Intro to Relational Model
Presentation transcript:

The Relational Algebra

1 Relational Algebra Operations From Set Theory * UNIONUNION * INTERSECTIONINTERSECTION * MINUSMINUS * CARTESIAN OPERATIONCARTESIAN OPERATION 2 Binary Relational Operations *JOINJOIN *DIVISIONDIVISION

UNION Operation The result of this operation, denoted by R  S, is a relation that includes all tuples that are either in R or in S or in both R and S. Duplicate tuples are eliminated. Union operation is a binary which means that it always operates on two relations. The notation for the union of relations(table) R &S is R  S Is Q We have two relations R&S, then the union of R &S as shown in fig.

IDNAME 501AMIT 503ANAND 504SONAL 506MADHU 507RAJNI 510KAPIL 512RANJIT IDNAME 503ANAND 504SONAL 506MADHU 510KAPIL IDNAME 501AMIT 503ANAND 504SONAL 507RAJNI 510KAPIL 512RANJIT R S R  S

 Properties of Union Operation 1. The input relations must be union compatible. 2. Commutativity -It means that result of R  S is same as that of S  R. 3. Associativity- It means that P  Q  S)= (P  Q)  S Where P,Q and S are relations. BACK TO INDEX

 INTERSECTION OPERATION The result of this operation, denoted by R  S, is a relation that includes all tuples that are in both R and S. The two relations used in intersection operation must be “union compatible’’ The intersection operation is a binary operation. The notation for the Intersection of relations(table) R &S is R  S Is Q We have two relations R&S, then the intersection of R &S as shown in fig.

IDNAME 503ANAND 504SONAL 510KAPIL IDNAME 503ANAND 504SONAL 506MADHU 510KAPIL IDNAME 501AMIT 503ANAND 504SONAL 507RAJNI 510KAPIL 512RANJIT R S Q=R  S

 Properties of Intersection Operation 1. The input relations must be union compatible. 2. Commutativity -It means that result of R  S is same as that of S  R. 3. Associativity- It means that P  Q  S)= (P  Q)  S Where P,Q and S are relations. BACK TO INDEX

 Set Difference (or MINUS) Operation The result of this operation, denoted by R - S, is a relation that includes all tuples that are in R but not in S. The two relations used in difference operation must be “union compatible’’ The Difference operation is a binary operation, which means that it always operates on two relations The notation for the Difference of relations(table) R &S is R  S Is Q

IDNAME 501AMIT 507RAJNI 512RANJIT IDNAME 503ANAND 504SONAL 506MADHU 510KAPIL IDNAME 501AMIT 503ANAND 504SONAL 507RAJNI 510KAPIL 512RANJIT R S Q=R  S

 Properties of Difference Operation 1. The input relations must be union compatible. 2. They are not Commutative-It means that result of R - S is same as that of S - R. 3. They are not Associative- It means that P - ( Q - S) ≠ (P - Q) - S Where P,Q and S are relations. BACK TO INDEX

CARTESIAN (or cross product) Operation The Cartesian Product of two relations result in new relation that include concatenation of every tuple of first relation with every tuple of second relation. In other words, the new relation is created consisting of all possible combinations of the tuple. It is also known as the Cross Product or Cross Join. The Cartesian operation is a binary operation. Which means that it always operates on two relations. The notation for the Cartesian Product of relations(table) R &S is RXS Is Q

R.IDR.NAM E S.IDS.NAME 501AMIT503ANAND 501AMIT506MADHU 503ANAND503ANAND 503ANAND506MADHU 504SONAL503ANAND 504SONAL506MADHU IDNAME 503ANAND 506MADHU IDNAME 501AMIT 503ANAND 504SONAL R Q=RX  S S

 Properties of Cartesian Product Operation 1. The relations on which Cartesian product operation is applied need not necessary to be union compatible. 2. Commutativity-It means that result of R X  S is same as that of S X  R. 3. Associativity- It means that P  X  Q  XS) = (P  X  Q)  XS Where P,Q and S are relations. 4. The resultant relation(table) may hold duplicate attributes if some attributes of two relations are defined on common domains. 5. The Degree of the resultant operation is equal to the sum of the degree of all the relations i.e. Degree of R= Degree of P + Degree of Q 6. The total number of rows in the resultant operation is equal to the product of the number of rows of the first and the second relation i.e. Total number of Tuples of R = Total number of Tuples of P X Total number of Tuples of Q. BACK TO INDEX

 JOIN Operation ◦ The sequence of Cartesian product followed by select is used quite commonly to identify and select related tuples from two relations, a special operation, called JOIN. It is denoted by ∞ ◦ This operation is very important for any relational database with more than a single relation, because it allows us to process relationships among relations. ◦ The general form of a join operation on two relations R(A 1, A 2,..., A n ) and S(B 1, B 2,..., B m ) is: R∞ S where R and S can be any relations that result from general relational algebra expressions. Consider the example of EMP & DEPT table

EMP IDENAMESALARYDEPT ID 101ANU PRIYA ANKUR DEPT IDDNAME 01FINANCE 01MARKETING 02PACKING EMP IDENAMESALARYDEPT ID DNAME 101ANU FINANCE 103PRIYA MARKETING 106ANKUR PACKING EMP∞ dept_id=dept_no DEPT EMP DEPT

In the above example, the DEPT ID attribute of EMP table is matched with the DEPTNO attribute of DEPT table and if both are equal then the resultant tuples from both the tables are fetched as a single row The common attribute in both the tables should not have the same name so as to preserve the uniqueness of the column names in the resultant relation. If they have the same name then they should be renamed to preserve uniqueness. There is another join known as Natural Join in which there is no need of explicitly naming the columns. The join is Performed by joining all those col from the first table to any col in the second table with the same name The result of Natural Join in the above example is as shown

EMP IDENAMESALARYDEPT IDDNAME 101ANU FINANCE 103PRIYA MARKETING 106ANKUR PACKING Natural Join on EMP & DEPT table The natural Join is also referred as Inner Join

The Another type of join in which relation is joined to it self by comparing the values with a col of a single relation is known as Self Join EMP IDENAMEMANG ID 101REKHA KAPIL AMAR106 ATUL PUNEET1-1 ENAME KAPILREKHA AMARATUL PUNEETREKHA EMP THE RESULTANT RELATION THE RESULT OF SELF JOIN

 Properties of Join Operation 1. Commutativity-It means that result of R ⋈  S is same as that of S ⋈  R. 2. Associativity- Using this the same rows appear in the final resuIt regardless of the order in which relations are joined. P  ⋈  Q  ⋈ S) = (P  ⋈ Q)  ⋈ S Where P,Q and S are relations. 3. We can combine a selection with a cross product to form a join. R ⋈  S=  (R X S) 4. We can commute a selection with a join. If the selection condition only involves attributes of one of the arguments to the join.  R ⋈  S )=  R) ⋈  S. The attributes must appear only in R and not in S. 5. Rows whose join attributes are null,don’t appear in the resultants relation(table). 6. More than two tables can also be joined but it increases the complexity. 7. Joins are most commonly used when there exists a relationship between tables i.e. where a join condition is based on a Primary key and Foreign key columns. BACK TO INDEX

 DIVISION Operation The division operation results in a new relation such that every tuple appearing in the resultant relation must exist in the dividend relation in combination with the every tuple in divisor relation. It is binary operation which operates in two relations. The division is denoted by a symbol “÷” The notation for the division of two relations P & Q is P÷Q. where P is the dividend relation and Q is the divisor relation. Consider an example, in which we take two tables P and Q

N n1 n2 N n1 MN m1n1 m1n2 m2n1 m3n3 m4n1 m4n2 m1n3 N n1 n2 n3 Q - PQQQQ iii iiiiv M m1 m4 M m1 m2 m3 M m1 M m2 m3 m4 RRRR The resultant relation R obtained by P÷Q in each case when tuples of Q are different

 Properties of Divide Operation 1. It is a binary operation as it operates on two relations(tables). 2. In the Operation P÷Q, the relation P is known as dividend relation and relation Q is known as a divisor relation. If the dividend relation has degree m+n and the divisor relation has a degree of n, then the resultant relation will have a degree m. 3. The division operation is suited to queries that include the phrase “for all”. 4. It is very rarely used in database applications. BACK TO INDEX

BACK TO INDEX