+ Division Not supported as a primitive operator, but useful for expressing queries like: Find sailors who have reserved all boats. Precondition: in A/B,

Slides:



Advertisements
Similar presentations
Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
Advertisements

1 CHAPTER 4 RELATIONAL ALGEBRA AND CALCULUS. 2 Introduction - We discuss here two mathematical formalisms which can be used as the basis for stating and.
Relational Algebra Ch. 7.4 – 7.6 John Ortiz. Lecture 4Relational Algebra2 Relational Query Languages  Query languages: allow manipulation and retrieval.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4, Part A Modified by Donghui Zhang.
INFS614, Fall 08 1 Relational Algebra Lecture 4. INFS614, Fall 08 2 Relational Query Languages v Query languages: Allow manipulation and retrieval of.
Relational Algebra 1 Chapter 5.1 V3.0 Napier University Dr Gordon Russell.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 52 Database Systems I Relational Algebra.
Database Systems Chapter 6 ITM Relational Algebra The basic set of operations for the relational model is the relational algebra. –enable the specification.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4, Part A.
Relational Algebra Archana Gupta CS 157. What is Relational Algebra? Relational Algebra is formal description of how relational database operates. It.
Rutgers University Relational Algebra 198:541 Rutgers University.
Relational Algebra Chapter 4 - part I. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.  Relational.
The Relational Model Codd (1970): based on set theory Relational model: represents the database as a collection of relations (a table of values --> file)
Relational Data Model Ervi Bongso Alan Fernando CS157A.
CSCD343- Introduction to databases- A. Vaisman1 Relational Algebra.
Relational Algebra, R. Ramakrishnan and J. Gehrke (with additions by Ch. Eick) 1 Relational Algebra.
3 The Relational Model MIS 304 Winter Class Objectives That the relational database model takes a logical view of data That the relational model’s.
The Relational Database Model
RELATIONAL ALGEBRA Objectives
Relational Model & Relational Algebra. 2 Relational Model u Terminology of relational model. u How tables are used to represent data. u Connection between.
1 The Relational Data Model, Relational Constraints, and The Relational Algebra.
1 Relational Algebra and Calculus Chapter 4. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.
DBSQL 3-1 Copyright © Genetic Computer School 2009 Chapter 3 Relational Database Model.
Database System Concepts, 5 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 2: Relational.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 2: Intro to Relational.
Chapter 2 Adapted from Silberschatz, et al. CHECK SLIDE 16.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com ICOM 5016 – Introduction.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Relational Algebra.
Relational Algebra  Souhad M. Daraghma. Relational Query Languages Query languages: Allow manipulation and retrieval of data from a database. Relational.
Module Coordinator Tan Szu Tak School of Information and Communication Technology, Politeknik Brunei Semester
CS 4432query processing1 CS4432: Database Systems II Lecture #11 Professor Elke A. Rundensteiner.
1 Relational Algebra and Calculas Chapter 4, Part A.
ICS 321 Fall 2011 The Relational Model of Data (i) Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 8/29/20111Lipyeow.
1 Relational Algebra Chapter 4, Sections 4.1 – 4.2.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 2: Intro to Relational.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Management Systems Chapter 4 Relational Algebra.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
Advanced Relational Algebra & SQL (Part1 )
Chapter 2: Intro to Relational Model. 2.2 Example of a Relation attributes (or columns) tuples (or rows)
CSCD34-Data Management Systems - A. Vaisman1 Relational Algebra.
Database Management Systems, R. Ramakrishnan1 Relational Algebra Module 3, Lecture 1.
Operations in the Relational Model COP 4720 Lecture 8 Lecture Notes.
CpSc 462/662: Database Management Systems (DBMS) (TEXNH Approach) Relational Algebra James Wang.
Presented By: Miss N. Nembhard. Relation Algebra Relational Algebra is : the formal description of how a relational database operates the mathematics.
CMPT 258 Database Systems Relational Algebra (Chapter 4)
603 Database Systems Senior Lecturer: Laurie Webster II, M.S.S.E.,M.S.E.E., M.S.BME, Ph.D., P.E. Lecture 9 A First Course in Database Systems.
3 1 Chapter 3 The Relational Database Model Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel.
CSCI 6315 Applied Database Systems Review for Midterm Exam I Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX 78539
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 2: Intro to Relational.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4, Part A.
LECTURE THREE RELATIONAL ALGEBRA 11. Objectives  Meaning of the term relational completeness.  How to form queries in relational algebra. 22Relational.
Relational Algebra COMP3211 Advanced Databases Nicholas Gibbins
©Silberschatz, Korth and Sudarshan2.1Database System Concepts - 6 th Edition Chapter 8: Relational Algebra.
Copyright © 2011 Ramez Elmasri and Shamkant Navathe Lecture 8 Relational Algebra April 26, 2015.
Ritu CHaturvedi Some figures are adapted from T. COnnolly
Relational Algebra Database Management Systems, 3rd ed., Ramakrishnan and Gehrke, Chapter 4.
COMP3017 Advanced Databases
Module 2: Intro to Relational Model
Relational Algebra.
Lecture 2 The Relational Model
Chapter 2: Intro to Relational Model
Relational Algebra 461 The slides for this text are organized into chapters. This lecture covers relational algebra, from Chapter 4. The relational calculus.
Chapter 3 The Relational Database Model
LECTURE 3: Relational Algebra
Chapter 2: Intro to Relational Model
Chapter 2: Intro to Relational Model
Chapter 2: Intro to Relational Model
Example of a Relation attributes (or columns) tuples (or rows)
Chapter 2: Intro to Relational Model
Unit Relational Algebra 1
Presentation transcript:

+ 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 A B1 B2 B3 A/B1A/B2A/B3

+ Housekeeping – Jan 25, 2013

+ Today: Quick review of the various relational algebra concepts using small example tables Monday: work through exercises based on our running example of the transit scheme (I will finish off our first draft of the table scheme today) Goal: see if we can retrieve the information needed to support our scenarios

+ Quiz Confusion (a.k.a. don’t try to remember how you wrote a quiz on less than 4 hours sleep) Q6. What is the role of a foreign key in a table a. Links two or more tables together b. Uniquely identifies each record in the table for which it is the foreign key c. Uniquely identifies each record in the table from which it originates d. A and C

+ Key selection A candidate key is any set of one or more columns whose combined values are unique among all occurrences (i.e., tuples or rows). Since a null value is not guaranteed to be unique, no component of a candidate key is allowed to be null. There can be any number of candidate keys in a table The primary key of any table is any candidate key of that table which the database designer arbitrarily designates as "primary". The primary key may be selected for convenience, comprehension, performance, or any other reasons. The alternate keys of any table are simply those candidate keys which are not currently selected as the primary key. A foreign key is a set of one or more columns in any table (not necessarily a candidate key, let alone the primary key, of that table) which may hold the value(s) found in the primary key** column(s) of some other table. ** see slide 7

+ Entity Integrity Constraint No primary key value can be null. Need to be able to identify individual tuples in the relation – if allowed to have a null value for the primary key, may not be able to distinguish between two or more tuples

+ Referential Integrity Constraint Specified between two relations Used to maintain consistency among tuples of the two relations A tuple in one relation that refers to another relation must refer to an EXISTING TUPLE in that relation Foreign keys specify a referential integrity constraint between the two relation A set of attributes FK in relation schema R1 is a foreign key of R1 if: The attributes in FK have the same domain as the primary key attributes PK of another relation R2 (FK references R2) A value of FK in a tuple t1 of R1 either occurs as a value of PK for some tuple T=t2 in R2 or is null. E.g. if dept # is the foreign key of employee, an employee must either be assigned a value of the primary key in Dept or the value can be Null in employee if no department is currently assigned

