Operations in the Relational Model COP 4720 Lecture 8 Lecture Notes.

Slides:



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

Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
Relational Algebra Ch. 7.4 – 7.6 John Ortiz. Lecture 4Relational Algebra2 Relational Query Languages  Query languages: allow manipulation and retrieval.
CM036: Advanced Database Lecture 3 Relational Algebra and SQL.
CS4432: Database Systems II Query Operator & Algebraic Expressions 1.
INFS614, Fall 08 1 Relational Algebra Lecture 4. INFS614, Fall 08 2 Relational Query Languages v Query languages: Allow manipulation and retrieval of.
SQL and Relational Algebra Zaki Malik September 02, 2008.
1 Lecture No 16 Asma Ahmad Relational Algebra I March 10, 2011 Database Systems.
Relational Algebra.
Midterm Review II. Redundancy. –Information may be repeated unnecessarily in several tuples. –E.g. length and filmType. Update anomalies. –We may change.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 52 Database Systems I Relational Algebra.
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.
Relational Model and Relational Algebra Rose-Hulman Institute of Technology Curt Clifton.
Operations in the Relational Model These operation can be expressed in an algebra, called “relational algebra”. In this algebra relations are the operands.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
1 Relational Algebra Basic operators Relational algebra expression tree.
Relational Algebra Basic Operations Algebra of Bags.
Relational Algebra Example Database Application (COMPANY) Relational Algebra –Unary Relational Operations –Relational Algebra Operations From Set Theory.
Relational Algebra CIS 4301 Lecture Notes Lecture /28/2006.
Relational Algebra Spring 2012 Instructor: Hassan Khosravi.
Relational Algebra - Chapter (7th ed )
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 The Relational Algebra and Calculus.
CSE314 Database Systems The Relational Algebra and Relational Calculus Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson Ed Slide Set.
+ Division Not supported as a primitive operator, but useful for expressing queries like: Find sailors who have reserved all boats. Precondition: in A/B,
Lecture 15: Relational Algebra
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)
Introduction to Data Manipulation in SQL CIS 4301 Lecture Notes Lecture /03/2006.
Relational Algebra – Constraints CIS 4301 Lecture Notes Lecture /02/2006.
Relational Model COP 4720 Lecture 9 Lecture Notes.
Relational Algebra (Chapter 7)
The Relational Algebra and Calculus
Chapter 6 The Relational Algebra Copyright © 2004 Ramez Elmasri and Shamkant Navathe.
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.
Rensselaer Polytechnic Institute CSCI-4380 – Database Systems David Goldschmidt, Ph.D.
Advanced Relational Algebra & SQL (Part1 )
More Relation Operations 2015, Fall Pusan National University Ki-Joune Li.
603 Database Systems Senior Lecturer: Laurie Webster II, M.S.S.E.,M.S.E.E., M.S.BME, Ph.D., P.E. Lecture 10 A First Course in Database Systems.
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.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
603 Database Systems Senior Lecturer: Laurie Webster II, M.S.S.E.,M.S.E.E., M.S.BME, Ph.D., P.E. Lecture 11 A First Course in Database Systems.
603 Database Systems Senior Lecturer: Laurie Webster II, M.S.S.E.,M.S.E.E., M.S.BME, Ph.D., P.E. Lecture 9 A First Course in Database Systems.
The Relational Model of Data Prof. Yin-Fu Huang CSIE, NYUST Chapter 2.
CS 157B Database Systems Dr. T Y Lin. 1.2 Overview of a Database Management System Data-Definition Language Commands –Illustrated by three examples.
© D. Wong Ch. 3 (part 1)  Relational Model basics  From E/R diagram to Relations.
Relational Algebra COMP3211 Advanced Databases Nicholas Gibbins
©Silberschatz, Korth and Sudarshan2.1Database System Concepts - 6 th Edition Chapter 8: Relational Algebra.
CSE202 Database Management Systems
Chapter (6) The Relational Algebra and Relational Calculus Objectives
Basic Operations Algebra of Bags
The Relational Algebra and Calculus
COMP3017 Advanced Databases
CS4432: Database Systems II
COP4710 Database Systems Relational Algebra.
Querying Relational Databases
An Algebraic Query Language
Chapter 2: Intro to Relational Model
Querying Relational Databases
The Relational Algebra and Relational Calculus
More Relation Operations
Basic Operations Algebra of Bags
Chapter 2: Intro to Relational Model
Chapter 2: Intro to Relational Model
Presentation transcript:

Operations in the Relational Model COP 4720 Lecture 8 Lecture Notes

Lecture 8COP Outline of Lecture Relational Algebra –Set Operations –Projection –Selection –Cartesian Product –Join –Combining Operations Completeness of Relational Algebra Operations Sec. 3.2

Lecture 8COP Database Query Languages Given a database, ask questions, get data as answers –Ex: Get all students with GPA > 3.7 who applied to Berkeley and Stanford and nowhere else –Ex: Get all humanities departments at campuses in Florida with < 1000 applicants –Ex: Get the campus with highest average accept rate over the last five years Some questions are easy to pose, some are not Some questions are easy for DBMS to answer, some are not. "Query language" also used to update the database

