© 2003-2006 M.E. Fayad SJSU -- CmpE Database Design Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering San José.

Slides:



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

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.
Relational Algebra Dashiell Fryer. What is Relational Algebra? Relational algebra is a procedural query language. Relational algebra is a procedural query.
L8-S1 Datalog Queries 2003 SJSU -- CmpE Database Design Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering San.
1 3. Relational Algebra and SQL Example: Let the following relations describe point sets: A(x, y), B(x, y), J(x, y) 2D points in the plane H(x, y, z),
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4, Part A Modified by Donghui Zhang.
INFS614, Fall 08 1 Relational Algebra Lecture 4. INFS614, Fall 08 2 Relational Query Languages v Query languages: Allow manipulation and retrieval of.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 52 Database Systems I Relational Algebra.
L3-5c-S1 Object Diagrams © M.E. Fayad SJSU -- CmpE Software System Engineering Dr. M.E. Fayad, Professor Computer Engineering Department,
© M.E. Fayad SJSU -- CmpE Database Design Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering San José.
Database Systems Chapter 6 ITM Relational Algebra The basic set of operations for the relational model is the relational algebra. –enable the specification.
L16-S1 Object Diagrams 2003 SJSU -- CmpE Software Patterns Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering.
L28-S1 Interaction Diagrams 2003 SJSU -- CmpE Advanced Object-Oriented Analysis & Design Dr. M.E. Fayad, Professor Computer Engineering Department, Room.
L19-S1 More on Class Diagrams 2003 SJSU -- CmpE Object-Oriented Analysis & Design Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I.
1 Relational Algebra. 2 Relational Query Languages Query languages: Allow manipulation and retrieval of data from a database. Relational model supports.
L3-5d-S1 Class Diagrams © M.E. Fayad SJSU -- CmpE Software System Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room.
L26-S1 Interactions 2003 SJSU -- CmpE Advanced Object-Oriented Analysis & Design Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4, Part A.
P2-S1 MySQL 2003 SJSU -- CmpE-- M.E. Fayad Database Design Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering.
Relational Algebra Chapter 4 - part I. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.  Relational.
© M.E. Fayad SJSU -- CmpE Database Design Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering San José.
SQL SQL stands for Structured Query Language SQL allows you to access a database SQL is an ANSI standard computer language SQL can execute queries against.
L5-S1 Class Diagrams 2003 SJSU -- CmpE Software Patterns Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering San.
© M.E. Fayad SJSU -- CmpE Database Design Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering San José.
© M.E. Fayad SJSU -- CmpE Software System Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering.
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.
© M.E. Fayad SJSU -- CmpE Software System Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering.
L3-6b-S1 Sequence Diagrams © M.E. Fayad SJSU -- CmpE Software System Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room.
Chapter 11.1 and 11.2 Data Manipulation: Relational Algebra and SQL Brian Cobarrubia Introduction to Database Management Systems October 4, 2007.
1 Relational Algebra and Calculus Chapter 4. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.
Lecture 05 Structured Query Language. 2 Father of Relational Model Edgar F. Codd ( ) PhD from U. of Michigan, Ann Arbor Received Turing Award.
Relational Algebra - Chapter (7th ed )
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Relational Algebra.
ICS 321 Fall 2011 The Relational Model of Data (i) Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 8/29/20111Lipyeow.
The Relational Algebra and Calculus
1 Relational Algebra Chapter 4, Sections 4.1 – 4.2.
Advanced Relational Algebra & SQL (Part1 )
CSCD34-Data Management Systems - A. Vaisman1 Relational Algebra.
L8-2-S1 Misc Topics © M.E. Fayad SJSU -- CmpE Database Design Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of.
IST 210 The Relational Language Todd S. Bacastow January 2004.
Database Management Systems, R. Ramakrishnan1 Relational Algebra Module 3, Lecture 1.
1 Algebra of Queries Classical Relational Algebra It is a collection of operations on relations. Each operation takes one or two relations as its operand(s)
CMPT 258 Database Systems Relational Algebra (Chapter 4)
L6-S1 MLPQ 2003 SJSU -- CmpE-- M.E. Fayad Database Design Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering.
L1-S1 Infinite R-DB 2003 SJSU -- CmpE Database Design Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering San.
L12-S1 Spatiotmporal DB 2003 SJSU -- CmpE Database Design Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering.
Relational Algebra. CENG 3512 Relational Query Languages Query languages: Allow manipulation and retrieval of data from a database. Relational model supports.
Database Systems Chapter 6
Fundamental of Database Systems
Relational Algebra Chapter 4 1.
Database Design Dr. M.E. Fayad, Professor
Chapter 2: Intro to 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.
Software System Engineering
Relational Algebra 1.
Object-Oriented Analysis & Design
LECTURE 3: Relational Algebra
Relational Algebra Chapter 4 1.
Relational Algebra Chapter 4 - part I.
Instructor: Mohamed Eltabakh
Chapter 2: Intro to Relational Model
Database Design Dr. M.E. Fayad, Professor
CENG 351 File Structures and Data Managemnet
Database Design Dr. M.E. Fayad, Professor
Database Design Dr. M.E. Fayad, Professor
Software System Engineering
Relational Algebra Chapter 4 - part I.
Presentation transcript:

