Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "© 2003-2006 M.E. Fayad SJSU -- CmpE Database Design Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering San José."— Presentation transcript:

1 © 2003-2006 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 95192-0180 http://www.engr.sjsu.edu/~fayad

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

3 L6-S3 RA & SQL © 2003-2006 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

4 L6-S4 RA & SQL © 2003-2006 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)

5 L6-S5 RA & SQL © 2003-2006 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)

6 L6-S6 RA & SQL © 2003-2006 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

7 L6-S7 RA & SQL © 2003-2006 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

8 L6-S8 RA & SQL © 2003-2006 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

9 L6-S9 RA & SQL © 2003-2006 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

10 L6-S10 RA & SQL © 2003-2006 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

11 L6-S11 RA & SQL © 2003-2006 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

12 L6-S12 RA & SQL © 2003-2006 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

13 L6-S13 RA & SQL © 2003-2006 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

14 L6-S14 RA & SQL © 2003-2006 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

15 L6-S15 RA & SQL © 2003-2006 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

16 L6-S16 RA & SQL © 2003-2006 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

17 L6-S17 RA & SQL © 2003-2006 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

18 L6-S18 RA & SQL © 2003-2006 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.

19 L6-S19 RA & SQL © 2003-2006 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

20 L6-S20 RA & SQL © 2003-2006 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

21 L6-S21 RA & SQL © 2003-2006 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)

22 L6-S22 RA & SQL © 2003-2006 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)

23 L6-S23 RA & SQL © 2003-2006 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)

24 L6-S24 RA & SQL © 2003-2006 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)

25 L6-S25 RA & SQL © 2003-2006 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)

26 L6-S26 RA & SQL © 2003-2006 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)

27 L6-S27 RA & SQL © 2003-2006 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)

28 L6-S28 RA & SQL © 2003-2006 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)

29 L6-S29 RA & SQL © 2003-2006 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)

30 L6-S30 RA & SQL © 2003-2006 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)

31 L6-S31 RA & SQL © 2003-2006 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

32 L6-S32 RA & SQL © 2003-2006 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)

33 L6-S33 RA & SQL © 2003-2006 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)

34 L6-S34 RA & SQL © 2003-2006 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)

35 L6-S35 RA & SQL © 2003-2006 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

36 L6-S36 RA & SQL © 2003-2006 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.

37 L6-S37 RA & SQL © 2003-2006 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

38 L6-S38 RA & SQL © 2003-2006 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

39 L6-S39 RA & SQL © 2003-2006 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

40 L6-S40 RA & SQL © 2003-2006 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

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

42 L6-S42 RA & SQL © 2003-2006 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


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

Similar presentations


Ads by Google