We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 29
Chapter 6
Relational Algebra Overview of DBMS Relational Algebra And Relational Calculus.
Relational algebra is a procedural query language used to
query data stored in a relational database. It is used to select data from one or more tables, filter data based on certain criteria, and perform calculations on the selected data. Relational algebra consists of various operations such as Projection, Selection, Union, Intersection, Difference, Join, and Division. Overview of DBMS Relational Algebra And Relational Calculus. In contrast, relational calculus is a declarative query language used to express a query in terms of mathematical logic. It is a non-procedural language that specifies what data to retrieve rather than how to retrieve it. There are two types of relational calculus: tuple calculus and domain calculus. Tuple calculus retrieves specific tuples based on certain conditions, while domain calculus retrieves values that satisfy certain conditions. Overview of DBMS Relational Algebra And Relational Calculus. Here are some important points to remember about relational algebra and calculus: - Relational algebra is based on set theory, while relational calculus is based on predicate logic. - Both relational algebra and calculus are used for expressing queries in a structured way. Overview of DBMS Relational Algebra And Relational Calculus. - Relational algebra is a query language that is used for manipulating relations (tables) in a relational database system, while relational calculus provides a way to define queries using mathematical formulas. - Relational algebra is used for complex data manipulation, while calculus is used to retrieve specific subsets of data. - Relational algebra is more procedural, while calculus is more declarative. Relational Query Languages Query languages: Allow manipulation and retrieval of data from a database. Relational model supports simple, powerful QLs: Strong formal foundation based on logic. Allows for much optimization.
Query Languages != programming languages!
QLs not expected to be “Turing complete”. QLs not intended to be used for complex calculations. QLs support easy, efficient access to large data sets. Formal Relational Query Languages Two mathematical Query Languages form the basis for “real” languages (e.g. SQL), and for implementation: Relational Algebra: More operational(procedural), very useful for representing execution plans. Relational Calculus: Lets users describe what they want, rather than how to compute it. (Non- operational, declarative.) Preliminaries A query is applied to relation instances, and the result of a query is also a relation instance. Schemas of input relations for a query are fixed (but query will run regardless of instance!) The schema for the result of a given query is also fixed! Determined by definition of query language constructs. Positional vs. named-field notation: Positional notation easier for formal definitions, named-field notation more readable. Both used in SQL R1 sid bid day Example Instances 22 101 10/10/96 58 103 11/12/96 “Sailors” and “Reserves” sid sname rating age relations for our examples. S1 “bid”= boats. “sid”: sailors 22 dustin 7 45.0 We’ll use positional or 31 lubber 8 55.5 named field notation, 58 rusty 10 35.0 assume that names of fields in query results are S2 sid sname rating age `inherited’ from names of 28 yuppy 9 35.0 fields in query input relations. 31 lubber 8 55.5 44 guppy 5 35.0 58 rusty 10 35.0 Relational Algebra Basic operations: Selection ( ) Selects a subset of rows from relation. Projection ( ) Deletes unwanted columns from relation. Cross-product ( ) Allows us to combine two relations. Set-difference ( ) Tuples in relation. 1, but not in relation. 2. Union ( ) Tuples in relation. 1 and in relation 2. Additional operations: Intersection, join, division, renaming: Not essential, but (very!) useful. Since each operation returns a relation, operations can be composed! (Algebra is “closed”.) Projection Deletes attributes that are not in projection list. Schema of result contains exactly the fields in the projection list, with the same names that they had in the (only) input relation. Projection operator has to eliminate duplicates! (Why??, what are the consequences?) Note: real systems typically don’t do duplicate elimination unless the user explicitly asks for it. (Why not?) Selection Selects rows that satisfy selection condition. Schema of result identical to schema of (only) input relation. Result relation can be the input for another sname rating relational algebra yuppy 9 operation! (Operator rusty 10 composition.) sname,rating( rating 8(S2)) Union, Intersection, Set-Difference
All of these operations take two input relations, which
must be union-compatible: Same number of fields. `Corresponding’ fields have the same type. What is the schema of result? Set Operation: Union R U S returns relation instance containing all tuples that occur in either relation instance R or S, or both. • R and S must be union compatible. • Schema of the result is defined to be that of R. Set Operation: Intersection R ⋂ S: returns a relation instance containing all tuples that occur in both R and S. R and S must be union compatible. Schema of the result is that of R. Set Operation: Set-Difference R – S: returns a relation instance containing all tuples that occur in R but not in S. R and S must be union-compatible. Scheme of the result is the schema of R. Cross-Product Each row of S1 is paired with each row of R1. Result schema has one field per field of S1 and R1, with field names `inherited’ if possible. Conflict: Both S1 and R1 have a field called sid. Renaming Name conflict can arise in some situations It is convenient to be able to give names to the fields of a relation instance defined by a relational algebra expression. Joins
Result schema same as that of cross-product.
Fewer tuples than cross-product. Filters tuples not satisfying the join condition. Sometimes called a theta-join. Equi-Join: A special case of condition join where the condition c contains only equalities.
Result schema similar to cross-product, but only
one copy of fields for which equality is specified. Natural Join: Equijoin on all common fields. Division Not supported as a primitive operator, but useful for expressing queries like: Find sailors who have reserved all boats. Precondition: in A/B, the attributes in B must be included in the schema for A. Also, the result has attributes A-B. SALES(supId, prodId); PRODUCTS(prodId); Relations SALES and PRODUCTS must be built using projections. SALES/PRODUCTS: the ids of the suppliers supplying ALL products. Examples of Division A/B sno pno pno pno pno s1 p1 p2 p2 p1 s1 p2 p4 p2 s1 p3 B1 p4 B2 s1 p4 s2 p1 sno B3 s2 p2 s1 s3 p2 s2 sno s1 sno s4 p2 s3 s4 p4 s4 s4 s1 A A/B1 A/B2 A/B3 Example Tables
bid 103 Find names of sailors who’ve reserved a red boat Information about boat color only available in Boats; so need an extra join:
sname (( Boats) Re serves Sailors)
color ' red '
A more efficient solution:
sname ( (( Boats) Re s) Sailors) sid bid color ' red '
A query optimizer can find this, given the first solution!
Find sailors who’ve reserved a red or a green boat Can identify all red or green boats, then find sailors who’ve reserved one of these boats: (Tempboats, ( Boats)) color ' red ' color ' green '
sname(Tempboats Re serves Sailors)
Can also define Tempboats using union! (How?)
What happens if is replaced by in this query? Find sailors who’ve reserved a red and a green boat
Previous approach won’t work! Must identify
sailors who’ve reserved red boats, sailors who’ve reserved green boats, then find the intersection (note that sid is a key for Sailors): (Tempred, (( Boats) Re serves)) sid color ' red ' (Tempgreen, (( Boats) Re serves)) sid color ' green'
sname((Tempred Tempgreen) Sailors)
Find the names of sailors who’ve reserved all boats
Uses division; schemas of the input relations
to / must be carefully chosen:
(Tempsids, ( Re serves) / ( Boats))
sid, bid bid sname (Tempsids Sailors)
To find sailors who’ve reserved all ‘Interlake’ boats:
..... / ( Boats) bid bname ' Interlake' Summary The relational model has rigorously defined query languages that are simple and powerful. Relational algebra is more operational; useful as internal representation for query evaluation plans. Several ways of expressing a given query; a query optimizer should choose the most efficient version.