Lecture 8COP Relational Query Languages Formal(pure): relational algebra, relational calculus Actual: SQL In all languages, a query is executed over a set of relations, get a relation as the result

Lecture 8COP Relational Algebra Construct new relations from old ones –Set of operators –Relations are operands Build progressively more complex expressions by applying operators to relations or to rel. algebra expressions (which are relations as well) Query is a relational algebra expression –First concrete example of a query language Four broad classes of operations –Set operations, selection operations, operations that combine data from two relations, rename operation

Lecture 8COP Sample Relational Schema Movies(title,year,length,filmType) MovieStars(name,address,gender,birthdate)

Lecture 8COP Relational Operators: Select Select or Restrict (unary)  (r) = {t | t  r and predicate(t)} is a conditional expression of the type that we are familiar with from conventional programming languages – –attribute in R –op  {=, ,, , …, AND, OR} Ex:  length  100 (Movies)

Lecture 8COP Pictorially A 1 A 2 A 3 … A n... i A 1 A 2 A 3 … A n... j, i  j  titleyearlengthfilmType Star Wars Mighty Ducks Wayne’s World color Movies result set Comparison with null values return a false condition

Lecture 8COP Relational Operators: Project Project (unary)  (r) is a list of attributes (columns) from R only Ex:  title, year, length (Movies) A 1 A 2 A 3 … A n... i A 1 A 2 … A k... i  n k, k<=n

Lecture 8COP Relational Operators: Rename Rename (unary)  New (Old) New is the name and schema of the relation to be renamed (Old) Ex:  MyMovies (Movies) Ex:  MyMovies(title,year,length,type) (Movies)

Lecture 8COP Set Operations Union (binary, commutative, associative) R  S Intersection (binary, commutative, associative) R  S Set Difference (binary) R - S –Set of elements in R but not in S –R-S  S-R !! R(A 1,A 2,…,A n ), S(B 1,B 2,…,B n ) must be union compatible –R and S are of the same degree –for each i, dom(A i ) = dom(B i )

Lecture 8COP Example nameaddressgenderbirthdate Carrie Fisher Mark Hamil 123 Maple St., Hollywood 456 Oak Rd., Brentwood F M 9/9/99 8/8/88 R nameaddressgenderbirthdate Carrie Fisher Harrison Ford 123 Maple St., Hollywood 789 Palm Dr., Beverly Hills F M 9/9/99 7/7/77 S

Lecture 8COP Sample Operations nameaddressgenderbirthdate Carrie Fisher123 Maple St., HollywoodF 9/9/99 R  S nameaddressgenderbirthdate Carrie Fisher Harrison Ford 123 Maple St., Hollywood 789 Palm Dr., Beverly Hills F M 9/9/99 7/7/77 R  S Mark Hamil456 Oak Rd., BrentwoodM 8/8/88 nameaddressgenderbirthdate R - S Mark Hamil456 Oak Rd., BrentwoodM 8/8/88

Lecture 8COP Cartesian Product Cartesian Product (binary, commutative, associative) R x S Sets of all pairs that can be formed by choosing the first element of the pair to be any element of R, the second any element of S Resulting schema may be ambiguous –Use R.A or S.A to remove ambiguity for an attribute that occurs in both schemas –Alternatively, use Rename operator and rename

Lecture 8COP Example AB BC D x AR.BS.BCD RS

Lecture 8COP Join Operations Natural Join (binary) R join S R S Match only those tuples from R and S that agree in whatever attributes are common to the schemas of R and S –If r and s from r(R) and s(S) are successfully paired, result is called a joined tuple

Lecture 8COP Example AB BC D join Resulting schema has attributes from R, either R or S (i.e., joining attribute(s)), and S Tuples that fail to pair with any tuple of the other relation are called dangling tuples ABCD RS

Lecture 8COP Join Operations Theta Join (binary) R join C S, where C is an arbitrary join condition –R  S  is an arbitrary join condition Step 1: take the product of R and S Step 2: Select from the product only those tuples that satisfy condition C As with the product operation, the schema for the result is the union of the schemas of R and S natural join - join based on common attributes R  S =   R x S

Lecture 8COP Example BC D join A<D AND U.B  V.B AB C UV V.BV.CDAU.BU.C

Lecture 8COP Combining Operations to Form Queries Form expressions of arbitrary complexity by applying operators either to –given relations or –to relations that are the result of applying one or more relational operators to relations Single relational expression, using parenthesis as necessary Apply only one operator at a time and create intermediate result relations

Lecture 8COP Complete Set of Relational Algebra Expressions select project union set difference cartesian product Any of the other operators can be expressed as a sequence of operations from this set

Lecture 8COP Sample Schema for Exercises Student(ID, name, address, GPA, SAT) Campus(location, enrollment, rank) Apply(ID, location, date, major, decision)

Lecture 8COP Sample Queries PROJECT _{date,decision} (Apply) Campus X Apply Find Names and addresses of all students with GPA > 3.7 who applied to CS major and were rejected List name and address of all students with GPA > 3.7