Relational Algebra CS420. Relation A relation is a two dimensional table that Rows contain data about an entity Columns contains data about attributes.

Slides:



Advertisements
Similar presentations
Foundations of Relational Implementation (2) IS 240 – Database Management Lecture #14 – Prof. M. E. Kabay, PhD, CISSP Norwich University
Advertisements

The Relational Model and Relational Algebra Nothing is so practical as a good theory Kurt Lewin, 1945.
1 Relational Algebra* and Tuple Calculus * The slides in this lecture are adapted from slides used in Standford's CS145 course.
Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
Relational Algebra Rohit Khokher. Relational Algebra Set Oriented Operations UnionIntersectionDifference Cartesian Product Relation Oriented Operations.
1 CHAPTER 4 RELATIONAL ALGEBRA AND CALCULUS. 2 Introduction - We discuss here two mathematical formalisms which can be used as the basis for stating and.
Foundations of Relational Implementation n Defining Relational Data n Relational Data Manipulation n Relational Algebra.
Relational Algebra Ch. 7.4 – 7.6 John Ortiz. Lecture 4Relational Algebra2 Relational Query Languages  Query languages: allow manipulation and retrieval.
1 Lecture 11: Basic SQL, Integrity constraints
Relational Algebra 1 Chapter 5.1 V3.0 Napier University Dr Gordon Russell.
© 2002 by Prentice Hall 1 David M. Kroenke Database Processing Eighth Edition Chapter 8 Foundations of Relational Implementation.
By relieving the brain of all unnecessary work, a good notation sets it free to concentrate on more advanced problems, and, in effect, increases the mental.
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 The mathematical foundation for SQL Basic operators  select  project  union  intersect  set difference  cross product (i.e. Cartesian.
Chapter 14 Getting to First Base: Introduction to Database Concepts.
Relational Model and Relational Algebra Rose-Hulman Institute of Technology Curt Clifton.
RELATIONAL ALGEBRA (III) Prof. Sin-Min LEE Department of Computer Science.
By relieving the brain of all unnecessary work, a good notation sets it free to concentrate on more advanced problems, and, in effect, increases the mental.
© 2002 by Prentice Hall 1 SI 654 Database Application Design Winter 2003 Dragomir R. Radev.
RELATIONAL ALGEBRA (II) Prof. Sin-Min LEE Department of Computer Science.
Relational Algebra.
Concepts of Database Management, Fifth Edition
Copyright © Curt Hill The Relational Algebra What operations can be done?
DBSQL 3-1 Copyright © Genetic Computer School 2009 Chapter 3 Relational Database Model.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 The Relational Algebra.
Relational Algebra A presentation for CS 457 By Dawn Haddan.
Relational Algebra References: Databases Illuminated by Catherine Ricardo, published by Jones and Bartlett in 2004 Fundamentals of Relational Databases.
The Relational Model UC Berkeley Extension Copyright © 2008 Patrick McDermott.
Relational Algebra (Chapter 7)
Chapter 8 Foundations of Relational Implementation David M. Kroenke Database Processing © 2000 Prentice Hall.
11/07/2003Akbar Mokhtarani (LBNL)1 Normalization of Relational Tables Akbar Mokhtarani LBNL (HENPC group) November 7, 2003.
Relational Algebra – Part 2
Chapter 9 Structured Query Language David M. Kroenke Database Processing © 2000 Prentice Hall.
CS 4432query processing1 CS4432: Database Systems II Lecture #11 Professor Elke A. Rundensteiner.
1.1 CAS CS 460/660 Introduction to Database Systems Relational Algebra.
The Relational Algebra and Calculus
CS424 Relational Data Manipulation Relational Data Manipulation Relational tables are sets. Relational tables are sets. The rows of the tables can be considered.
SQL “Structured Query Language; standard language for relational data manipulation” DB2, SQL/DS, Oracle, INGRES, SYBASE, SQL Server, dBase/Win, Paradox,
Chapter 6 The Relational Algebra Copyright © 2004 Ramez Elmasri and Shamkant Navathe.
Chapter 8 Database Redesign We only discuss complex sub-queries.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Management Systems Chapter 4 Relational Algebra.
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 )
(C) 2000, The University of Michigan 1 Database Application Design Handout #5 February 4, 2000.
CS 338Relational Algebra4-1 Relational Algebra Lecture Topics Basic concepts and operations of relational algebra Reference, selection, projection Cross-product.
 CS 405G: Introduction to Database Systems Lecture 6: Relational Algebra Instructor: Chen Qian.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4, Part A.
Relational Algebra COMP3211 Advanced Databases Nicholas Gibbins
©Silberschatz, Korth and Sudarshan2.1Database System Concepts - 6 th Edition Chapter 8: Relational Algebra.
Database Systems Chapter 6
CH 9 SQL 9.1 Querying A Single Table 9.2 Querying Multiple Tables
COMP3017 Advanced Databases
CS4432: Database Systems II
Ch 8: Foundations of Relational Implementation
Fundamental of Database Systems
Relational Math CSC 240 (Blum).
Database Systems 10/13/2010 Lecture #4.
Relational Algebra.
LECTURE 3: Relational Algebra
More Relational Algebra
Data Manipulation using Relational Algebra
Getting to First Base: Introduction to Database Concepts
Getting to First Base: Introduction to Database Concepts
Getting to First Base: Introduction to Database Concepts
CENG 351 File Structures and Data Managemnet
Unit Relational Algebra 1
04 SQL & Relational Algebra
Presentation transcript:

