42
Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield

Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield

Embed Size (px)

Citation preview

Page 1: Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield

Relational Algebra, Join and QBE

Yong ChoiSchool of BusinessCSUB, Bakersfield

Page 2: Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield

Study Objectives

Learn about relational database algebra Understand Query-by-Example (QBE) Use Criteria in QBE Create Calculated Columns in QBE Calculate Statistics in QBE

Page 3: Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield

Relational Algebra Relational algebra is a procedural query language. Relational algebra consists of a set of operations

that take one or two relations as input and produce a new relation as their result.

Relational algebra is a high level query language because operations are applied to entire relations.

When specifying a relational algebra query, users must specifies how – in what order – to execute the query operations.

Difficult and no standard method.

Page 4: Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield

Relational Algebra (con’t)

Most commercial relational DBMSs provide a high-level declarative language interface.

So, the user only specifies what the result is… Leaving the actual optimization and decisions on how

to execute the query to the DBMS.

Page 5: Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield

Relational Algebra Operators The relational algebra consists of a

collection of high-level operators that operate on relations.

Each such operator takes either one or two relations as its input and produces a new relation as its output. The traditional set operations: union,

intersection, difference, and (Cartesian) product.

The special relational operations: select, project, join, and divide.

Page 6: Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield

Set Theory

The notation S T will mean that S is a subset of T.- e.g., {1, 2, 3} {1,2,3,4}

If S is a subset of T and T contains at least one element that is not in S, then S is a proper subset of T. Notation for a proper subset is .

- e.g., {1, 2, 3} {1,2,3,4}, {1, 2, 3} {1,2,3,4} are true.

As far as a set is concerned, changing the order of elements does not change the set: e.g., {1, 2, 3} = {3, 2,1}

Two set A and B are disjoint iff A B = .

Page 7: Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield

Union (R1 U R2)

Produces a relation that includes all the rows (tuples) in R1 or R2 or both R1 and R2 must be union-compatible (derived from the math).

Union-compatible: Tables must have the same attributes

characteristics to be used in the union. That is, columns and domains must have be identical such as name and format of the filed.

Page 8: Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield

Union

Combines all rows

Page 9: Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield

Intersection (R1 R2)

Produces a relation that includes only the tuples that appears in both R1 and R2; R1 and R2 must be union compatible.

Page 10: Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield

Difference (R1 - R2 ) Produces a relation that includes all the tuples

in R1 that are not in R2; R1 and R2 must be union compatible.

How about R1 – R2?

Page 11: Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield

Product (R1 X R2)

Produces a relation that has the attributes of R1 and R2 and includes as tuples all possible combinations of tuples from R1 and R2.

Build a relation from two specified relations consisting of all possible combinations of tuples, one from each of the two relations.

Page 12: Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield

Product

Page 13: Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield

Select ( <selection condition> (R))

Do not confused with one of the SQL commands “select”

Select all tuples that satisfy the selection condition from a relation R.

The restriction operator effectively yields a "horizontal" subset of a given relation -- that is, that subset of the tuples of the given relation for which a specified comparison is satisfied.

Page 14: Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield

Yields a subset of rows based on specified criterion

Select

Page 15: Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield

Project (<attribute list> (R))

Produce a new relation with only some of the attributes of R and remove duplicate tuples.

The projection operator yields a "vertical" subset of a given relation --That is, that subset obtained by selecting specified attributes and then eliminating duplicate tuples within the attributes selected.

Page 16: Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield

Yields all values for selected attributes

Project

Page 17: Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield

Requires user of single-column table and two-column table

Divide

Page 18: Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield

Information from two or more tables is combined

Join

Page 19: Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield

Inner Join

the most common type of join. There must be a matching value in a field

common to both tables. all employees working on each project who

live in the same city as where the project is taking place:

SELECT Username, ProjectName, Location FROM Employee INNER JOIN Project ON Employee.City =

Project.Location;

Page 20: Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield

Equi Join Equijoin means the join between tables where

the values of two or more columns are equal. When a join condition relates two tables by an

operator other than equality, it is a non-equijoin.

SELECT e.first_name, d.department_nameFROM employees INNER JOIN departments d ON

e.department_id = d.department_id

Page 21: Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield

Other Joins

Theta JOIN EquiJOIN that compares specified columns

of each table using operator other than equality one

Outer JOIN Matched pairs are retained Unmatched values in other tables left null Right and left

Page 22: Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield

Query-by-Example (QBE)

Query Questions represented in a way the DBMS

can recognize and process QBE

Visual approach to writing queries Used in MS-Access

Page 23: Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield

Simple Queries

Page 24: Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield

Simple Queries (con’t.)

Page 25: Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield

Query that Includes All Fields

Page 26: Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield

Query with Simple Criteria

Page 27: Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield

Query Using AND Criteria

Page 28: Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield

Query Using OR Criteria

Page 29: Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield

Query Using Two Conditions on a Single Field

Page 30: Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield

Query Using Computed Field

Page 31: Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield

Query to Count Records

Page 32: Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield

Query to Calculate an Average

Page 33: Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield

Query to Sort Records

Page 34: Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield

Query to Sort on Multiple Keys

Page 35: Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield

Query to Sort on Multiple Keys (con’t.)

Page 36: Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield

Query to Join Tables

Page 37: Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield

Query to Join Tables (con’t.)

Page 38: Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield

Query to Restrict Records in a Join

Page 39: Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield

Update Query

Page 40: Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield

Delete Query

Page 41: Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield

Make-Table Query

Page 42: Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield

Make-Table Query (con’t.)