The Relational Model Part III. Remember: 3 Aspects of the Model It concerns 1) data objects storing it 2) data integrity making sure it corresponds to.

Slides:



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

The Relational Model and Relational Algebra Nothing is so practical as a good theory Kurt Lewin, 1945.
Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
D ATABASE S YSTEMS I R ELATIONAL A LGEBRA. 22 R ELATIONAL Q UERY L ANGUAGES Query languages (QL): Allow manipulation and retrieval of data from a database.
Concepts of Database Management Sixth Edition
Concepts of Database Management Seventh Edition
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4, Part A Modified by Donghui Zhang.
1 Relational Algebra & Calculus. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.  Relational.
Relational Algebra 1 Chapter 5.1 V3.0 Napier University Dr Gordon Russell.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 52 Database Systems I Relational Algebra.
Database Systems Chapter 6 ITM Relational Algebra The basic set of operations for the relational model is the relational algebra. –enable the specification.
Lecture 4: Relational algebra
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 8 Advanced SQL.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
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.
1 Relational Algebra and Calculus Yanlei Diao UMass Amherst Feb 1, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke.
Rutgers University Relational Algebra 198:541 Rutgers University.
Relational Algebra Chapter 4 - part I. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.  Relational.
Databases Illuminated
Concepts of Database Management, Fifth Edition
Chapter 3 Section 3.4 Relational Database Operators
1 Relational Algebra and Calculus Chapter 4. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.
Copyright © Curt Hill The Relational Algebra What operations can be done?
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.
1 The Relational Database Model. 2 Learning Objectives Terminology of relational model. How tables are used to represent data. Connection between mathematical.
Concepts of Database Management Seventh Edition
Chapter 7 Relational Algebra. Topics in this Chapter Closure Revisited The Original Algebra: Syntax and Semantics What is the Algebra For? Further Points.
NULLs & Outer Joins Objectives of the Lecture : To consider the use of NULLs in SQL. To consider Outer Join Operations, and their implementation in SQL.
Chapter 7 Relational Algebra. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.7-2 Topics in this Chapter Closure Revisited The Original Algebra:
1 Relational Algebra & Calculus Chapter 4, Part A (Relational Algebra)
1 Relational Algebra and Calculas Chapter 4, Part A.
1.1 CAS CS 460/660 Introduction to Database Systems Relational Algebra.
CS424 Relational Data Manipulation Relational Data Manipulation Relational tables are sets. Relational tables are sets. The rows of the tables can be considered.
1 Relational Algebra Chapter 4, Sections 4.1 – 4.2.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Management Systems Chapter 4 Relational Algebra.
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.
Advanced Relational Algebra & SQL (Part1 )
Database Management Systems, R. Ramakrishnan1 Relational Algebra Module 3, Lecture 1.
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.
Query Processing – Implementing Set Operations and Joins Chap. 19.
Manipulating Data Lesson 3. Objectives Queries The SELECT query to retrieve or extract data from one table, how to retrieve or extract data by using.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
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.
SQL and Relational Algebra Edel Sherratt Nigel Hardy Horst Holstein.
Ritu CHaturvedi Some figures are adapted from T. COnnolly
CS4432: Database Systems II
Relational Algebra Chapter 4 1.
Database Systems: Design, Implementation, and Management Tenth Edition
Lecture 2 The Relational Model
Relational Algebra 461 The slides for this text are organized into chapters. This lecture covers relational algebra, from Chapter 4. The relational calculus.
Relational Algebra.
LECTURE 3: Relational Algebra
Relational Algebra Chapter 4 1.
The Relational Algebra and Relational Calculus
Relational Algebra Chapter 4, Sections 4.1 – 4.2
Database Systems: Design, Implementation, and Management Tenth Edition
CENG 351 File Structures and Data Managemnet
Relational Algebra & Calculus
Unit Relational Algebra 1
Presentation transcript:

The Relational Model Part III

Remember: 3 Aspects of the Model It concerns 1) data objects storing it 2) data integrity making sure it corresponds to reality 3) data manipulation working with it

Manipulating Data The theory in the model

