Operations in the Relational Model These operation can be expressed in an algebra, called “relational algebra”. In this algebra relations are the operands.

Slides:



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

พีชคณิตแบบสัมพันธ์ (Relational Algebra) บทที่ 3 อ. ดร. ชุรี เตชะวุฒิ CS (204)321 ระบบฐานข้อมูล 1 (Database System I)
Algebraic and Logical Query Languages Spring 2011 Instructor: Hassan Khosravi.
INFS614, Fall 08 1 Relational Algebra Lecture 4. INFS614, Fall 08 2 Relational Query Languages v Query languages: Allow manipulation and retrieval of.
SQL and Relational Algebra Zaki Malik September 02, 2008.
Functional Dependencies - Example
Relational Algebra.
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.
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.
Relational Algebra on Bags A bag is like a set, but an element may appear more than once. –Multiset is another name for “bag.” Example: {1,2,1,3} is a.
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.
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.
Rutgers University Relational Algebra 198:541 Rutgers University.
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)
Relational Algebra.
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.
The Relational Algebra and Calculus
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Management Systems Chapter 4 Relational Algebra.
Database Management Systems 1 Raghu Ramakrishnan Relational Algebra Chpt 4 Xin Zhang.
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.
Advanced Relational Algebra & SQL (Part1 )
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.
Databases : Relational Algebra - Complex Expression 2007, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof.
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.
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.
CPSC 603 Database Systems Lecturer: Laurie Webster II, M.S.S.E., M.S.E.E., M.S.BME, Ph.D., P.E. Lecture 5 Introduction to a First Course in Database Systems.
The Relational Model of Data Prof. Yin-Fu Huang CSIE, NYUST Chapter 2.
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.
© D. Wong Ch. 3 (part 1)  Relational Model basics  From E/R diagram to Relations.
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
Relational Algebra at a Glance
THE RELATIONAL MODEL OF DATA
Chap 2. The Relational Model of Data
Operators Expression Trees Bag Model of Data
The Relational Algebra and Relational Calculus
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:

Operations in the Relational Model These operation can be expressed in an algebra, called “relational algebra”. In this algebra relations are the operands and by applying any of operators to be described later, we can build progressively more complex expressions.

Operations Operations of relational algebra fall into four broad classes: 1.The usual set operations union intersection difference 2.Operations that remove parts of a relation: selection eliminates some rows(tuples) projection eliminates some columns 3.Operations that combine the tuples of two relations: Cartesian product, which pairs the tuples of two relations in all possible ways join” operation, which selectively pair tuples from two relations. 4.An operation called “renaming”.

Conditions for Set Operations on Relations We can apply three most common operations on sets- union, intersection, difference- to relations provided that these two conditions hold: 1.R and S must have schemas with identical sets of attributes. 2.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 elements that are in R or S or both. R  S, the intersection of R and S, is the set of elements that are in both R and S. R  S, the difference of R and S, is the set of elements that are in R but not in S. Note that R  S is different from S  R.

Projection Produces from a relation R a new relation that has only some of R’s columns.  A1, A2,…,An (R) is a relation that has only the columns for attributes A 1, A 2,…, A n of R. Example: Compute the expression  title, year, length (Movies) on the table: title yearlengthfilmType studioName producerC# Star wars color Fox Mighty Ducks color Disney Wayne’s World color Paramount 99999

Example (Continued) Resulting relation: titleyearlength Star wars Mighty Ducks Wayne’s World What about  filmtype (Movies)

Selection The selection operator, applied to a relation R, produces a new relation with a subset of R’s tuples. The tuples in the result are those that satisfy some condition C. We denote this operation  C ( R ). The schema for the resulting relation is the same as R’s schema. Example: The expression  length  100 (Movie) is: title yearlengthfilmType studioName producerC# Star wars color Fox Mighty Ducks color Disney 67890

Example Suppose we want the set of tuples in the previous relation Movie such that represent Fox movies at least 100 minutes long. We can get these with a more complicated condition:  length  100 And studioName=’Fox’ (Movie) The tuple title yearlengthfilmType studioName producerC# Star wars color Fox Is the only one in the resulting relation.

Cartesian Product The Cartesian Product (or just product) of two sets R and S is the set of pairs that can be formed by choosing the first element of the pair to be any element of R and the second an element of S. This product is denoted as R  S. The relation schema for the resulting schema is the union of the schemas for R and S. However, if R and S should happen to have some attributes in common, then we need to invent new names for at least one of each pair identical attributes. Example: RA BSB C D

Example(Continued) The resulting relation will be: R  S AR.BS.BCD

Natural Join Denoted as R S. Let A 1, A 2,…,A n 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 A 1, A 2, …, A n. Example: The natural join of the relation R and S from previous example is: ABCD

Theta-Joins R C S. The result of this operation is constructed as follows: Take the 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 two relations U and V: ABCB CD Relation U Relation V

Example A BC D AU.B U.C V.B V.C D Result U V 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

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 the starName.

Renaming Operator In order to control the names of the attributes used for relations that are constructed by one or more applications of the relational algebra operations, it is often convenient to use an operator that explicitly renames relations. We shall use the operator  S(A1,A2,…,An) (R) to rename a relation R. The 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 A 1, A 2, …, A n, 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.