Relational Algebra - Part 1

Slides:



Advertisements
Similar presentations
พีชคณิตแบบสัมพันธ์ (Relational Algebra) บทที่ 3 อ. ดร. ชุรี เตชะวุฒิ CS (204)321 ระบบฐานข้อมูล 1 (Database System I)
Advertisements

Relational Algebra Dashiell Fryer. What is Relational Algebra? Relational algebra is a procedural query language. Relational algebra is a procedural query.
Ver 1,12/09/2012Kode :CCs 111,sistem basisdataFASILKOM Chapter 2: Relational Model Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan.
CS4432: Database Systems II Query Operator & Algebraic Expressions 1.
Relational Algebra – Basis for Relational Query Languages Based on presentation by Juliana Freire.
CMSC424, Spring 2005 CMSC424: Database Design Lecture 5.
Chapter 2 Relational Model (part II) Hankz Hankui Zhuo
©Silberschatz, Korth and Sudarshan3.1Database System Concepts Chapter 3: Relational Model Structure of Relational Databases Relational Algebra Tuple Relational.
Database Systems Chapter 6 ITM Relational Algebra The basic set of operations for the relational model is the relational algebra. –enable the specification.
©Silberschatz, Korth and Sudarshan3.1Database System Concepts Extended Relational-Algebra-Operations Generalized Projection Outer Join Aggregate Functions.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
Relational algebra SHIRAJ MOHAMED M | MIS 1. Relational algebra Notation SHIRAJ MOHAMED M | MIS 2.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
Lecture 6 Structured Query Language SQL Lecture 6 Structured Query Language SQL Instructor: Haya Sammaneh.
Relational Model Concepts. The relational model represents the database as a collection of relations. Each relation resembles a table of values. A table.
Chapter 6: Formal Relational Query Languages. 6.2 Chapter 6: Formal Relational Query Languages Relational Algebra Tuple Relational Calculus Domain Relational.
CSE314 Database Systems The Relational Algebra and Relational Calculus Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson Ed Slide Set.
Bayu Adhi Tama, ST., MTI. Introduction Relational algebra and relational calculus are formal languages associated with the relational.
Computing & Information Sciences Kansas State University Monday, 08 Sep 2008CIS 560: Database System Concepts Lecture 5 of 42 Monday, 08 September 2008.
Relational Model By Dr.S.Sridhar, Ph.D.(JNUD), RACI(Paris, NICE), RMR(USA), RZFM(Germany) DIRECTOR ARUNAI ENGINEERING COLLEGE TIRUVANNAMALAI.
Yufis Azhar – Teknik Informatika – UMM.  Aggregation function takes a collection of values (of a single attribute) and returns a single value as a result.
Relational Algebra – Part 2
The Relational Algebra and Calculus
Chapter 8: SQL. Data Definition Modification of the Database Basic Query Structure Aggregate Functions.
Chapter 5 Relational Algebra and Relational Calculus Pearson Education © 2009.
©Silberschatz, Korth and Sudarshan2.1Database System Concepts - 5 th Edition, Oct 5, 2006 Example.
Chapter 5 Relational Algebra Pearson Education © 2014.
Advanced Relational Algebra & SQL (Part1 )
©Silberschatz, Korth and Sudarshan3.1Database System Concepts Extended Relational-Algebra-Operations Generalized Projection Aggregate Functions Outer Join.
Presented By: Miss N. Nembhard. Relation Algebra Relational Algebra is : the formal description of how a relational database operates the mathematics.
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.
ASET Relational Algebra continues…. ASET Rename Operation Allows us to name, and therefore to refer to, the results of relational-algebra expressions.
IS 230Lecture 7Slide 1 Relational Algebra Lecture 8 Text Book – Chapter.
Chapter 3: Relational Model III Additional Relational Algebra Operations Additional Relational Algebra Operations Views Views.
Relational Algebra Instructor: Mohamed Eltabakh 1.
Query Languages Language in which user requests information from the database. Categories of languages Procedural Non-procedural, or declarative “Pure”
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 6: Formal Relational.
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
Midlands State University Topic: Relational DB Model
Module 2: Intro to Relational Model
Relational Algebra.
Chapter # 6 The Relational Algebra and Calculus
Relational Model By Dr.S.Sridhar, Ph.D.(JNUD), RACI(Paris, NICE), RMR(USA), RZFM(Germany)
Fundamental of Database Systems
Chapter 3: Relational Model III
Relational Algebra and Relational Calculus
Chapter 2: Intro to Relational Model
Chapter 2: Intro to Relational Model
Elmasri/Navathe, Fundamentals of Database Systems, 4th Edition
Chapter 2: Relational Model
Lecture 4 of 42 Relational Joins Wednesday, 30 January 2008
Introduction to Database
The Relational Algebra and Relational Calculus
Session 3 Welcome: To session 3-the sixth learning sequence
Instructor: Mohamed Eltabakh
Relational Algebra.
The Relational Algebra
Chapter 2: Intro to Relational Model
Chapter 2: Relational Model
Example of a Relation attributes (or columns) tuples (or rows)
Chapter 2: Intro to Relational Model
Relational Model B.Ramamurthy 5/28/2019 B.Ramamurthy.
CS 405G: Introduction to Database Systems
Presentation transcript:

