An Algebraic Query Language

Slides:



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

Tallahassee, Florida, 2014 COP4710 Database Systems Relational Algebra Fall 2014.
D ATABASE S YSTEMS I R ELATIONAL A LGEBRA. 22 R ELATIONAL Q UERY L ANGUAGES Query languages (QL): Allow manipulation and retrieval of data from a database.
1 Relational Algebra. Motivation Write a Java program Translate it into a program in assembly language Execute the assembly language program As a rough.
Relational Algebra Ch. 7.4 – 7.6 John Ortiz. Lecture 4Relational Algebra2 Relational Query Languages  Query languages: allow manipulation and retrieval.
CS4432: Database Systems II Query Operator & Algebraic Expressions 1.
SQL and Relational Algebra Zaki Malik September 02, 2008.
1 Relational Algebra & Calculus. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.  Relational.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 52 Database Systems I Relational Algebra.
Oct 28, 2003Murali Mani Relational Algebra B term 2004: lecture 10, 11.
Relational Algebra Relational Calculus. Relational Algebra Operators Relational algebra defines the theoretical way of manipulating table contents using.
CS 4432query processing1 CS4432: Database Systems II.
1 Relational Algebra and Calculus Yanlei Diao UMass Amherst Feb 1, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke.
Relational Algebra Chapter 4 - part I. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.  Relational.
Murali Mani Relational Algebra. Murali Mani What is Relational Algebra? Defines operations (data retrieval) for relational model SQL’s DML (Data Manipulation.
Relational Algebra.
1 Relational Algebra and Calculus Chapter 4. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.
Relational Algebra Instructor: Mohamed Eltabakh 1.
The Relational Model Part III. Remember: 3 Aspects of the Model It concerns 1) data objects storing it 2) data integrity making sure it corresponds to.
Relational Algebra (Chapter 7)
RELATIONAL ALGEBRA CHAPTER 6 1. LECTURE OUTLINE  Unary Relational Operations: SELECT and PROJECT  Relational Algebra Operations from Set Theory  Binary.
Relational Algebra – Part 2
CS 4432query processing1 CS4432: Database Systems II Lecture #11 Professor Elke A. Rundensteiner.
1.1 CAS CS 460/660 Introduction to Database Systems Relational Algebra.
From Relational Algebra to SQL CS 157B Enrique Tang.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Management Systems Chapter 4 Relational Algebra.
Rensselaer Polytechnic Institute CSCI-4380 – Database Systems David Goldschmidt, Ph.D.
Advanced Relational Algebra & SQL (Part1 )
Database Management Systems, R. Ramakrishnan1 Relational Algebra Module 3, Lecture 1.
Operations in the Relational Model COP 4720 Lecture 8 Lecture Notes.
 CS 405G: Introduction to Database Systems Lecture 6: Relational Algebra Instructor: Chen Qian.
Lu Chaojun, SJTU 1 Extended Relational Algebra. Bag Semantics A relation (in SQL, at least) is really a bag (or multiset). –It may contain the same tuple.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4, Part A.
Relational Algebra COMP3211 Advanced Databases Nicholas Gibbins
©Silberschatz, Korth and Sudarshan2.1Database System Concepts - 6 th Edition Chapter 8: Relational Algebra.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 6: Formal Relational.
Relational Algebra & Calculus
Chapter (6) The Relational Algebra and Relational Calculus Objectives
Relational Algebra Database Management Systems, 3rd ed., Ramakrishnan and Gehrke, Chapter 4.
CPSC-310 Database Systems
COMP3017 Advanced Databases
Module 2: Intro to Relational Model
CS4432: Database Systems II
COP4710 Database Systems Relational Algebra.
CS257 Query Optimization.
Relational Algebra Chapter 4 1.
Chapter 6: Formal Relational Query Languages
Chapter 2: Intro to Relational Model
Chapter 2: Intro to Relational Model
Relational Algebra Chapter 4, Part A
Relational Algebra 461 The slides for this text are organized into chapters. This lecture covers relational algebra, from Chapter 4. The relational calculus.
Relational Algebra.
LECTURE 3: Relational Algebra
Relational Algebra Chapter 4 1.
The Relational Algebra and Relational Calculus
Relational Algebra Chapter 4 - part I.
More Relational Algebra
Chapter 6: Formal Relational Query Languages
Instructor: Mohamed Eltabakh
Relational Algebra Chapter 4, Sections 4.1 – 4.2
Basic Operations Algebra of Bags
Chapter 2: Intro to Relational Model
Chapter 2: Intro to Relational Model
Chapter 2: Intro to Relational Model
Chapter 6: Formal Relational Query Languages
Example of a Relation attributes (or columns) tuples (or rows)
Chapter 2: Intro to Relational Model
CENG 351 File Structures and Data Managemnet
Relational Algebra & Calculus
Chapter 4 Relational Algebra
Presentation transcript:

