Database Design Dr. M.E. Fayad, Professor

Slides:



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

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.
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.
1 Relational Algebra & Calculus. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.  Relational.
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é.
FALL 2004CENG 351 File Structures and Data Managemnet1 Relational Algebra.
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.
© M.E. Fayad SJSU -- CmpE Database Design Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering San José.
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.
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é.
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.
Relational Algebra.
The Relational Algebra and Calculus
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.
CSCD34-Data Management Systems - A. Vaisman1 Relational Algebra.
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)
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
Chapter # 6 The Relational Algebra and Calculus
Fundamental of Database Systems
Relational Algebra - Part 1
Advanced Object-Oriented Analysis & Design
Relational Algebra Chapter 4 1.
Database Design Dr. M.E. Fayad, Professor
Chapter 2: Intro to Relational Model
Relational Algebra Chapter 4, Part A
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
Relational Algebra Chapter 4, Sections 4.1 – 4.2
Databases.
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
Software System Engineering
Relational Algebra Chapter 4 - part I.
Presentation transcript:

Database Design Dr. M.E. Fayad, Professor 5/9/2019 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 95192-0180 http://www.engr.sjsu.edu/~fayad © 2003-2006 M.E. Fayad SJSU -- CmpE

Relational Algebra and SQL 5/9/2019 Lesson 06: Relational Algebra and SQL 2 © 2003-2006 M.E. Fayad SJSU – CmpE M.E. Fayad

5/9/2019 Lesson Objectives 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 3 © 2003-2006 M.E. Fayad SJSU – CmpE M.E. Fayad

Two Different Query Languages for Relational Database (1) 5/9/2019 Two Different Query Languages for Relational Database (1) 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 © 2003-2006 M.E. Fayad SJSU – CmpE M.E. Fayad

Two Different Query Languages for Relational Database (2) 5/9/2019 Two Different Query Languages for Relational Database (2) 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 © 2003-2006 M.E. Fayad SJSU – CmpE M.E. Fayad

Relational Algebra Operators 5/9/2019 Relational Algebra Operators 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 © 2003-2006 M.E. Fayad SJSU – CmpE M.E. Fayad

5/9/2019 Intersection 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 © 2003-2006 M.E. Fayad SJSU – CmpE M.E. Fayad

8 Union The union of two relations A and B, denoted A  B 5/9/2019 Union 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 © 2003-2006 M.E. Fayad SJSU – CmpE M.E. Fayad

9 Difference The difference of two relations, denoted A \ B 5/9/2019 Difference 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 © 2003-2006 M.E. Fayad SJSU – CmpE M.E. Fayad

5/9/2019 Product 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 © 2003-2006 M.E. Fayad SJSU – CmpE M.E. Fayad

5/9/2019 Project 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 [l1, ….., lk] 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 li. 11 © 2003-2006 M.E. Fayad SJSU – CmpE M.E. Fayad

5/9/2019 Select 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 © 2003-2006 M.E. Fayad SJSU – CmpE M.E. Fayad

5/9/2019 Rename 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 Xi to Yi. If we only want to change the name of the relation, then the form ρBA can be used. 13 © 2003-2006 M.E. Fayad SJSU – CmpE M.E. Fayad

5/9/2019 Natural Join 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 © 2003-2006 M.E. Fayad SJSU – CmpE M.E. Fayad

Which Operators can be applied? 5/9/2019 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 15 © 2003-2006 M.E. Fayad SJSU – CmpE M.E. Fayad

How to read set of points 5/9/2019 How to read set of points 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 © 2003-2006 M.E. Fayad SJSU – CmpE M.E. Fayad

Relation algebra operators (1) 5/9/2019 Relation algebra operators (1) A and B A  B = C 5 A  B = D A \ B = E © 2003-2006 M.E. Fayad SJSU – CmpE M.E. Fayad

Relation algebra operators (2) 5/9/2019 Relation algebra operators (2) 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. A  F © 2003-2006 M.E. Fayad SJSU – CmpE M.E. Fayad

Relation algebra operators (3) 5/9/2019 Relation algebra operators (3)  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.  x,y H Projection from H © 2003-2006 M.E. Fayad SJSU – CmpE M.E. Fayad

Relation algebra operators (4) 5/9/2019 Relation algebra operators (4) 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  2x+z = 0 I © 2003-2006 M.E. Fayad SJSU – CmpE M.E. Fayad

Relation algebra operators (5) 5/9/2019 Relation algebra operators (5) 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) J K © 2003-2006 M.E. Fayad SJSU – CmpE M.E. Fayad

Relational Algebra Queries (1) 5/9/2019 Relational Algebra Queries (1) 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 © 2003-2006 M.E. Fayad SJSU – CmpE M.E. Fayad

Relational Algebra Queries (2) 5/9/2019 Relational Algebra Queries (2) Each relation name Ri is a relational algebra expression. (This will just return the relation Ri as a result.) If e1 and e2 are relational algebra expressions that result in relations that have the same set and order of attributes, then (e1  e2), (e1  e2), (e1 \ e2) are also relational algebra expressions. 23 © 2003-2006 M.E. Fayad SJSU – CmpE M.E. Fayad

