Relational Algebra Relational Operators – Set operations –Special relational operators Relational Algebra Expressions PROJECT (SELECT FROM (A TIMES B)…

Slides:



Advertisements
Similar presentations
Relational Database Operators
Advertisements

COMP 5138 Relational Database Management Systems Semester 2, 2007 Lecture 5A Relational Algebra.
Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
Relational Algebra Jianlin Feng School of Software SUN YAT-SEN UNIVERSITY courtesy of Joe Hellerstein for some slides.
Relational Algebra Rohit Khokher. Relational Algebra Set Oriented Operations UnionIntersectionDifference Cartesian Product Relation Oriented Operations.
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.
1 Relational Algebra & Calculus. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.  Relational.
Relational Algebra 1 Chapter 5.1 V3.0 Napier University Dr Gordon Russell.
By relieving the brain of all unnecessary work, a good notation sets it free to concentrate on more advanced problems, and, in effect, increases the mental.
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.
By relieving the brain of all unnecessary work, a good notation sets it free to concentrate on more advanced problems, and, in effect, increases the mental.
By relieving the brain of all unnecessary work, a good notation sets it free to concentrate on more advanced problems, and, in effect, increases the mental.
FALL 2004CENG 351 File Structures and Data Managemnet1 Relational Algebra.
By relieving the brain of all unnecessary work, a good notation sets it free to concentrate on more advanced problems, and, in effect, increases the mental.
Relational Algebra Relational Calculus. Relational Algebra Operators Relational algebra defines the theoretical way of manipulating table contents using.
1 Relational Algebra. 2 Relational Query Languages Query languages: Allow manipulation and retrieval of data from a database. Relational model supports.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4, Part A.
Introduction to Database Systems 1 Relational Algebra Relational Model: Topic 3.
By relieving the brain of all unnecessary work, a good notation sets it free to concentrate on more advanced problems, and, in effect, increases the mental.
1 Relational Algebra and Calculus Yanlei Diao UMass Amherst Feb 1, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke.
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.
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.
Lecture 05 Structured Query Language. 2 Father of Relational Model Edgar F. Codd ( ) PhD from U. of Michigan, Ann Arbor Received Turing Award.
Chapter 2 Adapted from Silberschatz, et al. CHECK SLIDE 16.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 The Relational Algebra.
1 The Relational Database Model. 2 Learning Objectives Terminology of relational model. How tables are used to represent data. Connection between mathematical.
M Taimoor Khan Course Objectives 1) Basic Concepts 2) Tools 3) Database architecture and design 4) Flow of data (DFDs)
9/7/2012ISC329 Isabelle Bichindaritz1 The Relational Database Model.
Relational Algebra (Chapter 7)
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.
CS 4432query processing1 CS4432: Database Systems II Lecture #11 Professor Elke A. Rundensteiner.
1 Relational Algebra & Calculus Chapter 4, Part A (Relational Algebra)
1.1 CAS CS 460/660 Introduction to Database Systems Relational Algebra.
Database Management Systems 1 Raghu Ramakrishnan Relational Algebra Chpt 4 Xin Zhang.
Relational Algebra.
1 Relational Algebra Chapter 4, Sections 4.1 – 4.2.
Chapter 6 The Relational Algebra Copyright © 2004 Ramez Elmasri and Shamkant Navathe.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Management Systems Chapter 4 Relational Algebra.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 6- 1.
Al-Maarefa College for Science and Technology INFO 232: Database systems Chapter 3 “part 2” The Relational Algebra and Calculus Instructor Ms. Arwa Binsaleh.
Relational Algebra MBAD 613 R. Nakatsu. Relational Data Manipulation Language Query-by-Example; Query-by-Form Transform-Oriented Languages Relational.
Advanced Relational Algebra & SQL (Part1 )
Chapter 2: Intro to Relational Model. 2.2 Example of a Relation attributes (or columns) tuples (or rows)
Database Management Systems, R. Ramakrishnan1 Relational Algebra Module 3, Lecture 1.
1 CS 430 Database Theory Winter 2005 Lecture 5: Relational Algebra.
CMPT 258 Database Systems Relational Algebra (Chapter 4)
Relational Algebra p BIT DBMS II.
CHAPTER 2 : RELATIONAL DATA MODEL Prepared by : nbs.
Chapter 6 The Relational Algebra Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
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.
Relations, Relational database Idea of relation Relational data base From the diagram ERD to database schema.
Ritu CHaturvedi Some figures are adapted from T. COnnolly
Chapter (6) The Relational Algebra and Relational Calculus Objectives
Relational Algebra Database Management Systems, 3rd ed., Ramakrishnan and Gehrke, Chapter 4.
Chapter 2: Relational Model
Chapter 2: Intro to 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.
Relational Algebra.
LECTURE 3: Relational Algebra
More Relational Algebra
Presentation transcript:

Relational Algebra Relational Operators – Set operations –Special relational operators Relational Algebra Expressions PROJECT (SELECT FROM (A TIMES B)…

Relational Algebra Relational Algebra Set of operators that can be applied to database relations Set operatorsSpecial relational operators *Union *Difference Intersection *Cross-product *Selection *Projection Joins Operators signed by (*) are called primitive. It means that any one of them can not be expressed by any combination of other operators. The result of any operator of relational algebra is a relation. Thus relational algebra is shut. It means, we can build any number of nested relational expressions. For example, perform projection of union, selection of joins, joins of a result of some selections.

TNOTNAMETITLECITYSUPNO T1BlakeProfLondonNull T2SmithPhDGlasgowT1 T3JonesProfLondonT1 T4ClarkPhDLiverpoolT3 T5AdamsMScBristolT4 TEACHERS An example of tables for the database School1

SNOSNAMESYEARCITY S1Henry1975London S2Jones1980Davos S3Johnson1983Dublin S4Higgins1984London S5Ford1990Bristol S6Hopkins1990Adelaide STUDENTS An example of tables for the database School1

TNOSNOHOURS T1S164 T1S264 T2S2100 T2S3120 T2S4120 T3S132 T3S236 T3S360 T3S572 T4S196 T4S396 T4S596 T5S632 An example of tables for the database School1 TS

Set Operations - Union Definition Two relations are said to be compatible if the following conditions hold: –they have the same degree n, (the same number of the fields), and –corresponding fields, taken in order from left to right, have the same domains. R UNION S (where R and S are compatible): returns a relation instance containing all tuples that occur in either relation instance R or relation instance S (or both) Examples R – Set of teachers teaching student S5 S - Set of teachers teaching student S6 R UNION S – Set of teachers who teach student S5 or student S6 R S R UNION S If R = (r1, r2, r3) S=(r1,r2,r3.r4) R U S= (r1, r2, r3, r4)

Set Operations - Difference R MINUS S (where R and S are compatible): returns a relation instance containing all tuples that occur in R but not in S. Examples R – Set of teachers teaching student S5 S - Set of teachers teaching student S6 R MINUS S – Set of teachers who teach student S5 but do not teach student S6 R S R MINUS S R - S If R = (r1, r2, r3, r5) S=(r1,r2,r3.r4) R - S= r5 S – R= r4

Set Operations - Intersection R INTERSECT S (where R and S are compatible): returns a relation instance containing all tuples that occur in both R and S. Examples R – Set of teachers teaching student S5 S - Set of teachers teaching student S6 R INTERSECT S – Set of teachers who teach both students S5 and student S6 R S R ∩ S If R = (r1, r2, r3, r5) S=(r1,r2,r3.r4) R ∩ S= r1, r2, r3 S ∩ R= r1, r2, r3 Note that Intersection is not a primitive operator, because: R ∩ S = R – (R – S) = S – (S – R)

Set Operations – Cross-Product R TIMES S : returns a relation instance whose schema contains all the fields of R ( in the same order as they appear in R) followed by all fields of S ( in the same order as they appear in S). The result of R X S contains one tuple (r,s) for each pair of tuples r Є R, s Є S. The cross-product operation is sometimes called Cartesian product. The degree of the new relation resulting from the operation R X S is the sum of degrees of two relations R and S Examples R – Set of all identifiers of teachers S - Set of all identifiers of students R TIMES S – Set of all pairs (ident-teachers, ident-students): {(T1,S1), (T1,S2),…,(T1,S6), (T2,S1),…(T2,S6),…(T5,S1),…,(T5,S6)} ABCABC XyXy AX AY BX BY CX CY product R TIMES S R S

Set Operations – Cross-Product Cross-product using the convention that the fields of R X S inherit names from the corresponding fields of R and S. It is possible for both R and S to contain one or more fields having the same names. This situation creates a naming conflict. E.g. The result of this operation: TEACHERS TIMES STUDENTS have two attributes with the same name CITY. In this situation we use the convention using names of their relations: TEACHERS.CITY, STUDENTS.CITY Therefore, the schema of the relation TEACHERS TIMES STUDENTS has the form: (TNO, TNAME, TITLE, TEACHERS.CITY, SUPNO, SNO, SNAME, SYEAR, STUDENTS.CITY) For better legibility, we can use this convention even if there is no naming conflict, e.g.: (TEACHERS.TNO, TEACHERS.TNAME,…)

Special Relational Operators: Projection How to extract only specified attributes from a relation? For this aim, we use projection operator (PROJECT): A1A2A3B1B A1A2B C PROJECT C OVER A1,A2,B1 D

Special Relational Operators: Projection ABCDE Let A 1, A 2, …, A k be attributes of the relation R PROJECT R OVER A 1, A 2, …, A k : Contains all tuples (A 1,A 2,…A k ) such, that in the relation R a tuple t exists, which as a value of A 1 has a 1, as a value of A 2 has a 2, …, as a value of A k has a k R PROJECT R OVER B, D “vertical” subset from the relation R Note that, as the result of PROJECTION definition, the duplicate tuples are eliminated e.g. PROJECT TEACHERS OVER CITY CITY London Glasgow Liverpool Bristol

Special Relational Operators: SELECTION How to extract only specified tuples from a relation? For this aim, we use selection operator (SELECT): A1A2A3B1B C SELECT FROM C WHERE A1 < 12 A1 A2A3B1B E

Special Relational Operators: Selection ABCDE SELECT R WHERE condition contains all tuples t from the relation R, such that for all t the condition has the value TRUE R SELECT R WHERE… “horizontal” subset from the relation R Example SELECT STUDENTS WHERE SYEAR > 1983 In general, the selection condition is a boolean combination using one of the following operators: =, or > SNOSNAMESYEARCITY S4Higgins1984London S5Ford1990Bristol S6Hopkins1990Adelaide

Special Relational Operators: Joins A1A2A B1B A1A2A3B1B How to combine information from two or more relations? E.g. how to add “Names” of “Clients” to the relation “Ordering”? For this aim, we use JOIN operator (JOIN) A JOIN B ON A1=B2 A B C

Special Relational Operators: Joins TNOTNAMETITLETEACHERS. CITY SUPNOSNOSNAMESYEA R STUDENTS. CITY T1BlakeProf.LondonNullS4Higgins1984London T4ClarkPhDLondonT1S4Higgins1984London T5AdamsMScBristolT4S5Fords1990Bristol R JOIN S WHERE condition: contains all tuples t, such that t is concatenation of the tuple r from relation R and tuple s from relation S, and for each tuple t, the condition has TRUE value. JOIN can be also defined as a cross-product followed by selection and projections R JOIN S WHERE condition= SELECT (R TIMES S) WHERE condition Example TEACHERS JOIN STUDENTS WHERE TEACHERS.CITY = STUDENTS.CITY AND SYEAR > 1983

Special Relational Operators: Joins JOINS can be used for more tables: R JOIN S JOIN T JOIN … WHERE condition= SELECT (R TIMES S TIMES T TIMES …) WHERE condition Usually, joins are used together with projection, otherwise the result could consist of a large number of columns When the join condition consists solely of equalities of the form R.name1 = S.name2, that is equalities between two fields in R and S,  there is some redundancy in retaining both attributes in the result. For join conditions that contain only such equalities, the joint operation is refined by doing an additional projection in which S.name2 is dropped. The joint operation with this refinement is called equijoin. The schema of the result of an equijoin contains the fields of R followed by the fields of S that do not appear in the join condition.

PROJECT (TEACHERS JOIN STUDENTS WHERE TEACHERS.CITY =STUDENTS.CITY AND SYEAR > 1983) OVER TNO, TNAME, TITLE, TEACHERS.CITY, SUPNO, SNO, SNAME, SYEAR TNOTNAMETITLETEACHERS. CITY SUPNOSNOSNAMESYEA R T1BlakeProf.LondonNullS4Higgins1984 T4ClarkPhDLondonT1S4Higgins1984 T5AdamsMScBristolT4S5Fords1990 Example

Special Relational Operators: Joins TS.SNOSNAMEHOURS S1Henry32 S2Jones36 S3Johnson60 S5Ford72 Example PROJECT (TS JOIN STUDENTS WHERE TS.SNO = STUDENTS.SNO AND TS.TNO = “T3”) OVER TS.SNO, SNAME, HOURS Joins according to equality of such attributes are called equijoin Equijoin allow complete – linked together- information from different relations The result shows students, who had lectures with the teacher T3. The attributes TS.SNO, STUDENTS.SNO have a role of a connecting link between relations TS and STUDENTS (TS.SNO is a foreign key related to the relation STUDENTS).

TS.TNOTS.SNOHOURSSTUDENTS.SNOSNAMESYEARCITY … … T3 S132S1Henry1975London T3 S236S2Jones1980Davos T3 S360S3Johnson1983Dublin T3 S572S5Ford1990Bristol... … TS.SNO = STUDENTS.SNO TS.TNO=T3

Special Relational Operators: Joins Example SELECT (PROJECT (TS JOIN STUDENTS JOIN TEACHERS WHERE TS.SNO=STUDENTS.SNO AND TS.TNO=TEACHERS.TNO) OVER TS.SNO, SNAME, TS.TNO, TNAME, HOURS) WHERE HOURS > 95 TS.SNOSNAMETS.TNOTNAMEHOURS S2JonesT2Smith100 S3JohnsonT2Smith120 S4HigginsT2Smith120 S1HenryT4Clark96 S3JohnsonT4Clark96 S5FordT4Clark96 Result shows all pairs (student, teacher) such, that student and teacher have spent together on subjects more than 95 hours. Moreover, number of spent hours is shown.

TNOTNAMETITLECITYSUPNO T1BlakeProfLondonNull T2SmithPhDGlasgo w T1 T3JonesProfLondonT1 T4ClarkPhDLiverpo ol T3 T5AdamsMScBristolT4 TNOSNOHOURS T1S164 T1S264 T2S2100 T2S3120 T2S4120 T3S132 T3S236 T3S360 T3S572 T4S196 T4S396 T4S596 T5S632 SNOSNAMESYEARCITY S1Henry1975London S2Jones1980Davos S3Johnson1983Dublin S4Higgins1984London S5Ford1990Bristol S6Hopkins1990Adelaide = = TEACHE RS.TNO TNAMETITLETEACHE RS.CITY SUPNOTS.TNOTS.SNOHOURSSTUDEN TS.SNO SNAMESYEARSTUDEN TS.CITY T1BLAKEPROFLONDONNULLT1S164S1HENRY1975LONDON

Joins, Projection and Selection - example SELECT FROM ( PROJECT (Clients JOIN Ordering ON Clients.NO = Ordering.Client) OVER NO, Name, No-Ord, Date) WHERE Date >= ’ ’ NONameNo-OrdDate Ahmad Ahmad Said Said Ahmad Kemal Kemal SELECT NO,Name, No-Ord, Date FROM Clients, Ordering WHERE Clients.No = Ordering.Client AND Date >=’ ’’ SELECT NO, Name, No-Ord, Date FROM Clients JOIN Ordering ON Clients.No = Ordering.Client WHERE Date >= ’

Relational database - example NoNameStatus Ahmad Said Ali Kemal90 Ord- No DateClientWholes SMALL NEW ONLY HERE SMALL ONLY HERE ABDATE ABC AAA Clients Ordering An example of tables Clients and Ordering from the relations of the previous database NONameNo-OrdDate Ahmad Ahmad Said Said Ahmad Kemal Kemal From previous slide