© M.E. Fayad SJSU -- CmpE Database Design Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering San José State University One Washington Square San José, CA

L6-S2 RA & SQL © M.E. Fayad SJSU – CmpE M.E. Fayad 2 Lesson 06: Relational Algebra and SQL

L6-S3 RA & SQL © M.E. Fayad SJSU – CmpE M.E. Fayad Lesson Objectives 3 Understand Two Different Query Languages: Relational Algebra and SQL Learn how to deal with: î Relational Algebra Operators î Relational Algebra Queries î The Benefits of Query Composition îIllustrated Examples Explore SQL with examples

L6-S4 RA & SQL © M.E. Fayad SJSU – CmpE M.E. Fayad  Relational Algebra  Has a small set of well-defined operators that can be composed to form query expressions.  It is a procedural language, because the sequence of operators and the operators themselves can be evaluated using well-defined procedures  Relational Algebra is a useful theoretical language that serves to define more complex languages. 4 Two Different Query Languages for Relational Database (1)

L6-S5 RA & SQL © M.E. Fayad SJSU – CmpE M.E. Fayad  Structured Query Language (SQL)  It is a practical language that allows a high level expression of queries.  A user of SQL does not need to think procedurally about queries  But can rely on the meaning of higher-level keywords provided by SQL.  However, most SQL queries can be translated to relational algebra queries to understand the precise meaning of a SQL query. 5 Two Different Query Languages for Relational Database (2)

L6-S6 RA & SQL © M.E. Fayad SJSU – CmpE M.E. Fayad  Relational Algebra is a query language composed of a number of operators.  Each take relations as arguments and return a single relation as result. 6 Relational Algebra Operators

L6-S7 RA & SQL © M.E. Fayad SJSU – CmpE M.E. Fayad  The intersection of two relations A and B, denoted A  B  Results: the set of points belongs to both A and B  The intersection operator can be applied only to operands that have the same set and order of attributes. 7 Intersection

L6-S8 RA & SQL © M.E. Fayad SJSU – CmpE M.E. Fayad  The union of two relations A and B, denoted A  B  Results: the set of points belongs to A or B or both.  The union operator can be applied only to operands that have the same set and order of attributes. 8 Union

L6-S9 RA & SQL © M.E. Fayad SJSU – CmpE M.E. Fayad  The difference of two relations, denoted A \ B  Results: the set of points belongs to A but do not belongs to B.  The difference operator can be applied only to operands that have the same set and order of attributes. 9 Difference

L6-S10 RA & SQL © M.E. Fayad SJSU – CmpE M.E. Fayad  The product operator applied to an n-dimensional relation A and an m-dimensional relation B, denoted A x B  Results: a relation that contains all (n + m) dimensional points whose first n components belong to A and last m components belong to B.  The product operator can be applied only to operands that have no common attributes. 10 Product

L6-S11 RA & SQL © M.E. Fayad SJSU – CmpE M.E. Fayad  The project is used to reorder the columns of a relation or to eliminate some columns of a relation.  The project operator from a relation A is denoted π L A, where L is a list of [l 1, ….., l k ] that specifies an ordering of a subset of the attributes of A.  The project operator creates a new relation that contains in its ith column the column of A corresponding to attribute l i. 11 Project

L6-S12 RA & SQL © M.E. Fayad SJSU – CmpE M.E. Fayad  The select operator is used to select a relation A those points that satisfy a certain logical formula F.  The select operator has the form σ F A. The logical formula F is a constraint formula.  Constraint formula – Chapter 2 12 Select

L6-S13 RA & SQL © M.E. Fayad SJSU – CmpE M.E. Fayad  The rename operator ρB(X1/Y1, …., Xn/Yn) A for any n ≥ 1, changes the name of relation A to B and changes the attribute X i to Y i.  If we only want to change the name of the relation, then the form ρB A can be used. 13 Rename

L6-S14 RA & SQL © M.E. Fayad SJSU – CmpE M.E. Fayad  The natural join operator applied to an n-dimensional relation A and an m-dimensional relation B that have k attributes in common is denoted A B.  The natural join operator returns a relation that contains all (n + m – k)-dimensional points whose projection onto attributes of A belong to A and whose projection onto the attributes of B belong to B. 14 Natural Join

L6-S15 RA & SQL © M.E. Fayad SJSU – CmpE M.E. Fayad 15 Which Operators can be applied? A and B  Two sets of points: A(x, y) and B (x, y) are 2-dimensional  Operators that can be applied:  Intersection  Union  Difference