+ Does a foreign key have to link to a primary key? Slightly controversial. Remember that there could be several candidate keys from which a single primary key was chosen. To ensure that each record in the referencing table references exactly one record in the referenced table, the referenced column(s) in the referenced table must have a primary key constraint or have both unique and not-null constraints. Having a unique index is not sufficient. Under the hood: When you specify a primary key constraint for a table, the Database Engine enforces data uniqueness by automatically creating a unique index for the primary key columns. This index also permits fast access to data when the primary key is used in queries. If a primary key constraint is defined on more than one column, values may be duplicated within one column, but each combination of values from all the columns in the primary key constraint definition must be unique. More info on foreign key constraints (but remember, this is DB specific):

+ Relational Algebra CSCI 2141 W 2013

+ What is Relational Algebra? Relational Algebra is formal description of how relational database operates. It is a procedural query language, i.e. user must define both “ how ” and “ what ” to retrieve. It consists of a set of operators that consume either one or two relations as input. An operator produces one relation as its output.

+ Introduction to Relational Algebra Introduced by E. F. Codd in Codd proposed such an algebra as a basis for database query languages.

+ Relational Query Languages Formal: relational algebra, relational calculus, Datalog Actual: SQL, Quel, Query-by- Example (QBE) In ALL languages, a query is executed over a set of relations, get single relation as the result

+ Terminology Relation - a set of tuples. Tuple - a collection of attributes which describe some real world entity. Attribute - a real world role played by a named domain. Domain - a set of atomic values. Set - a mathematical definition for a collection of objects which contains no duplicates.

+ Algebra Operations Unary Operations - operate on one relation. These include select, project and rename operators. Binary Operations - operate on pairs of relations. These include union, set difference, division, cartesian product, equality join, natural join, join and semi-join operators.

+ Select Operator The Select operator selects tuples that satisfies a predicate; e.g. retrieve the employees whose salary is 30,000 б Salary = 30,000 (Employee) Conditions in Selection: Simple Condition: (attribute)(comparison)(attribute) (attribute)(comparison)(constant) Comparison: =,≠,≤,≥, Condition: combination of simple conditions with AND, OR, NOT

Select Operator Example NameAgeWeight Harry3480 Sally2864 George2970 Helena54 Peter3480 Person б Age≥34 (Person) б Age=Weight (Person)

Select Operator Example NameAgeWeight Harry3480 Sally2864 George2970 Helena54 Peter3480 NameAgeWeight Harry3480 Helena54 Peter3480 NameAgeWeight Helena54 Person б Age≥34 (Person) б Age=Weight (Person)

+ Project Operator Project (∏) retrieves a column. Duplication is not permitted. e.g., name of employees: ∏ name (Employee) e.g., name of employees earning more than 80,000: ∏ name ( б Salary>80,000 (Employee))

Project Operator Example NameAgeSalary Harry3480,000 Sally2890,000 George2970,000 Helena5454,280 Peter3440,000 Employee ∏ name (Employee)

Project Operator Example NameAgeSalary Harry3480,000 Sally2890,000 George2970,000 Helena5454,280 Peter3440,000 Name Harry Sally George Helena Peter Employee ∏ name (Employee)

Project Operator Example: ∏ name ( б Salary>80,000 (Employee)) NameAgeSalary Harry3480,000 Sally2890,000 George2970,000 Helena5454,280 Peter3440,000 Employee б Salary>80,000 (Employee) ∏ name (б Salary>80,000 (Employee))

Project Operator Example NameAgeSalary Harry3480,000 Sally2890,000 George2970,000 Helena5454,280 Peter3440,000 Name Sally NameAgeSalary Sally2890,000 Employee б Salary>80,000 (Employee) ∏ name (б Salary>80,000 (Employee))

+ Cartesian Product In mathematics, it is a set of all pairs of elements (x, y) that can be constructed from given sets, X and Y, such that x belongs to X and y to Y. It defines a relation that is the concatenation of every tuple of relation R with every tuple of relation S.

