Download presentation
Presentation is loading. Please wait.
Published byRichard Lynch Modified over 8 years ago
1
LECTURE THREE RELATIONAL ALGEBRA 11
2
Objectives Meaning of the term relational completeness. How to form queries in relational algebra. 22Relational Algebra
3
Introduction Relational algebra and relational calculus are formal languages associated with the relational model (form the basis for relational languages) Informally, relational algebra is a (high-level) procedural language; can tell the DBMS how to build a new relations from other relations in the database. Relational calculus a non-procedural language; used to define a relation in terms of one or more database relations. However, formally both are equivalent to one another; are a basis for other Data Manipulation Languages for relational databases-illustrates the basic operations required for any DML. A language that produces a relation that can be derived using relational calculus is relationally complete. 33Relational Algebra
4
Relational algebra operations work on one or more relations to define another relation without changing the original relations. Both operands and results are relations, so output from one operation can become input to another operation. Allows expressions to be nested, just as in arithmetic. Relational Algebra is a relation-at-a-time (or set) language in which all tuples, possibly from several relations, are manipulated in one statement without looping 44Relational Algebra
5
5 basic operations in relational algebra: Selection, Projection, Cartesian product, Union, and Set Difference. These perform most of the data retrieval operations needed. Also have Join, Intersection, and Division operations, which can be expressed in terms of 5 basic operations. 55Relational Algebra
6
Relational Algebra Operations cont’d These operations can be categorised into two groups; unary and binary operations. Unary: These operate on one relation. Examples of unary operations include; selection and projection Binary; These work on pairs of relations. Examples include the; Cartesian product, union, set difference, join, intersections and division operations 6Relational Algebra
7
Relational Algebra Operations 77Relational Algebra
8
Relational Algebra Operations 88Relational Algebra
9
Selection (or Restriction) predicate (R) Works on a single relation R and defines a relation that contains only those tuples (rows) of R that satisfy the specified condition (predicate). More complex predicates can be generated using the logical operators; Λ (AND), ν (OR) and ~ (NOT) 99Relational Algebra
10
10 Example - Selection (or Restriction) List all staff with a salary greater than £10,000. salary > 10000 (Staff)
11
Projection col1,..., coln (R) Works on a single relation R and defines a relation that contains a vertical subset of R, extracting the values of specified attributes and eliminating duplicates. 11 Relational Algebra
12
Example - Projection Produce a list of salaries for all staff, showing only their name and salary details. fName, lName, salary (Staff) 12 Relational Algebra
13
Union R S Union of two relations R and S defines a relation that contains all the tuples of R, or S, or both R and S, duplicate tuples being eliminated. R and S must be union-compatible; both relations have the same number of attributes with each pair of corresponding attributes having the same domain.. If R and S have I and J tuples, respectively, union is obtained by concatenating them into one relation with a maximum of (I + J) tuples. 13 Relational Algebra
14
Example BRANCH PROPERTYFORRENT
15
Example - Union List all cities where there is either a branch office or a property for rent. city (Branch) city (PropertyForRent) 15 Relational Algebra
16
Set Difference R – S Defines a relation consisting of the tuples that are in relation R, but not in S. R and S must be union-compatible. 16 Relational Algebra
17
Example - Set Difference List all cities where there is a branch office but no properties for rent. city (Branch) – city (PropertyForRent) 17 Relational Algebra
18
Intersection R S Defines a relation consisting of the set of all tuples that are in both R and S. R and S must be union-compatible. Expressed in terms of the set difference operation: R S = R – (R – S) 18 Relational Algebra
19
Example - Intersection List all cities where there is both a branch office and at least one property for rent. city (Branch) city (PropertyForRent) 19 Relational Algebra
20
Cartesian product R X S Defines a relation that is the concatenation of every tuple of relation R with every tuple of relation S; relation with all possible pairs of tuples from the two relations. If relation R has I tuples and N attributes, and relation S has J tuples and M attributes, then the cartesian product will have (I*J) tuples and (N+M) attributes. In case the relations have attributes with the same names, these are prefixed with the relation name to maintain the uniqueness of attribute names in the resulting relation. 20 Relational Algebra
21
Example - Cartesian Product List the names and comments of all clients who have viewed a property for rent. ( clientNo, fName, lName (Client)) X ( clientNo, propertyNo,comment (Viewing)) 21 Relational Algebra
22
22 Example - Cartesian Product cont’d Relational Algebra22
23
Example - Cartesian Product and Selection Use selection operation to extract those tuples where Client.clientNo = Viewing.clientNo. σ Client.clientNo = viewing.clientNo ((Π clientNo,fName,lName (Client)) (Π clientNo,propertyNo,comment (Viewing))) 23 Cartesian product and Selection can be reduced to a single operation called a Join. 23Relational Algebra
24
Join Operations This is one of the essential operations in relational algebra; a join operation combines two relations to form a new one. Join is a derivative of Cartesian product; Equivalent to performing a Selection, using join predicate as selection formula, over Cartesian product of the two operand relations (Cartesian product that satisfies a given condition(s)). One of the most difficult operations to implement efficiently in an RDBMS and one reason why RDBMSs have intrinsic performance problems. 24 Relational Algebra
25
Join Operations Various forms of join operation Theta join Equijoin (a particular type of Theta join) Natural join Outer join Semijoin 25 Relational Algebra
26
Theta join ( -join) R F S Defines a relation that contains tuples satisfying the predicate F from the Cartesian product of R and S. The predicate F is of the form R.a i S.b i where may be one of the comparison operators (, , =, ). 26 Relational Algebra
27
Theta join ( -join) 27 Degree of a Theta join is sum of degrees of the operand relations R and S; as the Cartesian product. If predicate F contains only equality (=), the term Equijoin is used. Can rewrite Theta join using basic Selection and Cartesian product operations. R F S = F (R S) 27Relational Algebra
28
Example - Equijoin List the names and comments of all clients who have viewed a property for rent. ( clientNo,fName,lName (Client)) Client.clientNo = Viewing.clientNo ( clientNo,propertyNo,comment (Viewing)) 28 Relational Algebra
29
Natural Join R S An Equijoin of the two relations R and S over all common attributes x. One occurrence of each common attribute is eliminated from the result. The degree of a natural join is the sum of the degrees of the relations R and S less the number of attributes in x. 29 Relational Algebra
30
Example - Natural Join List the names and comments of all clients who have viewed a property for rent. ( clientNo,fName,lName (Client)) ( clientNo,propertyNo,comment (Viewing)) 30 Relational Algebra
31
Outer join To display rows in the result that do not have matching values in the join column, use Outer join. Advantage: preservation of information (that would otherwise be lost). There are basically three types of outer join operations Left (natural) Outer join Right Outer join Full Outer join 31 Relational Algebra
32
Left Outer join ( ) R S (Left) outer join is join in which tuples from R that do not have matching values in common columns of S are also included in result relation. The missing values in the second relation are set to null. Relational Algebra32
33
Example - Left Outer join Produce a status report on property viewings. propertyNo,street,city (PropertyForRent) Viewing 33 Relational Algebra
34
Outer joins cont’d Right Outer Join: This join keeps all the tuples in the right-hand relation in the result. Full Outer Join: This join keeps all the tuples in both relations, padding tuples with nulls when no matching tuples are found. Relational Algebra34
35
Semijoin R F S Defines a relation that contains the tuples of R that participate in the join of R with S. The Semijoin operation performs a join of the two relations and then projects over the attributes of the first operand. 35 Can rewrite Semijoin using Projection and Join: R F S = A (R F S) 35Relational Algebra
36
Example
37
Example - Semijoin List complete details of all staff who work at the branch in Glasgow. Staff Staff.brancNo = Branch.branchNo and branch.city = ‘Glasgow’ Branch 37 Relational Algebra
38
Division (R S) Defines a relation over the attributes C that consists of set of tuples from R (attribute set A) that match combination of every tuple in S (attribute set B). Note: C is the set of attributes of R that are not attributes of S (A-B) Expressed using basic operations: T 1 C (R) T 2 C ((S X T 1 ) – R) T T 1 – T 2 38 Relational Algebra
39
Example VIEWING PROPERTYFORRENT
40
Example - Division Identify all clients who have viewed all properties with three rooms. ( clientNo,propertyNo (Viewing)) ( propertyNo ( rooms = 3 (PropertyForRent))) 40 Relational Algebra
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.