An Algebraic Query Language Relational Algebra An Algebraic Query Language Lu Chaojun, SJTU

Operations on Data Recall: A data model provides structures operations constraints Relational model provides operations on relations, expressed in Relational algebra Relational calculus SQL  "syntactically sugared" RA (+RC) Lu Chaojun, SJTU

Relational Algebra Algebra operands+operatorsexpressions operands: relations relation = a set of tuples operators = set op + special relational op Lu Chaojun, SJTU

Relational Algebra Expressions Relations as operands, represented by relation names, or constants (a list of tuples) Apply operators to relations/subexpressions Define the schema and set of tuples of the resulting relation! Query = relational algebra expression Lu Chaojun, SJTU

Simplest Query Our running example: Relation name itself is a query. Student(sno,name,age,dept) Course(cno,name,credit) SC(sno,cno,grade) Relation name itself is a query. E.g. Student returns the whole relation as the result Lu Chaojun, SJTU

Set Operations Union: RS Intersection: RS Difference: RS Insertion Duplicates elimination Intersection: RS Difference: RS Deletion Compatibility condition: R and S must have schemas with identical set of attribute:type pairs. Before computation, make the order of attributes the same for R and S. 6 Lu Chaojun, SJTU

Example: Union R: S: RS: Note that one copy of (,2) is eliminated. A B A B   1 2   2 3 A B   1 2 3 Note that one copy of (,2) is eliminated. 7 Lu Chaojun, SJTU

Example: Intersection B A B   1 2   2 3 A B  2 Lu Chaojun, SJTU

Example: Difference R: S: RS: A B A B   1 2   2 3 A B   1 9 Lu Chaojun, SJTU

Projection Projected on some columns: L(R) L is a list of attributes of R E.g. name,age(Student) Duplicates elimination E.g. age(Student) Lu Chaojun, SJTU

Example: Projection R: A,C (R): A B C   10 20 30 40 1 2 A C A C   = 11 Lu Chaojun, SJTU

Selection Selection of some rows: C(R) Compute C(R): C is a conditional expression involving the attributes of R attributes, constants,=,<,>,AND,OR,NOT E.g. age>20 AND dept=‘CS’(Student) Compute C(R): Apply C to each tuple t of R by substituting t[A] for each A in C. If t makes C true, then t is in C(R). Lu Chaojun, SJTU

Example: Selection R: A=B ^ D>5 (R) A B C D     1 5 12 23 7 3 10 A B C D     1 23 7 10 13 Lu Chaojun, SJTU

Cartesian Product Combining tables: RS RR is possible. Union of attributes Use qualified name to avoid ambiguities e.g. Student.name and Course.name Pairing tuples in all possible combinations e.g. Student  Course RR is possible. It looks odd to glue unrelated tuples together, so  is not useful. Lu Chaojun, SJTU

Example: Cartesian Product R: S: RS: A B C D E   1 2    10 20 a b A B C D E   1 2    10 20 a b 15 Lu Chaojun, SJTU