Relational Algebra A set of operators Take relations as operands c.f. arithmetic operators returns 4 R1 op R2 returns R3

Relational Closure The output of a relational operation is a relation Put simply, we always work with tables The output of one operation can be the input to the next we are familiar with the concept in arithmetic 2 + (4 × 3) - (28/4) The great trick!

Operators Any number could be defined 8 originals 4 traditional set operations (modified) union, intersection, difference, Cartesian product 4 special relational operations restrict, project, join and divide We will also look at 2 more extend, summarize

Type Compatibility Having the same set of attributes with corresponding attributes defined on the same domains Some operators require this some do not adding apples and oranges

The 4 set operators 1.Union 2.Intersection 3.Difference = Minus 4.(Cartesian) Product = Times

Union operator (Requires type compatibility) A UNION B returns a relation with: the same heading as A or B the set of all tuples in A or B or both

Union NameJobPosting GordonAccountantLondon GeorgeSalesmanWashington VladimirSecurityMoscow Duplicates eliminated NameJobPosting GordonAccountantLondon GeorgeSalesmanWashington NameJobPosting GordonAccountantLondon VladimirSecurityMoscow UNION RETURNS

Intersection operator (Requires type compatibility) A INTERSECTION B returns a relation with: the same heading as A or B the set of all tuples belonging to both A and B

Intersection NameJobPosting GordonAccountantLondon NameJobPosting GordonAccountantLondon GeorgeSalesmanWashington NameJobPosting GordonAccountantLondon VladimirSecurityMoscow INTERSECTION RETURNS

Difference operator (Requires type compatibility) A DIFFERENCE B returns a relation with: the same heading as A or B the set of all tuples belonging to A and not to B

Difference NameJobPosting GeorgeSalesmanWashington Directionality NameJobPosting GordonAccountantLondon GeorgeSalesmanWashington NameJobPosting GordonAccountantLondon VladimirSecurityMoscow DIFFERENCE RETURNS

Product operator (Does not require type compatibility) A PRODUCT B returns a relation with: a heading which is the union of the headings of A and B the set of tuples formed by coalescing all tuples from A with all tuples from B – all permutations Not typically of practical use No extra information Theoretical value

Product C A B PRODUCT RETURNS N CN A1 A2 A3 B1 B2 B3

Product operator - note If the headers have names in common product would have duplicated attributes not a well formed relation must rename one or both R1 (a, b, c) Product R2 (c, d, e) might be made to return R3 (a, b, c1, c2, d, e) or R3 (a, b, R1.c, R2.c, d, e)

Operator Ordering Associative Union, Intersection, Product but not Difference Commutative: Union, Intersection, Product but not Difference Equivalent: (A Union B) Union C A Union (B Union C) A Union B Union C Equivalent: A Union B B Union A

The 4 relational operators 1.Restrict 2.Project 3.Join 4.Divide

The Restrict Operation Based on: one relation scalar operator Θ Θ could be, >=, > etc. two attributes Often represented by the word where One attribute can be replaced by an expression Examples A where X Θ Y B where r > s C where length < 42 Selects tuples Removes rows

RESTRICT NameJobPosting GeorgeSalesmanWashington people WHERE job = ‘Salesman’ NameJobPosting GordonAccountantLondon GeorgeSalesmanWashington RETURNS

Restrict Conditions (and/or) A where C1 and C2 ≡ (A where C1) INTERSECTION (A where C2) A where C1 or C2 ≡ (A where C1) UNION (A where C2) A where not C ≡ A DIFFERENCE (A where C) We can extend the WHERE clause with any arbitrary Boolean combination of comparisons People WHERE height 50

Project Removes “columns” (attributes) Written as: A [X, Y] returns a relation with two named attributes Duplicate tuples eliminated if the lost attributes distinguished them All attributes named - identity projection No attributes named - nullary projection

Join The output relation from A JOIN B has: a heading consisting of: attributes found only in A attributes found only in B attributes found in both A and B (1 copy) tuples where values of identified attributes are the same in A and B Associative and commutative Sometimes called the natural join