+ Cartesian Product Note: Relation schema is the union of schemas for R and S Resulting schema may be ambiguous Use R.A or S.A to disambiguate an attribute that occurs in both schemas

Cartesian Product Example NameAgeWeight Harry3480 Sally2864 George2970 City San Jose Austin Person City Person X City

Cartesian Product Example NameAgeWeight Harry3480 Sally2864 George2970 City San Jose Austin NameAgeWeightCity Harry3480San Jose Sally2864San Jose George2970San Jose Harry3480Austin Sally2864Austin George2970Austin Person City Person X City

+ Example AB BC D x RS

+ Example AB BC D x AR.BS.BCD RS

+ Rename Operator In relational algebra, a rename is a unary operation written as ρ a / b (R) where: a and b are attribute names R is a relation The result is identical to R except that the b field in all tuples is renamed to an a field. Example, rename operator changes the name of its input table to its subscript, ρ EmployeeName / Name (Employee) Changes the Name field of the Employee table to EmployeeName

Rename Operator Example NameSalary Harry80,000 Sally90,000 George70,000 Helena54,280 Peter40,000 EmployeeNameSalary Harry80,000 Sally90,000 George70,000 Helena54,280 Peter40,000 Employee ρ EmployeeName / Name (Employee)

+ Union Operator The union operation is denoted U as in set theory. It returns the union (set union) of two compatible relations. For a union operation r U s to be legal, we require that, r and s must have the same number of attributes. The domains of the corresponding attributes must be the same. As in all set operations, duplicates are eliminated.

Union Operator Example FNLN SusanYao RameshShah BarbaraJones AmyFord JimmyWang FNLN JohnSmith RicardoBrown SusanYao FrancisJohnson RameshShah FNLN SusanYao RameshShah BarbaraJones AmyFord JimmyWang JohnSmith RicardoBrown FrancisJohnson Student Professor Student U Professor

+ Intersection Operator Denoted as . For relations R and S, intersection is 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.

Intersection Operator Example FNLN SusanYao RameshShah BarbaraJones AmyFord JimmyWang FNLN JohnSmith RicardoBrown SusanYao FrancisJohnson RameshShah FNLN SusanYao RameshShah Student Professor Student  Professor

+ Set Difference Operator For relations R and S, Set difference R - S, defines a relation consisting of the tuples that are in relation R, but not in S. Set difference S – R, defines a relation consisting of the tuples that are in relation S, but not in R. Note: R-S  S-R!

Set Difference Operator Example FNLN SusanYao RameshShah BarbaraJones AmyFord JimmyWang FNLN JohnSmith RicardoBrown SusanYao FrancisJohnson RameshShah Student Professor Student - Professor Professor - Student

Set Difference Operator Example FNLN SusanYao RameshShah BarbaraJones AmyFord JimmyWang FNLN JohnSmith RicardoBrown SusanYao FrancisJohnson RameshShah FNLN BarbaraJones AmyFord JimmyWang Student Professor Student - Professor FNLN JohnSmith RicardoBrown FrancisJohnson Professor - Student

+ Try R  S, R  S, R - S nameaddressgenderbirthdate Carrie Fisher Mark Hamil 123 Maple St., Hollywood 456 Oak Rd., Brentwood F M 9/9/99 8/8/88 R nameaddressgenderbirthdate Carrie Fisher Harrison Ford 123 Maple St., Hollywood 789 Palm Dr., Beverly Hills F M 9/9/99 7/7/77 S

+ Sample Operations nameaddressgenderbirthdate Carrie Fisher123 Maple St., HollywoodF 9/9/99 R  S nameaddressgenderbirthdate Carrie Fisher Harrison Ford 123 Maple St., Hollywood 789 Palm Dr., Beverly Hills F M 9/9/99 7/7/77 R  S Mark Hamil456 Oak Rd., BrentwoodM 8/8/88 nameaddressgenderbirthdate R - S Mark Hamil456 Oak Rd., BrentwoodM 8/8/88