Relational Algebra - Part 1 Much of the material presented in these slides was developed by Dr. Ramon Lawrence at the University of Iowa

Relational Algebra A query language is used to update and retrieve data that is stored in a data model. Relational algebra is a set of relational operations for retrieving data. Just like algebra with numbers, relational algebra consists of operands (which are relations) and a set of operators. Every relational operator takes as input one or more relations and produces a relation as output. Closure property - input is relations, output is relations Unary operations - operate on one relation Binary operations - have two relations as input A sequence of relational algebra operators is called a relational algebra expression, i.e., an expression may contain more than one relational algebra operator.

Relational Algebra Operators Relational Operators: Selection σ Projection Π Cartesian product x Join Union  Difference - Intersection ∩ Division  Summarize Note: Relational algebra is the foundation of ALL relational database systems. SQL gets translated into relational algebra.

Relational Algebra Operators select

Selection Operation The selection operation is a unary operation that takes in a relation as input and returns a new relation as output that contains a subset of the tuples of the input relation. That is, the output relation has the same number of columns as the input relation, but may have less rows. To determine which tuples are in the output, the selection operation has a specified condition, called a predicate, that tuples must satisfy to be in the output. The predicate is similar to a condition in an if statement.

Selection Operation Formal Definition The selection operation on relation R with predicate F is denoted by σF(R). σF(R)={t | t  R and F(t) is true} where R is a relation, t is a tuple variable F is a formula (predicate) consisting of operands that are constants or attributes comparison operators: <, >, =, ≠, ≤, ≥ logical operators: AND, OR, NOT

Selection Example

Selection Questions Write the relational algebra expression that: 1) Returns all employees working on project P2. 2) Returns all employees who are working as a manager on a project. 3) Returns all employees that have been working as a manager for more than 40 months. Show the resulting relation for each case.

Projection Operation The projection operation is a unary operation that takes in a relation as input and returns a new relation as output that contains a subset of the attributes of the input relation and all non-duplicate tuples. The output relation has the same number of tuples as the input relation unless removing the attributes caused duplicates to be present. Question: When are we guaranteed to never have duplicates when performing a projection operation? Besides the relation, the projection operation takes as input the names of the attributes that are to be in the output relation.

Projection Operation Formal Definition The projection operation on relation R with output attributes A1,…,Am is denoted by ΠA1,…,Am(R). ΠA1,…,Am(R)={t[A1,…, Am] | t  R} where R is a relation, t is a tuple variable {A1,…,Am} is a subset of the attributes of R over which the projection will be performed. Order of A1,…, Am fixes their order in the result. Cardinality of ΠA1,…,Am(R) is not necessarily the same as R because of duplicate removal.

Projection Example

Projection Questions Write the relational algebra expression that: 1) Returns only attributes resp and dur. 2) Returns only eno. 3) Returns only pno. Show the resulting relation for each case.

Union Union is a binary operation that takes two relations R and S as input and produces an output relation that includes all tuples that are either in R or in S or in both R and S. Duplicate tuples are eliminated. General form: R  S = {t | t  R or t  S} where R, S are relations, t is a tuple variable. R and S must be union-compatible. To be union-compatible means that the relations must have the same number of attributes with the same domains.

Union Example

Set Difference Set difference is a binary operation that takes two relations R and S as input and produces an output relation that contains all the tuples of R that are not in S. General form: R – S = {t | t  R and t∉S} where R and S are relations, t is a tuple variable. Note that: R – S ≠ S – R R and S must be union compatible.

Set Difference Example

Intersection Intersection is a binary operation that takes two relations R and S as input and produces an output relation which contains all tuples that are in both R and S. General form: R ∩ S = {t | t  R and t  S} where R, S are relations, t is a tuple variable. R and S must be union-compatible. Note that R ∩ S = R – (R – S) = S – (S – R).

Intersection Example

Summarize Operator and Aggregate Functions Aggregate functions take a collection of values (i.e., a relation?) and return a single value as a result. avg: average value min: minimum value max: maximum value sum: sum of values count: number of values The summarize operation in relational algebra E is any relational-algebra expression G1, G2 …, Gn is a list of attributes on which to group (can be empty) Each Fi is an aggregate function Each Ai is an attribute name )

Aggregate Operation – Example Relation R: A B C     7 3 10 g sum(c) (R) sum(c ) 27

Aggregate Operation – Example Relation account grouped by branch-name: branch_name account_number balance Perryridge Brighton Redwood A-102 A-201 A-217 A-215 A-222 400 900 750 700 branch_name g sum(balance) (account) branch_name sum(balance) Perryridge Brighton Redwood 1300 1500 700

Aggregate Functions (Cont.) Result of aggregation does not have a name Can use rename operation to give it a name For convenience, we permit renaming as part of aggregate operation branch_name g sum(balance) as sum_balance (account)