L6-S16 RA & SQL © M.E. Fayad SJSU – CmpE M.E. Fayad Example: Let the following relations describe point sets: A(x, y), B(x, y), J(x, y) 2D points in the plane H(x, y, z), I(x, y, z) 3D points in space F(z) line on the z-axis K(y,z) 2D points in (y, z) plane 16 How to read set of points

L6-S17 RA & SQL © M.E. Fayad SJSU – CmpE M.E. Fayad 5 Relation algebra operators (1) A  B = C A  B = D A \ B = E A and B

L6-S18 RA & SQL © M.E. Fayad SJSU – CmpE M.E. Fayad Relation algebra operators (2) A  F  Let F(z) be a relation that contains all points on the z-axis between z = 5 and z = 15.  Then the product A(x, y) X F(z) = G(x, y, z), where G is the polyhedron, as shown in the figure.

L6-S19 RA & SQL © M.E. Fayad SJSU – CmpE M.E. Fayad Relation algebra operators (3)  x,y H  y,z H  Consider H(x, y, z) is the polyhedron in the shown figure.  The projection  x,y H is the shaded rectangle in the (x, y) plane, while  y,z H is the shaded rectangle in (y, z) plane. Projection from H

L6-S20 RA & SQL © M.E. Fayad SJSU – CmpE M.E. Fayad Relation algebra operators (4)  2x+z = 0 I  Consider the rectangular body l(x, y, z), shown in the figure.  The shaded rectangle that lies in the plane defined by the equation 2x + z = 0 cuts l into two.  The selection operation  2x+z ≤0 I returns the part of l that lies below the shaded rectangle

L6-S21 RA & SQL © M.E. Fayad SJSU – CmpE M.E. Fayad Relation algebra operators (5) J K  The natural join of the shaded rectangle J(x, y) and the shaded circle K(y, z) is the cylinder L(x, y, z).  This can be written as J(x, y) K(y, z) = L(x, y, z)

L6-S22 RA & SQL © M.E. Fayad SJSU – CmpE M.E. Fayad  Because the result of each operation is a relation, the operators can be composed to form queries  The relational algebra query language is defined recursively as following: 22 Relational Algebra Queries (1)

L6-S23 RA & SQL © M.E. Fayad SJSU – CmpE M.E. Fayad 1. Each relation name R i is a relational algebra expression. (This will just return the relation R i as a result.) 2. If e 1 and e 2 are relational algebra expressions that result in relations that have the same set and order of attributes, then (e 1  e 2 ), (e 1  e 2 ), (e 1 \ e 2 ) are also relational algebra expressions. 23 Relational Algebra Queries (2)

L6-S24 RA & SQL © M.E. Fayad SJSU – CmpE M.E. Fayad 1. If e 1 and e 2 are relational algebra expressions that result in relations with no common attributes, then (e 1 x e 2 ) is also relational algebra expression. 2. If e 1 and e 2 are relational algebra expressions, then (e 1 e 2 ) is also relational algebra expression. 24 Relational Algebra Queries (3)

L6-S25 RA & SQL © M.E. Fayad SJSU – CmpE M.E. Fayad 1. If e 1 is a relational algebra expression, then π L (e 1 ), where L is a subset of the attributes of the resulting relation σ F (e 1 ), where F is a selection condition, and ρC (e 1 ), where C is a renaming specification, are also relational algebra expressions. 2. No other expressions are relational algebra expressions. 25 Relational Algebra Queries (4)

L6-S26 RA & SQL © M.E. Fayad SJSU – CmpE M.E. Fayad Example: Consider the relations Taxrecord and Taxable – Chapter 1. Find the SSN and tax for each person. π SSN,Tax σ wages+interest+capital_gain = income Taxrecord × Taxtable Means: if there is a tuple t in Taxrecord and a tuple s in Taxable, such that the sum of the last three attributes value of t equals the first attribute value of s, then print out the first attribute value of t and the second attribute value of s. Relational Algebra Queries (5)

L6-S27 RA & SQL © M.E. Fayad SJSU – CmpE M.E. Fayad Example: Assume for now that the point of origin in both town and the broadcast maps in the same. The following query finds the area of Lincoln reached by a radio station. ( π X,Y ( σ Name=“Lincoln” Town ) )  ( π X,Y Broadcast ) The part before  selects the points of town and the part after  selects the points belong to at least one radio station broadcast area. Relational Algebra Queries (6)

L6-S28 RA & SQL © M.E. Fayad SJSU – CmpE M.E. Fayad SQL stands for Structured Query Language SQL is a popular query language for relational database systems. It is a complex language with many functions and options SQL is an ANSI standard language for accessing databases! SQL can execute queries against a database 28 SQL (1)

