Relational Algebra.

Slides:



Advertisements
Similar presentations
Practice for SQL and Constraints (Product-PC-Laptop-Printer)
Advertisements

1 Relational Algebra* and Tuple Calculus * The slides in this lecture are adapted from slides used in Standford's CS145 course.
Relational Algebra Rohit Khokher. Relational Algebra Set Oriented Operations UnionIntersectionDifference Cartesian Product Relation Oriented Operations.
Algebraic and Logical Query Languages Spring 2011 Instructor: Hassan Khosravi.
SQL and Relational Algebra Zaki Malik September 02, 2008.
Functional Dependencies - Example
Midterm Review II. Redundancy. –Information may be repeated unnecessarily in several tuples. –E.g. length and filmType. Update anomalies. –We may change.
Relational Operations on Bags Extended Operators of Relational Algebra.
Database Modifications, Data Types, Views. Database Modifications A modification command does not return a result as a query does, but it changes the.
Query Compiler. The Query Compiler Parses SQL query into parse tree Transforms parse tree into expression tree (logical query plan) Transforms logical.
Database Modifications, Data Types, Views. Database Modifications A modification command does not return a result as a query does, but it changes the.
Subqueries Example Find the name of the producer of ‘Star Wars’.
Indexes. An index on an attribute A of a relation is a data structure that makes it efficient to find those tuples that have a fixed value for attribute.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 52 Database Systems I Relational Algebra.
Query Compiler By:Payal Gupta Roll No:106(225) Professor :Tsau Young Lin.
Oct 28, 2003Murali Mani Relational Algebra B term 2004: lecture 10, 11.
Closure The closure of {B 1 …B k } under the set of FDs S, denoted by {B 1 …B k } +, is defined as follows: {B 1 …B k } + = {B | any relation satisfies.
SQL. 1.SQL is a high-level language, in which the programmer is able to avoid specifying a lot of data-manipulation details that would be necessary in.
The principal problem that we encounter is redundancy, where a fact is repeated in more than one tuple. Most common cause: attempts to group into one relation.
Operations in the Relational Model These operation can be expressed in an algebra, called “relational algebra”. In this algebra relations are the operands.
Relational Operations on Bags Extended Operators of Relational Algebra.
SQL SQL is a very-high-level language, in which the programmer is able to avoid specifying a lot of data-manipulation details that would be necessary in.
Joins Natural join is obtained by: R NATURAL JOIN S; Example SELECT * FROM MovieStar NATURAL JOIN MovieExec; Theta join is obtained by: R JOIN S ON Example.
Chapter 5 Algebraic and Logical Query Languages pp.54 is added Pp 61 updated.
Nov 18, 2003Murali Mani Relational Algebra B term 2004: lecture 10, 11.
Midterm Review/Practice for SQL and Constraints. Exercise – Suppliers and Parts Suppliers(sid,sname,address) Parts(pid,pname,color) Catalog(sid, pid,price)
SQL By: Toan Nguyen. Download Download the software at During the installation –Skip sign up for fast installation.
Relational Algebra Basic Operations Algebra of Bags.
Relational Algebra CIS 4301 Lecture Notes Lecture /28/2006.
Relational Algebra Spring 2012 Instructor: Hassan Khosravi.
Introduction to Data Manipulation in SQL CIS 4301 Lecture Notes Lecture /03/2006.
Introduction to Indexes. Indexes An index on an attribute A of a relation is a data structure that makes it efficient to find those tuples that have a.
Relational Algebra (Chapter 7)
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Relational Algebra.
1.1 CAS CS 460/660 Introduction to Database Systems Relational Algebra.
Databases : Relational Algebra 2007, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof. Jeffrey D. Ullman distributes.
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.
Rensselaer Polytechnic Institute CSCI-4380 – Database Systems David Goldschmidt, Ph.D.
CSCD34-Data Management Systems - A. Vaisman1 Relational Algebra.
© D. Wong Normalization  Purpose: process to eliminate redundancy in relations due to functional or multi-valued dependencies.  Decompose relation.
More Relation Operations 2015, Fall Pusan National University Ki-Joune Li.
More Relation Operations 2014, Fall Pusan National University Ki-Joune Li.
Operations in the Relational Model COP 4720 Lecture 8 Lecture Notes.
1 Algebra of Queries Classical Relational Algebra It is a collection of operations on relations. Each operation takes one or two relations as its operand(s)
603 Database Systems Senior Lecturer: Laurie Webster II, M.S.S.E.,M.S.E.E., M.S.BME, Ph.D., P.E. Lecture 10 A First Course in Database Systems.
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 11 A First Course in Database Systems.
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.
The Relational Model of Data Prof. Yin-Fu Huang CSIE, NYUST Chapter 2.
CS 157B Database Systems Dr. T Y Lin. 1.2 Overview of a Database Management System Data-Definition Language Commands –Illustrated by three examples.
Functional Dependencies. Babies Exercise 2.2.5: At a birth, there is one baby (twins would be represented by two births), one mother, any number of nurses,
© D. Wong Functional Dependencies (FD)  Given: relation schema R(A1, …, An), and X and Y be subsets of (A1, … An). FD : X  Y means X functionally.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4, Part A.
Databases : SQL Multi-Relations 2007, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof. Jeffrey D. Ullman.
© D. Wong Ch. 3 (part 1)  Relational Model basics  From E/R diagram to Relations.
1 Database Design: DBS CB, 2 nd Edition Relational Algebra: Basic Operations & Algebra of Bags Ch. 5.
1. Chapter 2: The relational Database Modeling Section 2.4: An algebraic Query Language Chapter 5: Algebraic and logical Query Languages Section 5.1:
©Silberschatz, Korth and Sudarshan2.1Database System Concepts - 6 th Edition Chapter 8: Relational Algebra.
Subqueries CIS 4301 Lecture Notes Lecture /23/2006.
Basic Operations Algebra of Bags
An Algebraic Query Language
16.2.Algebraic Laws for Improving Query Plans
THE RELATIONAL MODEL OF DATA
Chap 2. The Relational Model of Data
More Relational Algebra
More Relation Operations
Basic Operations Algebra of Bags
Algebraic and Logical Query Languages pp.54 is added
Query Compiler By:Payal Gupta Shirali Choksi Professor :Tsau Young Lin.
Presentation transcript:

Relational Algebra

Operations in the Relational Model These operation can be expressed in an algebra, called “relational algebra.” In this algebra, relations are the operands and we apply operators on them.

Operations Four broad classes: Usual set operations union intersection difference Operations that remove parts of a relation: selection eliminates some rows(tuples) projection eliminates some columns Operations that combine the tuples of two relations: Cartesian product pairs tuples of two relations in all possible ways join selectively pairs tuples from two relations. An operation called “renaming”.

Conditions for Set Operations on Relations We can apply union, intersection, difference- on relations R and S provided that: R and S must have schemas with identical sets of attributes. Before applying the operations, the columns of R and S must be ordered so that the order of attributes is the same for both relations.

Set Operations on Relations R  S, the union of R and S, is the set of tuples that are in R or S or both. R  S, the intersection of R and S, is the set of tuples that are in both R and S. R  S, the difference of R and S, is the set of tuples that are in R but not in S. Note that R  S is different from S  R.

Projection A1,…,An(R) Produces from relation R a new relation that has only the A1, …, An columns of R. Example: For title, year, length(Movies) on: title year length filmType studioName producerC# Star wars 1977 124 color Fox 12345 Mighty Ducks 1991 104 color Disney 67890 Wayne’s World 1992 95 color Paramount 99999

Example (Continued) We get: title year length Star wars 1977 124 Mighty Ducks 1991 104 Wayne’s World 1992 95 What about filmtype(Movies) ?

Selection C( R ) Produces a new relation with those tuples of R which satisfy condition C. Example: For length100(Movie) we have as result: title year length filmType studioName producerC# Star wars 1977 124 color Fox 12345 Mighty Ducks 1991 104 color Disney 67890

Another Example Suppose we want the movies by Fox which are at least 100 minutes long. length100 And studioName=’Fox’(Movie) Result is: title year length filmType studioName producerC# Star wars 1977 124 color Fox 12345

Cartesian Product RS Set of tuples rs that are formed by choosing the first part (r) to be any tuple of R and the second part (s) to be any tuple of S. Schema for the resulting relation is the union of schemas for R and S. If R and S happen to have some attributes in common, then prefix those attributes by the relation name. Example: R A B S B C D 1 2 2 5 6 3 4 4 7 8 9 10 11

Example (Continued) Resulting relation will be: RS A R.B S.B C D 1 2 2 5 6 1 2 4 7 8 1 2 9 10 11 3 4 2 5 6 3 4 4 7 8 3 4 9 10 11

Attributes with the same name have only one representative. Why? Natural Join R S Let A1, A2,…,An be the attributes in both the schema of R and the schema of S. Then a tuple r from R and a tuple s from S are successfully paired if and only if r and s agree on each of the attributes A1, A2, …, An. Example: The natural join of the relation R and S from previous example is: A B C D 1 2 5 6 3 4 7 8 Attributes with the same name have only one representative. Why?

Theta-Join R C S. The result of this operation is constructed as follows: Take the Cartesian product of R and S. Select from the product only those tuples that satisfy the condition C. Schema for the result is the union of the schema of R and S with, “R” or “S” prefix as necessary.

Example Compute the natural and theta join for relations U and V: A B C B C D 1 2 3 2 3 4 6 7 8 2 3 5 9 7 8 7 8 10 Relation U Relation V U V and U A<D V

Example A B C D A U.B U.C V.B V.C D 1 2 3 4 1 2 3 2 3 4 1 2 3 4 1 2 3 2 3 4 1 2 3 5 1 2 3 2 3 5 6 7 8 10 1 2 3 7 8 10 9 7 8 10 6 7 8 7 8 10 Result U V 9 7 8 7 8 10 Result of U A<D V

Combing Operations to Form Queries “What are the title and years of movies made by Fox that are at least 100 minutes long?” One way to compute the answer to this query is: Select those Movie tuples that have length  100. Select those Movie tuples that have studioName =‘Fox’. Compute the intersection of first and second steps. Project the relation from the third step onto attributes title and year.

Example (Continued)  title,year( length100(Movie)   studioName=‘Fox’(Movie))  title,year   length100  studioName=‘Fox’ Movie Movie

Another Example Consider two relations Movie1 and Movie2, With schemas: Movie1(title, year, length, filmType, studioName) Movie2(title, year, starName) Suppose we want to know: “Find the stars of the movies that are at least 100 minutes long.” First we join the two relations: Movie1, Movie2 Second we select movies with length at least 100 min. Then we project onto starName.

Renaming Operator S(A1,A2,…,An) (R) Resulting relation has exactly the same tuples as R, but the name of the relation is S. Moreover, the attributes of the result relation S are named A1, A2, …, An, in order from the left.

Problem Product(maker, model, type) PC(model, speed, ram, hd, rd, price) Laptop(model, speed, ram, hd, screen, price) Printer(model, color, type, price) (Exercise 5.2.1)

Problem a) What PC models have a speed of at least 1000? b) Which manufacturers make laptops with a hard disk of at least one gigabyte? c) Find the model number and price of all products (of any type) made by manufacturer B. d) Find the model numbers of all color laser printers. e) Find those manufacturers that sell Laptops, but not PC's. !f) Find those hard-disk sizes that occur in two or more PC's. !g) Find those pairs of PC models that have both the same speed and RAM. A pair should be listed once. !!h)Find those manufacturers of at least two different computers (PC or Laptops) with speed of at least 700.