Relational Algebra Queries (3) 5/9/2019 Relational Algebra Queries (3) If e1 and e2 are relational algebra expressions that result in relations with no common attributes, then (e1 x e2) is also relational algebra expression. If e1 and e2 are relational algebra expressions, then (e1 e2) is also relational algebra expression. 24 © 2003-2006 M.E. Fayad SJSU – CmpE M.E. Fayad

Relational Algebra Queries (4) 5/9/2019 Relational Algebra Queries (4) If e1 is a relational algebra expression, then πL (e1), where L is a subset of the attributes of the resulting relation σF (e1), where F is a selection condition, and ρC(e1), where C is a renaming specification, are also relational algebra expressions. No other expressions are relational algebra expressions. 25 © 2003-2006 M.E. Fayad SJSU – CmpE M.E. Fayad

Relational Algebra Queries (5) 5/9/2019 Relational Algebra Queries (5) 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. © 2003-2006 M.E. Fayad SJSU – CmpE M.E. Fayad

Relational Algebra Queries (6) 5/9/2019 Relational Algebra Queries (6) 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. © 2003-2006 M.E. Fayad SJSU – CmpE M.E. Fayad

28 SQL (1) SQL stands for Structured Query Language 5/9/2019 SQL (1) 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 © 2003-2006 M.E. Fayad SJSU – CmpE M.E. Fayad

29 SQL (2) SQL can retrieve data from a database 5/9/2019 SQL (2) 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 © 2003-2006 M.E. Fayad SJSU – CmpE M.E. Fayad

30 SQL (3) SELECT a1, ..., an FROM R1, R2, …, Rm WHERE Con1, …,Conk 5/9/2019 SQL (3) SELECT a1, ..., an FROM R1, R2, …, Rm WHERE Con1, …,Conk Ri for 1 ≤ I ≤ m are relation names, an (attribute names) Coni are atomic constraints This means: π a1, ..., an( σ Con1( … (σ Conk ( R1 × R2 × … × Rm))…)) 30 © 2003-2006 M.E. Fayad SJSU – CmpE M.E. Fayad

31 SQL: Example Find the SSN and tax for each person. SELECT SSN, Tax 5/9/2019 SQL: Example Find the SSN and tax for each person. SELECT SSN, Tax FROM Taxrecord, Taxtable WHERE wages+interest+capital_gain = income 31 © 2003-2006 M.E. Fayad SJSU – CmpE M.E. Fayad

AS – keyword used to rename relations (1) 5/9/2019 AS – keyword used to rename relations (1) Two SQL expressions can be combined by: INTERSECT UNION MINUS – set difference Which has the form: SQL Query Q1 SQL Query Q2 32 © 2003-2006 M.E. Fayad SJSU – CmpE M.E. Fayad

AS – keyword used to rename relations (2) 5/9/2019 AS – keyword used to rename relations (2) 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 © 2003-2006 M.E. Fayad SJSU – CmpE M.E. Fayad

AS – keyword used to rename relations (3) 5/9/2019 AS – keyword used to rename relations (3) 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 © 2003-2006 M.E. Fayad SJSU – CmpE M.E. Fayad

35 Another Example Example: Assume we have the relations: 5/9/2019 Another Example 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 © 2003-2006 M.E. Fayad SJSU – CmpE M.E. Fayad

Another way of connecting SQL expressions is using the IN keyword. 5/9/2019 Another way of connecting SQL expressions is using the IN keyword. SELECT …….. FROM …….. WHERE a IN ( SELECT b FROM ….. WHERE ….. ) 36 © 2003-2006 M.E. Fayad SJSU – CmpE M.E. Fayad

37 SQL with Aggregation SELECT aggregate_function FROM ……. WHERE …… 5/9/2019 SQL with Aggregation SELECT aggregate_function FROM ……. WHERE …… Aggregate_function – Max (c1a1 + ……..+ cnan) where ai are attributes Min (c1a1 + ……..+ cnan) and ci are constants Sum(a) where a is an attribute that is Avg(a) constant in each constraint tuple Count(a) 37 © 2003-2006 M.E. Fayad SJSU – CmpE M.E. Fayad

38 Example Find the total postage of all packages sent from Omaha. 5/9/2019 Example 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 © 2003-2006 M.E. Fayad SJSU – CmpE M.E. Fayad

39 GROUP BY SELECT a1, …, an, aggregate_function FROM ….. WHERE …… 5/9/2019 GROUP BY SELECT a1, …, an, aggregate_function FROM ….. WHERE …… GROUP BY a1, ..., ak Evaluates basic SQL query Groups the tuples according to different values of a1,..,ak Applies the aggregate function to each group separately {a1, …, ak}  {a1, …, an} 39 © 2003-2006 M.E. Fayad SJSU – CmpE M.E. Fayad

40 GROUP BY: Example Find the total postage sent out from each city. 5/9/2019 GROUP BY: Example 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 © 2003-2006 M.E. Fayad SJSU – CmpE M.E. Fayad

Solution for Exercise # 3.1 5/9/2019 Solution for Exercise # 3.1 ??? 41 © 2003-2006 M.E. Fayad SJSU – CmpE M.E. Fayad

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