420 likes | 562 Views
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. Relational Algebra.
E N D
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
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.
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.
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.
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 = .
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.
Union Combines all rows
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.
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?
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.
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.
Select Yields a subset of rows based on specified criterion
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.
Project Yields all values for selected attributes
Divide Requires user of single-column table and two-column table
Join Information from two or more tables is combined
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;
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_name FROM employees INNER JOIN departments d ON e.department_id = d.department_id
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
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
Query Using Two Conditions on a Single Field