L6-S29 RA & SQL © M.E. Fayad SJSU – CmpE M.E. Fayad SQL can retrieve data from a database SQL can insert new records in a database SQL can delete records from a database SQL can update records in a database SQL is easy to learn 29 SQL (2)

L6-S30 RA & SQL © M.E. Fayad SJSU – CmpE M.E. Fayad SELECT a 1,..., a n FROM R 1, R 2, …, R m WHERE Con 1, …,Con k R i for 1 ≤ I ≤ m are relation names, a n (attribute names) Con i are atomic constraints This means: π a 1,..., a n ( σ Con 1 ( … (σ Con k ( R 1 × R 2 × … × R m ))…)) 30 SQL (3)

L6-S31 RA & SQL © M.E. Fayad SJSU – CmpE M.E. Fayad Find the SSN and tax for each person. SELECT SSN, Tax FROM Taxrecord, Taxtable WHERE wages+interest+capital_gain = income 31 SQL: Example

L6-S32 RA & SQL © M.E. Fayad SJSU – CmpE M.E. Fayad Two SQL expressions can be combined by: INTERSECT UNION MINUS – set difference Which has the form: SQL Query Q1 INTERSECT SQL Query Q2 32 AS – keyword used to rename relations (1)

L6-S33 RA & SQL © M.E. Fayad SJSU – CmpE M.E. Fayad AS keyword can be used for renaming attribute names in the SELECT clause and relation names in the FROM clause of basic queries. AS keyword is also useful for making multiple copies of a relation. For example, to rename an attribute A by B and to declare S and T to be copies of the relation R in SQL, we write SELECT A AS B FROM R A, R AS T WHERE …. 33 AS – keyword used to rename relations (2)

L6-S34 RA & SQL © M.E. Fayad SJSU – CmpE M.E. Fayad Example: Find the names of the streets that intersect. SELECT S.NAME, T.NAME FROM Streets AS S, Streets AS T WHERE S.X = T.X and S.Y = T.Y 34 AS – keyword used to rename relations (3)

L6-S35 RA & SQL © M.E. Fayad SJSU – CmpE M.E. Fayad Example: Assume we have the relations: Broadcast ( Radio, X, Y ) Town ( Name, X, Y ) Find the parts of Lincoln, NE that can be reached by at least one Radio station. SELECT X, Y FROM Town WHERE Name = “Lincoln” INTERSECT SELECT X, Y FROM Broadcast 35 Another Example

L6-S36 RA & SQL © M.E. Fayad SJSU – CmpE M.E. Fayad SELECT …….. FROM …….. WHERE a IN ( SELECT b FROM ….. WHERE ….. ) 36 Another way of connecting SQL expressions is using the IN keyword.

L6-S37 RA & SQL © M.E. Fayad SJSU – CmpE M.E. Fayad SELECT aggregate_function FROM ……. WHERE …… Aggregate_function – Max (c 1 a 1 + ……..+ c n a n ) where a i are attributes Min (c 1 a 1 + ……..+ c n a n ) and c i are constants Sum(a) where a is an attribute that is Avg(a) constant in each constraint tuple Count(a) 37 SQL with Aggregation

L6-S38 RA & SQL © M.E. Fayad SJSU – CmpE M.E. Fayad Package(Serial_No, From, Destination, Weight) Postage (Weight, Fee) Find the total postage of all packages sent from Omaha. SELECT Sum(Fee) FROM Package, Postage WHERE Package.Weight = Postage.Weight AND Package.From = “ Omaha “ 38 Example

L6-S39 RA & SQL © M.E. Fayad SJSU – CmpE M.E. Fayad SELECT a 1, …, a n, aggregate_function FROM ….. WHERE …… GROUP BY a 1,..., a k Evaluates basic SQL query Groups the tuples according to different values of a 1,..,a k Applies the aggregate function to each group separately {a 1, …, a k }  {a 1, …, a n } 39 GROUP BY

L6-S40 RA & SQL © M.E. Fayad SJSU – CmpE M.E. Fayad Find the total postage sent out from each city. SELECT Package.From, Sum(Postage.Fee) FROM Package, Postage WHERE Package.Weight = Postage.Weight GROUP BY Package.From 40 GROUP BY: Example

L6-S41 RA & SQL © M.E. Fayad SJSU – CmpE M.E. Fayad ??? 41 Solution for Exercise # 3.1

L6-S42 RA & SQL © M.E. Fayad SJSU – CmpE M.E. Fayad T/F 1.The operands of intersection, union, and difference relational algebra operators have no common attributes 2.Product operands have to be the same set and order of attributes. 3.Intersection is denoted by A  B. 4.The difference operator is denoted by A / B. 5.product is denoted by A x B. 6.A "projection"of a table is a choice of some or all of its columns. 42 Discussion Questions