M.P. Johnson, DBMS, Stern/NYU, Sp20041 C20.0046: Database Management Systems Lecture #9 Matthew P. Johnson Stern School of Business, NYU Spring, 2004.

Slides:



Advertisements
Similar presentations
Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
Advertisements

Lecture 07: Relational Algebra
1 Relational Algebra. Motivation Write a Java program Translate it into a program in assembly language Execute the assembly language program As a rough.
Relational Algebra (end) SQL April 19 th, Complex Queries Product ( pid, name, price, category, maker-cid) Purchase (buyer-ssn, seller-ssn, store,
Algebraic and Logical Query Languages Spring 2011 Instructor: Hassan Khosravi.
Relational Algebra Maybe -- SQL. Confused by Normal Forms ? 3NF BCNF 4NF If a database doesn’t violate 4NF (BCNF) then it doesn’t violate BCNF (3NF) !
Relational Algebra.
Midterm Review II. Redundancy. –Information may be repeated unnecessarily in several tuples. –E.g. length and filmType. Update anomalies. –We may change.
Relational Operations on Bags Extended Operators of Relational Algebra.
Subqueries Example Find the name of the producer of ‘Star Wars’.
M.P. Johnson, DBMS, Stern/NYU, Sp20041 C : Database Management Systems Lecture #8 Matthew P. Johnson Stern School of Business, NYU Spring, 2004.
Oct 28, 2003Murali Mani Relational Algebra B term 2004: lecture 10, 11.
Relational Algebra on Bags A bag is like a set, but an element may appear more than once. –Multiset is another name for “bag.” Example: {1,2,1,3} is a.
M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #11 M.P. Johnson Stern School of Business, NYU Spring, 2008.
M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #10 Matthew P. Johnson Stern School of Business, NYU Spring,
M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #6 M.P. Johnson Stern School of Business, NYU Spring, 2008.
Matthew P. Johnson, OCL3, CISDD CUNY, June OCL3 Oracle 10g: SQL & PL/SQL Session #4 Matthew P. Johnson CISDD, CUNY June, 2005.
M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #12 M.P. Johnson Stern School of Business, NYU Spring, 2005.
M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #9 Matthew P. Johnson Stern School of Business, NYU Spring, 2005.
M.P. Johnson, DBMS, Stern/NYU, Sp20041 C : Database Management Systems Lecture #10 Matthew P. Johnson Stern School of Business, NYU Spring, 2004.
Operations in the Relational Model These operation can be expressed in an algebra, called “relational algebra”. In this algebra relations are the operands.
Relational Operations on Bags Extended Operators of Relational Algebra.
Relational Algebra Chapter 4 - part I. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.  Relational.
Joins Natural join is obtained by: R NATURAL JOIN S; Example SELECT * FROM MovieStar NATURAL JOIN MovieExec; Theta join is obtained by: R JOIN S ON Example.
1 Lecture 07: Relational Algebra. 2 Outline Relational Algebra (Section 6.1)
M.P. Johnson, DBMS, Stern/NYU, Spring Complex RA Expressions Scenario: 1. Purchase(pid, seller-ssn, buyer-ssn, etc.) 2. Person(ssn, name, etc.)
Chapter 5 Algebraic and Logical Query Languages pp.54 is added Pp 61 updated.
Instructor: Mohamed Eltabakh
M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #11 M.P. Johnson Stern School of Business, NYU Spring, 2005.
M.P. Johnson, DBMS, Stern/NYU, Sp20041 C : Database Management Systems Lecture #6 Matthew P. Johnson Stern School of Business, NYU Spring, 2004.
Nov 18, 2003Murali Mani Relational Algebra B term 2004: lecture 10, 11.
Murali Mani Relational Algebra. Murali Mani What is Relational Algebra? Defines operations (data retrieval) for relational model SQL’s DML (Data Manipulation.
Relational Schema Design (end) Relational Algebra Finally, querying the database!
One More Normal Form Consider the dependencies: Product Company Company, State Product Is it in BCNF?
Relation Decomposition A, A, … A 12n Given a relation R with attributes Create two relations R1 and R2 with attributes B, B, … B 12m C, C, … C 12l Such.
Databases 1 Seventh lecture. Topics of the lecture Extended relational algebra Normalization Normal forms 2.
1 Database Systems Lecture #7 Yan Pan School of Software, SYSU 2011.
1 Introduction to Database Systems CSE 444 Lecture 20: Query Execution: Relational Algebra May 21, 2008.
Relational Algebra (Chapter 7)
From Professor Ullman, Relational Algebra.
Transactions, Relational Algebra, XML February 11 th, 2004.
CSE 544: Relational Operators, Sorting Wednesday, 5/12/2004.
1 Lecture 7: Normal Forms, Relational Algebra Monday, 10/15/2001.
Relational Algebra 2. Relational Algebra Formalism for creating new relations from existing ones Its place in the big picture: Declartive query language.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
© D. Wong Normalization  Purpose: process to eliminate redundancy in relations due to functional or multi-valued dependencies.  Decompose relation.
Databases : Relational Algebra - Complex Expression 2007, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof.
More Relation Operations 2015, Fall Pusan National University Ki-Joune Li.
More Relation Operations 2014, Fall Pusan National University Ki-Joune Li.
1 CS 430 Database Theory Winter 2005 Lecture 5: Relational Algebra.
Lecture 13: Relational Decomposition and Relational Algebra February 5 th, 2003.
1 Lecture 10: Database Design and Relational Algebra Monday, October 20, 2003.
Lu Chaojun, SJTU 1 Extended Relational Algebra. Bag Semantics A relation (in SQL, at least) is really a bag (or multiset). –It may contain the same tuple.
Subqueries CIS 4301 Lecture Notes Lecture /23/2006.
Summary of Relational Algebra
Relational Algebra.
Relational Algebra at a Glance
Lecture 8: Relational Algebra
Operators Expression Trees Bag Model of Data
The Relational Algebra and Relational Calculus
Relational Algebra Chapter 4 - part I.
Instructor: Mohamed Eltabakh
More Relation Operations
Lecture 33: The Relational Model 2
Algebraic and Logical Query Languages pp.54 is added
Relational Algebra Friday, 11/14/2003.
5.1 Relational Operations on Bags
Relational Algebra Chapter 4 - part I.
Lecture 11: Functional Dependencies
Presentation transcript:

M.P. Johnson, DBMS, Stern/NYU, Sp20041 C : Database Management Systems Lecture #9 Matthew P. Johnson Stern School of Business, NYU Spring, 2004

M.P. Johnson, DBMS, Stern/NYU, Sp Agenda Last time: 4NF, RA This time: 1. More RA 2. Bags Project Part 2 due next time

M.P. Johnson, DBMS, Stern/NYU, Sp Normalization Review Q: What’s required for BCNF? Q: What’s the loophole for 3NF? Q: How do we fix a non-BCNF, non-3NF, non-4NF relations? Q: When are are FDs also MVDs? Q: When are MVDs also FDs?

M.P. Johnson, DBMS, Stern/NYU, Sp Relational Algebra Review Five basic operators:  Union:   Intersection:  Difference: -  Selection:   Projection:   Cartesian Product:  Extended operators:  Joins (natural, equijoin, theta join, semijoin)  Renaming:   Extended projection

M.P. Johnson, DBMS, Stern/NYU, Sp Renaming Changes the schema, not the instance Notation:  B1,…,Bn (R)  is spelled “rho”, pronounced “row” Example:  Employee(ssn,name)    social, name) (Employee)  Or just:   (Employee)

M.P. Johnson, DBMS, Stern/NYU, Sp Complex RA Expressions Q: How long was Star Wars (1977)? Strategy: find the row with Star Wars; then project the length field TitleYearLengthinColorStudioPrdcr# Star Wars TrueFox12345 M.Ducks TrueDisney67890 W.World199295TrueParamount99999

M.P. Johnson, DBMS, Stern/NYU, Sp Combining operations Q: Which Fox movies are at least 100 minutes long? TitleYearLengthFilmtypeStudio Star wars ColorFox Mighty ducks ColorDisney Wayne’s world199285ColorParamount

M.P. Johnson, DBMS, Stern/NYU, Sp Complex RA Expressions Reps(ssn, name, etc.) Clients(ssn, name, rssn) Q: Who are George’s clients?  Clients.name (  Reps.name=George (  Reps.ssn=rssn ( Reps x Clients))) Or:  Clients.name (  Reps.name=George and Reps.ssn=rssn (Reps x Clients)) Or:  Clients.name (  Reps.name=George (Reps x Clients)   Reps.ssn=rssn (Reps x Clients))

M.P. Johnson, DBMS, Stern/NYU, Sp Complex RA Expressions People(ssn, name, street, city, state)  assume for clarity that cities are unique Q: Who lives on George’s street? A: First, find George:   name=“George” (People) Get George’s street/city:   street,city (  name=“George” (People)) Cross with People:  People x  street,city (  name=“George” (People))

M.P. Johnson, DBMS, Stern/NYU, Sp Complex RA Expressions How to specify street = street? Rename   p2(s2,c2) (People) x  street,city (  name=“George” (People)) Now can select:  street=s2 AND city=c2 (  p2(s2,c2) (People) x  street,city (  name=“George” (People))) Then project names… Only way? No. Join!  People   street,city (  name=“George” (People)) Q: Would the following work?   street,city (  name=“George” (People  People))

M.P. Johnson, DBMS, Stern/NYU, Sp Complex RA Expressions Scenario: 1. Purchase(pid, seller-ssn, buyer-ssn, etc.) 2. Person(ssn, name, etc.) 3. Product(pid, name, etc.) Q: Who (give names) bought gizmos from Dick? Where to start? Purchase uses pid, ssn, so must get them…

M.P. Johnson, DBMS, Stern/NYU, Sp Complex RA Expressions Person Purchase Person Product  name=“Dick”  name=“Gizmo”  pid  ssn seller-ssn=ssnpid=pidbuyer-ssn=Person.ssn  name

M.P. Johnson, DBMS, Stern/NYU, Sp Complex RA Expressions Acc(name,ssn,balance) Q: Who has the largest balance? First, get two copies of rel to play with: Acc x  a2 (Acc) Now, consider this:  a2.bal < Acc.bal (Acc x  a2 (Acc)) Q: What does it give us? Now, subtract the names:  name (Acc) -  a2.name (  a2.bal < Acc.bal (Acc x  a2 (Acc)))

M.P. Johnson, DBMS, Stern/NYU, Sp Confession Relations aren’t really sets! They’re bags!

M.P. Johnson, DBMS, Stern/NYU, Sp Bag Theory (5.3) Bags: like sets but elements may repeat  “multisets” Set ops change somewhat when applied to bags  intuition: pretend identical elements are distinct {a,b,b,c}  {a,b,b,b,e,f,f} = {a,a,b,b,b,b,b,c,e,f,f} {a,b,b,b,c,c} – {b,c,c,c,d} = {a,b,b} {a,b,b,b,c,c}  {b,c,c,c,d} = {b,c,c} Reading assignment: 5.3 – 5.4

M.P. Johnson, DBMS, Stern/NYU, Sp Bag theory  C (R): preserve the number of occurrences  A (R), product, join: no duplicate elimination  |R1xR2| = |R1|*|R2| Can convert to sets when necessary Why not sets?  Too expensive: Union Projection updates…  consider: average(  bal (Acc))

M.P. Johnson, DBMS, Stern/NYU, Sp Some surprises in bag theory Be careful about your set theory laws – not all hold in bag theory (R  S) – T = (R – T)  (S – T)  always true in set theory  But true in bag theory?  suppose t is in R, S and T

M.P. Johnson, DBMS, Stern/NYU, Sp Finally: RA has limitations Cannot compute “transitive closure” Find all direct and indirect relatives of Fred Cannot express in RA! RA is not Turing-Complete Name1Name2Relationship FredMaryFather MaryJoeCousin MaryBillSpouse NancyLouSister

M.P. Johnson, DBMS, Stern/NYU, Sp Extended Operators (5.4) Duplicate-eliminator  Lower-case delta  Convert to set Aggregation operators  Compute functions of tuples  Sum, average, etc. Grouping-and-aggregation op  lwr-case gamma  Partition tuples into groups, then compute function Sorting  Lower-case tau Extended projection  Project onto new, computed columns Outerjoin  Include dangling duples by nulling

M.P. Johnson, DBMS, Stern/NYU, Sp Duplicate elimination AB RAB  (R)

M.P. Johnson, DBMS, Stern/NYU, Sp Aggregation operators Numerical: SUM, AVG, MIN, MAX Char: MIN, MAX  In lexocographic/alphabetic order Any attribute: COUNT  Number of values SUM(B) = 10 AVG(A) = 1.5 MIN(A) = 1 MAX(A) = 4 COUNT(A) = 4 AB R

M.P. Johnson, DBMS, Stern/NYU, Sp Grouping Motivation: Movie(title, year, length, studioName) Q: How many minutes of film have been produced by each studio? Strategy: Divide movies into groups per studio, then add lengths Our expression:   Studio,SUM(length)  total (Movies) The subscript: list of attributes and aggregations  Movies is grouped by these attributes  Result includes both

M.P. Johnson, DBMS, Stern/NYU, Sp Grouping example  Studio,SUM(length)  total (Movies) TitleYearLengthStudio Star Wars Fox Jedi Fox M.Ducks Disney Lion King Disney W.World199295Paramount

M.P. Johnson, DBMS, Stern/NYU, Sp Grouping example  Studio,SUM(length)  total (Movies) TitleYearLengthStudio Star Wars Fox Jedi Fox M.Ducks Disney Lion King Disney W.World199295Paramount StudioLength Fox225 Disney220 Paramount95

M.P. Johnson, DBMS, Stern/NYU, Sp Extended projection Form:  a  b, a+b  c, a||b  d (R) a  b rename attribute a as b a+b  c create att c as sum of a and b a||b  d create att d as concatenation of a, b Example:  firstname||“ ”||lastname  fullname (R)  Replace firstname and lastname fields with a fullname field

M.P. Johnson, DBMS, Stern/NYU, Sp Grouping/extended projection example StarsIn(SName,Title,Year) Q: Find the year of each star’s first movie  SName,MIN(year)  firstYear (StarsIn) How about Q: Find the span of each star’s career A idea: get max and min and subtract A:  SName,MIN(year)  firstY,MIN(year)  lastY (StarsIn) Now project onto diff:  SName,lastY-firstY+1  span (  SName,MIN(year)  firstY,MIN(year)  lastY (StarsIn))