Theta-Join Join: pairing tuples that match. joined tuple dangling tuple Special kind of product: R  S (RS) Use  to restrict possible pairing Example :-) name(Student Student.sno=SC.sno AND age>grade SC) Most often:  is an equation involving shared attributes of R and S. Lu Chaojun, SJTU

Example: Theta-Join R: S: R A=C S: A B C D E   1 2    10 20 a b A 17 Lu Chaojun, SJTU

Natural Join Special kind of Theta-Join: R S Example Implicitly,  is: equating shared attributes of R and S Only one copy of shared attributes is left in the resulting relation Example Student.name,Course.name,grade(Student SC Course) Lu Chaojun, SJTU

Example: Natural Join R: S: R S: A B C D B D E     1 2 4    a b 3 2 a c b      A B C D E   1 2    a c    19 Lu Chaojun, SJTU

Naming and Renaming Control the names of relations/attributes S(R) S(A1,…,An)(R) S(expression) S(A1,…,An)(expression) Lu Chaojun, SJTU

Combining Operations Apply op. to subexpressions. More than one way to write a query query optimizer Operator precedence Unary ops have highest precedence: , ,  “Multiplicative” ops are next : ,  , “Additive” ops are lowest: , ,  Use parentheses ! Expression tree Lu Chaojun, SJTU

Quotient T = RS Defined for R(X,Y) and S(Y) T(X) T = {t | t  X(R) AND sS(ts  R)} Lu Chaojun, SJTU

Example: Quotient(1) R: S: RS: A B B A 1 2        1 2 3 4 6 23 Lu Chaojun, SJTU

Example: Quotient(2) R: S: RS: A B C D E D E    a    a b 1 3 a 24 Lu Chaojun, SJTU

Example: Quotient(3) Find those students who have selected all the courses. sno,cno(SC)  cno(Course) Find those students who have selected at least the courses that S007 has taken. sno,cno(SC)  cno(sno=‘S007’(SC)) Exercise 2.4.10 RS = X(R)  X((X(R)  S)  R) Lu Chaojun, SJTU

Semijoin R S R’s tuples that agree with at least one tuple in S on all shared attributes Exercise 2.4.8 Equivalent expressions: (1) R(R S) (2) ? (3) ? Lu Chaojun, SJTU

Example: Semijoin R: S: R S: A B C D B D E     1 2 4    a b c 1 3 2 a c b      A B C D   1 2    a c 27 Lu Chaojun, SJTU

Independent Set of Operators Some op. can be expressed in terms of others. Independent set of operators: {, , , , , } R  S = R  (R  S) R C S = C (R  S) R S = L (C (R  S)) C: equating shared attributes L: eliminating duplicate attributes Lu Chaojun, SJTU

Assignment A linear notation for complex RA expressions R(a,b,c) := exp. R is a temporary relation Answer(...) for the resulting relation Example R(no,n,a,d) := dept=‘CS’ (Student) Answer(sno,name) := no,n(R) Lu Chaojun, SJTU

RA as a Constraint Language Two ways to express constraints equal-to-the-emptyset R =  set-containment R  S The two ways are equivalent: R =  iff R   R  S iff RS =  Lu Chaojun, SJTU

Referential Integrity Constraints The object/entity referenced by another object/entity must really exist. Example In SC(sno,cno,grade), if there’s a tuple ('001','CS123',90), then 'CS123' must exist in Course. We can express this constraint by cno(SC)  cno(Course) Lu Chaojun, SJTU

Key Constraint Example: Student(sno,name,age,dept) No two tuples agree on sno component. S1.sno=S2.sno AND S1.deptS2.dept(S1 S2) =  where S1 and S2 are the results of renaming Student: S1(Student) S2(Student) Similar for other attributes. Lu Chaojun, SJTU

Some Other Constraints Domain constraint: permitted values for an attribute. Example age<0 OR age>200(Student) =  More complex constraints age<18 AND cno=‘C1’ (Student SC) =  Lu Chaojun, SJTU

End