JOIN WeightColourLength Very heavyRedVery short Very heavyRedShort HeavyRedVery short HeavyRedShort LightYellowVery long WeightColour Very lightBlue Very heavyRed HeavyRed LightYellow ColourLength GreenLong RedVery short RedShort YellowVery long JOIN RETURNS

Θ -Join Join is based on equality Θ -join is based on any condition (A PRODUCT B) where X Θ Y if Θ is = we have an equijoin X and Y attributes same in all tuples eliminate one with projection -we have join Join is a projection of a restriction of a product Crucial to understand and appreciate this

The PRODUCT Table3 weight colou r verylightblue veryheavyred heavyred lightyellow colourlength greenlong redveryshort redshort yellowverylong weightTable3.colourTable4.colourlength verylightbluegreenlong veryheavyredgreenlong heavyredgreenlong lightyellowgreenlong verylightblueredveryshort veryheavyred veryshort heavyred veryshort lightyellowredveryshort verylightblueredshort veryheavyred short heavyred short lightyellowredshort verylightblueyellowverylong veryheavyredyellowverylong heavyredyellowverylong lightyellow verylong PRODUCT How many tuples? 4 x 4 = 16

Alphabetical Less Than Join Table3 weightcolour verylightblue veryheavyred heavyred lightyellow colourlength greenlong redveryshort redshort yellowverylong weightTable3.colourTable4.colourlength verylightbluegreenlong verylightblueredveryshort verylightblueredshort verylightblueyellowverylong veryheavyredyellowverylong heavyredyellowverylong A < B

Directional Joins a heading consisting of: attributes found only in A attributes found only in B attributes found in both A and B (1 copy) all the tuples from one relation only matching tuples from the other Left Join or Right Join will result in blanks

Left-Join Table3 weightcolour verylightblue veryheavyred heavyred lightyellow colourlength greenlong redveryshort redshort yellowverylong Left Join weightcolourlength verylightblue veryheavyredshort veryheavyredveryshort heavyredshort heavyredveryshort lightyellowverylong

Division given A{X, Y } and B{Y } division returns a relation with heading X tuples for which A has an {X, Y } for all Y in B X and/or Y can be multiple attributes

Division Person Jim PersonSport JimSoccer PaulRugby MaryTennis PaulTennis MarySquash JimTennis SallySoccer Sport Soccer Tennis DIVIDE RETURNS

2 additional operators Others have been proposed and still are These 2 have widespread value and are illustrative extend summarize

Extend Adds a new attribute calculated from one or more existing attributes EXTEND relation ADD expression AS ATTRIBUTE EXTEND item ADD (cost. 2.58) AS dollar the expression can involve constants, attributes and other relations

Summarize Column-wise computations - grouping c.f. row-wise in Extend e.g. SUMMARIZE R by A1 add sum A2 as Total Return a relation with heading {A1, Total} a tuple for each distinct value of A1 in R containing the total of A2 values over them

Summarize - notes Can be “by” more than one attribute projection plus one attribute Can be “by” no attribute grand total (or other calculation)

Relation assignment? So far it has all been expressions need a syntax for storing the result in named relations The existing heading and tuples in a relation will be “overwritten” e.g. A = B UNION C X = X UNION Y c.f. arithmetic Not done like this Rarely store “answers” We change tables

Updating relations Could use assignment with destination relation in the expression error conditions not then handled addition of duplicate tuple deletion of non-existent tuple not efficient not declarative Specific update operations handle this: insert update delete

Insert Source and target relations must be type compatible All tuples of source inserted into target set operation Source and target can be expressions insert(A where x > 1 or y = 42) into B

Update Change specified attribute values in specified tuples of a relation expression to identify the restriction of a relation assignments to set attributes update (A where model = delux) colour = red trim = gold set of tuples changed may be set of 1

Delete Identified tuples from a relation again, a set of tuples DELETE A where length > 42

What is the algebra for? Retrieval: as expected Views: virtual relations (stored queries) Update: what parts change Security: define data under particular authorisation control Concurrency control: data to be protected Integrity rules: some parts of the data which must obey certain rules

Data Manipulation End