Relational Algebra CS420

Relation A relation is a two dimensional table that Rows contain data about an entity Columns contains data about attributes of the entity Cells of the table hold a single value All entries in a column are of the same kind Each column has a unique name The order of the columns is unimportant The order of the rows is unimportant No two rows may be identical

Example of Relations

Relational Algebra Defines operations that work on relations to achieve the desired results The exercise allows people to better understand the database and SQL It is closed, meaning the operands are relations and so is the result It is an example of DML (Data Manipulation Language)

The Seven Relational Operations Union Difference Intersection Product Selection Projection Join

Union The union of two relations A and B is formed by adding the rows from one relation to those of a second relation to produce a third relation with duplications removed It is denoted as A+B.

Union Compatible Two relations are union compatible if The two relations have the same number of columns The corresponding columns have the same domain

Difference The Difference of two relations A and B contains all the rows in A but not in B. A and B must be union compatible It is denoted as A – B The difference of a-b on the right is c A – B  B - A (c)

Intersection The Intersection of two relations A and B contains all the rows in both A and B. A and B must be union compatible It is denoted as A Intersection B The Intersection of A -B on the right is c (c)

Product The Product of two relations A and B contains the concatenation of every row in A with every row in B. It is denoted as A X B If there are 100 2K rows in A and K rows in B, the product of A and B is a relation of 100X5000 rows of (2K+5K) records A and B do not need to be union compatible

Product Example A: STUDENT B: ENROLLMENT C: PRODUCT of A and B

Projection The Project operation is applied on a single relation. It selects specified attributes from a relation with duplications removed The example on the right is a projection on the STUDENT relation’s major and GradeLevel attributes, represented as STUDENT[Major, GradeLevel] Notice the removal of duplicated rows as the result of projection A: STUDENT

Selection The Selection operation is applied on a single relation. It selects specified rows from a relation based on the given condition The examples on the right are the results of (a) STUDENT where major = ‘MATH’ (b) STUDENT where GradeLevel = ‘JR” A: STUDENT

Join The Join operation is a Product followed by a Selection It is this concept of JOIN that connect rows in two different relations together There are two types of joins: equijoin and natural join

Join Examples A: STUDENTB: ENROLLMENT Equijoin Natural Join STUDENT Join ENROLLMENT on SID = Student-Number

Examples Of Relational Algebra The relations on the right will be used in the examples

Examples Of Relational Algebra 1. What are the names used by students? 2. What are the student numbers of all students enrolled in a class? 3. What are the student numbers of all students not enrolled in a class? 4. What are the student numbers of all students enrolled in ‘BD445’? 5. What are the student name of all students enrolled in ‘BD445’? 6. What are the names and meeting times of ‘PARKS classes 7. What are the names of the students who have a class at the same time as ‘BAKERs’

Example (1) What are the names used by students This is an example of Projection, just about the simplest operation STUDENT[Name]

Example (2) What are the student numbers of all students enrolled in a class? This is another example of Projection ENROLLMENT[StudentNumber]

Example (3) What are the student numbers of all students not enrolled in a class? This needs a little think … We know all the student IDs and IDs of all students taking a class, right? The Difference is the answer STUDENT[SID] - ENROLLMENT[StudentNumber]

Example (4) What are the student numbers of all students enrolled in ‘BD445’? This is an example of Selection because only a few records satisfies the condition (ENROLLMENT where ClassName=‘BD445’ ) [StudentNumber]

Example (5) What are the student names of all students enrolled in ‘BD445’? The student names are in STUDENT relation The information about who is taking which class is in the ENROLLMENT relation How to put the two together?  JOIN STUDENT Join ENROLLMENT on SID=StudentNumber where ClassName = ‘BD445’[STUDENT.NAME]

Example (6) What are the names and meeting times of ‘PARKS’ classes Again, JOIN, JOIN, and more JOIN Two possible approaches 1. Select as much as one can at the earliest possible time (STUDENT where name = ‘PARKS’ Join ENROLLMENT on SID = StudentNumber ) Join CLASS on ClassName = CLASS.NAME [CLASS.NAME, Time] 2. Join all then select STUDENT Join ENROLLMENT on SID = StudentNumber Join CLASS on ClassName = CLASS.NAME where STUDENT.name = ‘PARKS’ [CLASS.NAME, Time] Which is a better solution? First one! Why?

Example (7) What are the names of the students BAKER’s classmates. Translate: List the names of all students who take at least one class with a student who’s name is BAKER. This is a hard one. First, there are two BAKERs. Second, how do we express this “taking the same class?”

Example (7 - 2) Let’s try this one first: List the student numbers of the students taking a class with the student whose number is 200. ENROLLEMENT where studentNumber = 200 JOIN ENROLLMENT as E2 on ENROLLMENT.ClassName = E2.ClassName [E2.StudentNumber]

Example (7-3) ENROLLMENT ENROLLMENT as E2

Example (7-4) JONES RYE S E E2 S2

Example (7-Answer) What are the names of the students BAKER’s classmates S Where S.NAME = ‘BAKER’ Join E on S.SID=E.StudentNumber Join E as E2 on E.ClassName = E2.ClassName Join S as S2 on E2.StudentNumber = S2.SID Where S2.SID Not = S.SID [S2.Name]