+ Natural Join Operator Natural join is a dyadic operator that is written as R lXl S where R and S are relations. The result of the natural join is the set of all combinations of tuples in R and S that are equal on their common attribute names. If r and s from r(R) and s(S) are successfully paired, result is called a joined tuple

Natural Join Example NameEmpIDDeptName Harry3415Finance Sally2241Sales George3401Finance Harriet2202Sales DeptNameMgr FinanceGeorge SalesHarriet ProductionCharles NameEmpIDDeptNameMgr Harry3415FinanceGeorge Sally2241SalesHarriet George3401FinanceGeorge Harriet2202SalesHarriet Employee Dept Employee lXl Dept For an example, consider the tables Employee and Dept and their natural join:

+ Example AB BC D join Resulting schema has attributes from R, either R or S (i.e., joining attribute(s)), and S Tuples that fail to pair with any tuple of the other relation are called dangling tuples RS

+ Example AB BC D join ABCD RS

+ Join Operations Theta Join (binary) R join C S, where C is an arbitrary join condition Step 1: take the product of R and S Step 2: Select from the product only those tuples that satisfy condition C As with the product operation, the schema for the result is the union of the schemas of R and S

+ Example BC D join A<D AND U.B  V.B AB C UV

+ Example BC D join A<D AND U.B  V.B AB C UV V.BV.CDAU.BU.C

+ Additional slides w/ other types of joins For more information of the various relational algebra operations, see: ra

+ Semijoin Operator The semijoin is joining similar to the natural join and written as R ⋉ S where R and S are relations. The result of the semijoin is only the set of all tuples in R for which there is a tuple in S that is equal on their common attribute names.

Semijoin Example NameEmpIDDeptName Harry3415Finance Sally2241Sales George3401Finance Harriet2202Sales DeptNameMgr SalesHarriet ProductionCharles NameEmpIDDeptName Sally2241Sales Harriet2202Sales Employee Dept Employee ⋉ Dept For an example consider the tables Employee and Dept and their semi join:

+ Outerjoin Operator Left outer join The left outer join is written as R =X S where R and S are relations. The result of the left outer join is the set of all combinations of tuples in R and S that are equal on their common attribute names, in addition to tuples in R that have no matching tuples in S. Right outer join The right outer join is written as R X= S where R and S are relations. The result of the right outer join is the set of all combinations of tuples in R and S that are equal on their common attribute names, in addition to tuples in S that have no matching tuples in R.

Left Outerjoin Example NameEmpIDDeptName Harry3415Finance Sally2241Sales George3401Finance Harriet2202Sales DeptNameMgr SalesHarriet NameEmpIDDeptNameMgr Harry3415Financeω Sally2241SalesHarriet George3401Financeω Harriet2202SalesHarriet Employee Dept Employee =X Dept For an example consider the tables Employee and Dept and their left outer join:

Right Outerjoin Example NameEmpIDDeptName Harry3415Finance Sally2241Sales George3401Finance Harriet2202Sales DeptNameMgr SalesHarriet ProductionCharles NameEmpIDDeptNameMgr Sally2241SalesHarriet 2202SalesHarriet ωωProductionCharles Employee Dept Employee X= Dept For an example consider the tables Employee and Dept and their right outer join:

Full Outer join Example NameEmpIDDeptName Harry3415Finance Sally2241Sales George3401Finance Harriet2202Sales DeptNameMgr SalesHarriet ProductionCharles NameEmpIDDeptNameMgr Harry3415Financeω Sally2241SalesHarriet George3401Financeω Harriet2202SalesHarriet ωωProductionCharles Employee Dept Employee =X= Dept The outer join or full outer join in effect combines the results of the left and right outer joins. For an example consider the tables Employee and Dept and their full outer join:

+ References s.htm s.htm Database System Concepts, 5 th edition, Silberschatz, Korth, Sudarshan