Presentation is loading. Please wait.

Presentation is loading. Please wait.

Database Design Dr. M.E. Fayad, Professor

Similar presentations


Presentation on theme: "Database Design Dr. M.E. Fayad, Professor"— Presentation transcript:

1 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 © M.E. Fayad SJSU -- CmpE

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

3 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 © M.E. Fayad SJSU – CmpE M.E. Fayad

4 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 © M.E. Fayad SJSU – CmpE M.E. Fayad

5 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 © M.E. Fayad SJSU – CmpE M.E. Fayad

6 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 © M.E. Fayad SJSU – CmpE M.E. Fayad

7 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 © M.E. Fayad SJSU – CmpE M.E. Fayad

8 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 © M.E. Fayad SJSU – CmpE M.E. Fayad

9 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 © M.E. Fayad SJSU – CmpE M.E. Fayad

10 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 © M.E. Fayad SJSU – CmpE M.E. Fayad

11 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 © M.E. Fayad SJSU – CmpE M.E. Fayad

12 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 © M.E. Fayad SJSU – CmpE M.E. Fayad

13 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 © M.E. Fayad SJSU – CmpE M.E. Fayad

14 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 © M.E. Fayad SJSU – CmpE M.E. Fayad

15 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 © M.E. Fayad SJSU – CmpE M.E. Fayad

16 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) D points in the plane H(x, y, z), I(x, y, z) D points in space F(z) line on the z-axis K(y,z) D points in (y, z) plane 16 © M.E. Fayad SJSU – CmpE M.E. Fayad

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

18 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 © M.E. Fayad SJSU – CmpE M.E. Fayad

19 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 © M.E. Fayad SJSU – CmpE M.E. Fayad

20 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 © M.E. Fayad SJSU – CmpE M.E. Fayad

21 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 © M.E. Fayad SJSU – CmpE M.E. Fayad

22 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 © M.E. Fayad SJSU – CmpE M.E. Fayad

23 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 © M.E. Fayad SJSU – CmpE M.E. Fayad

24 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 (e e2) is also relational algebra expression. 24 © M.E. Fayad SJSU – CmpE M.E. Fayad

25 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 © M.E. Fayad SJSU – CmpE M.E. Fayad

26 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. © M.E. Fayad SJSU – CmpE M.E. Fayad

27 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. © M.E. Fayad SJSU – CmpE M.E. Fayad

28 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 © M.E. Fayad SJSU – CmpE M.E. Fayad

29 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 © M.E. Fayad SJSU – CmpE M.E. Fayad

30 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 © M.E. Fayad SJSU – CmpE M.E. Fayad

31 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 © M.E. Fayad SJSU – CmpE M.E. Fayad

32 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 © M.E. Fayad SJSU – CmpE M.E. Fayad

33 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 © M.E. Fayad SJSU – CmpE M.E. Fayad

34 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 © M.E. Fayad SJSU – CmpE M.E. Fayad

35 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 © M.E. Fayad SJSU – CmpE M.E. Fayad

36 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 © M.E. Fayad SJSU – CmpE M.E. Fayad

37 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 © M.E. Fayad SJSU – CmpE M.E. Fayad

38 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 © M.E. Fayad SJSU – CmpE M.E. Fayad

39 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 © M.E. Fayad SJSU – CmpE M.E. Fayad

40 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 © M.E. Fayad SJSU – CmpE M.E. Fayad

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

42 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 © M.E. Fayad SJSU – CmpE M.E. Fayad


Download ppt "Database Design Dr. M.E. Fayad, Professor"

Similar presentations


Ads by Google