database systems 236363 relational algebra. query languages a query – an expression that enables...

Download Database Systems 236363 Relational Algebra. Query Languages A query – An expression that enables extracting data from a database A query language – A

If you can't read please download the document

Upload: elizabeth-blair

Post on 18-Dec-2015

216 views

Category:

Documents


4 download

TRANSCRIPT

  • Slide 1
  • Database Systems 236363 Relational Algebra
  • Slide 2
  • Query Languages A query An expression that enables extracting data from a database A query language A language for expressing queries
  • Slide 3
  • Relational Algebra Relational algebra is a language that enables expressing queries over relational databases The syntax is similar to algebra, but the expressions operate on tables The result of such an algebraic expression (query) is a table (relation) Unless stated otherwise, we assume that all relations are sets and all algebraic operators operate on sets
  • Slide 4
  • Algebraic Operators There are 5 basic operators plus 2 technical ones Unary operators: Projection, Selection Binary operators: Cartesian product, Subtraction, Union Renaming attributes: a technical operation to enable composing operators Assignment: a technical operation to enable execution in stages Complex operators that can be obtained by composing basic operators: Intersection, Join (including its variants), Division, etc. For performance reasons, some complex operators might have direct implementations
  • Slide 5
  • Projection Projection eliminates some of the attributes from the records of a given relation In terms of tables manipulation, this means removing columns that do not appear in the projection index and then eliminating multiple lines formed due to the column elimination RomanHebNo i 1 ii 2 T= RomanHeb i ii Heb,Roman T=
  • Slide 6
  • Selection Let T=T(A 1,,A m ) be a table with schema consisting of attributes A 1,,A m. For an expression , T consists of all records in T satisfying the expression may consist of the following: Comparisons (through the operators =,,,,) between an attribute and a constant or between two attributes. When the attribute is a set, it is also possible to use set operators like etc. Boolean operators, e.g., (A 1 =Cat) (A 2 8) RomanHebNo i 1 ii 2 iii 3 T=T= RomanHebNo i 1 ii 2 No2 T=
  • Slide 7
  • Union, Subtraction and Intersection These operations are applied only to relations of the same schema and are identical to their counterparts from set theory Union S T: obtains all records that appear in either S or T Subtraction S\T: obtains all records that appear in S but not in T Intersection: S T: obtains all records that appear in both S and T Intersection is not a basic operator it can be expressed as S\(S\T) Examples: ColorAnimal BrownHorse WhiteGoat ColorAnimal BlackDog WhiteGoat ColorAnimal BrownHorse WhiteGoat BlackDog ColorAnimal BrownHorse ColorAnimal WhiteGoat T=T= S=S= ST=ST= S\T= ST=ST=
  • Slide 8
  • Cartesian Product A Cartesian product yields all combinations of records from the first relation with records from the second relation In terms of tables, we take all concatenations of rows from the first table with rows from the second table Whenever S and T have attributes with the same name, we distinguish between them either by adding the table name as a prefix of the attribute name, e.g., S.Name and T.Name, or by adding a sequence number, e.g., Name1 and Name2 ColorAnimal BlackDog WhiteGoat T=T= Size Big Small S=S= ColorAnimalSize BlackDogBig WhiteGoatBig BlackDogSmall WhiteGoatSmall S x T= What happens if one of the relations is empty?
  • Slide 9
  • Renaming This is not an algebraic operation, but rather a technical helper operation that is used to compose complex operations (examples appear shortly) For a given table T=T(A 1,,A m ) with a schema consisting of attributes A 1,,A m, the operator A 1 B 1,, A m B m (T) returns an identical table in which the attribute names were changed to B 1,,B m ColorAnimal BlackDog WhiteGoat T=T= BlackDog WhiteGoat Animal ,Color (T)=
  • Slide 10
  • Basic Operators The five operators - Projection, Selection, Cartesian Product, Subtraction, and Union are basic, i.e., None of these operators can be obtained from the other four More complex operators (e.g., Join, Division, Intersection) can be obtained by composing some of the basic ones How can we show that an operator is basic? We need to find a property that is satisfied by this operator and cannot be obtained by any composition of the others
  • Slide 11
  • Proving that an Operator is Basic Claim: Projection cannot be expressed by composing Select, Cartesian Product, Subtraction, and Union Proof sketch: Let R be a relation with n attributes A 1,,A n. Then A 1 R results in a relation with fewer than n attributes. However, the result of applying any of the operators Select, Cartesian Product, Subtraction, and Union on R (and possibly other relations) yields at least n attributes. This can be shown by induction on the number of operators in the expression How can this proof be adjusted to show that Cartesian Product is a basic operator?
  • Slide 12
  • -Join Given two relations S(A 1,,A n ) and T(B 1,,B m ) and an expression on the attributes A 1,,A n,B 1,,B m, denote by S T the result of the algebraic expression (SxT) Example BA 21 43 DC 21 12 DCBA 2121 2143 1243 S=S=T=T= S B>C T= An SQL server walks into a bar. He approaches two tables at the far corner asking them: do you mind if I join you
  • Slide 13
  • Natural Join A very common operation on databases For the relations S(A 1,,A n,B 1,,B m ) and T(B 1,,B m,C 1,,C k ), denote by S T the relation that includes all possible combinations of a record from S with a record from T whose common attributes are the same, in which only a single attribute (column) is kept for each pair of common attributes. More precisely, S T= A 1,...,A n,S.B 1,...,S.B m,C 1,...,C k (S (S.B 1 =T.B 1 ) ... (S.B m =T.B m ) T) ColorAnimal BlackDog WhiteGoat PinkElephant S=S= ShadeColor LightBlue DarkBlue LightPink DarkPink T=T= ShadeColorAnimal LightPinkElephant DarkPinkElephant ST=ST=
  • Slide 14
  • Semi-Join For the relations S(A 1,,A n,B 1,,B m ) and T(B 1,,B m,C 1,,C k ), denote by S T the relation that includes all records in S for which there exists a record in T whose common attributes are the same More precisely, S T= A 1,...,A n,B 1,...,B m (S T) For performance reasons, this is usually implemented directly ColorAnimal BlackDog WhiteGoat PinkElephant S=S= ShadeColor LightBlue DarkBlue LightPink DarkPink T=T= ColorAnimal PinkElephant ST=ST=
  • Slide 15
  • Division For the relations S(A 1,,A n,B 1,,B m ) and T(B 1,,B m ) (i.e., the attributes of T are a subset of the attributes of S), denote by S T the relation R(A 1,,A n ) consisting of all records for which there exists a record in S corresponding to all records in T More precisely, S T is the maximal relation R such that RxT S I.e., S T= A 1,...,A n S\ A 1,...,A n ((( A 1,...,A n S) T)\S) ColorAnimal BlackDog WhiteDog PinkElephant S=S= Color Black White T=T= Animal Dog ST=ST=
  • Slide 16
  • Division - Example Wed like to obtain from table A the supplier number of all suppliers that sell all parts in table B pnosno P1S1 P2S1 P3S1 P4S1 P1S2 P2S2 P2S3 P2S4 P4S4 A= pno P2 sno S1 S2 S3 S4 AB=AB= B=
  • Slide 17
  • Division - Example Wed like to obtain from table A the supplier number of all suppliers that sell all parts in table B pnosno P1S1 P2S1 P3S1 P4S1 P1S2 P2S3 P2S4 P4S4 A= pno P2 P4 sno S1 S4 AB=AB= B=
  • Slide 18
  • Division - Example Wed like to obtain from table A the supplier number of all suppliers that sell all parts in table B pnosno P1S1 P2S1 P3S1 P4S1 P1S2 P2S3 P2S4 P4S4 A= pno P1 P2 P4 sno S1 AB=AB= B=
  • Slide 19
  • Relational Algebra Summarizing Example Recall the train operation example Station Height S_Name S_Type Line L_Type L_Num Direction Serves Km Train T_Num Days Service T_Category Class Food Gives Arrives Platform D_TimeA_Time
  • Slide 20
  • Relational Algebra Summarizing Example What Tables do we Extract? What columns should exist for the relationship set Serves? The key S_Name (of the Station entity set) The keys attributes L_Num and Direction (of Line) These triplet would serve as the key for Serves In addition, a column for the relation attribute Km What columns should exist for the relationship set Arrives? The key T_Num of the entity set Train The keys attributes for the aggregated relationship set Serves, i.e., S_Name, L_num, and Direction The three attributes of the relationship set Arrives itself Platform, A_time, D_time
  • Slide 21
  • The Schemas From the previous slide (underlined attribute names represent keys) Serves(S_Name, L_Num, Direction, Km) Arrives(T_Num, S_Name, L_Num, Direction, Platform, D_Time, A_Time) We will represent the multiple value attribute as a separate relation Station(S_Name, Height) Station_Type(S_Name, S_Type)
  • Slide 22
  • Sample Queries Which stations are served by the line 1-South? Here, all information is in the table/relation Serves S_Name ( (L_Num=1) (Direction=south) (Serves)) Which lines serve stations below sea level? Here, we need to join Serves and Station L_Num,Direction ( Height