Algebra1 After this lecture, you should be able to:  Understand the differences between SQL (Structured Query Language) and Relational Algebra expressions.

Slides:



Advertisements
Similar presentations
Database Languages Chapter 7. The Relational Algebra.
Advertisements

Unit 5 The Network Model  5.1 The Network Model  5.2 IDMS.
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.
Chapter 3 An Introduction to Relational Databases.
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 (II) Prof. Sin-Min LEE Department of Computer Science.
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.
CS 4432query processing1 CS4432: Database Systems II.
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.
Domains, Relations & Base RelVars (Ch
RELATIONAL ALGEBRA (II) Prof. Sin-Min LEE Department of Computer Science.
Relational Algebra Chapter 4 - part I. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.  Relational.
RELATIONAL ALGEBRA (II) Prof. Sin-Min LEE Department of Computer Science.
CSCD343- Introduction to databases- A. Vaisman1 Relational Algebra.
Relational Algebra.
Data Manipulation 11 After this lecture, you should be able to:  Understand the differences between SQL (Structured Query Language) and other programming.
Relational Algebra II. Semantics - where A WHERE X  Y –  : an operator like =, >, … Result of restriction A by the condition X  Y is A relation –with.
6.8 Case Study: E-R for Supplier-and-Parts Database
1 Exercises of SQL uAnswer the following questions, based on the database below. wSupplier (SNO, SNAME, STATUS, CITY) wPart(PNO, PNAME, COLOR, WEIGHT)
3-1 Unit 3 The Relational Model. 3-2 Wei-Pang Yang, Information Management, NDHU Outline  3.1 Introduction  3.2 Relational Data Structure  3.3 Relational.
3-1 Unit 3 The Relational Model. 3-2 Wei-Pang Yang, Information Management, NDHU Outline  3.1 Introduction  3.2 Relational Data Structure  3.3 Relational.
1 Structured Query Language (SQL) CIS*2450 Advanced Programming Concepts.
Logical Database Design ( 補 ) Unit 7 Logical Database Design ( 補 )
Chapter 10 Views. Topics in this Chapter What are Views For? View Retrievals View Updates Snapshots SQL Facilities.
Chapter 7 Relational Algebra. Topics in this Chapter Closure Revisited The Original Algebra: Syntax and Semantics What is the Algebra For? Further Points.
Further Normalization I
Data Definition After this lecture, you should be able to:
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Relational Algebra.
Database Management Systems 1 Raghu Ramakrishnan Relational Algebra Chpt 4 Xin Zhang.
Data Manipulation 21 After this lecture, you should be able to:  Use SQL SELECT statement effectively to retrieve the data from multiple related tables.
1 Relational Algebra Chapter 4, Sections 4.1 – 4.2.
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.
Relational Algebra Operators
CSCD34-Data Management Systems - A. Vaisman1 Relational Algebra.
Database Management Systems, R. Ramakrishnan1 Relational Algebra Module 3, Lecture 1.
CMPT 258 Database Systems Relational Algebra (Chapter 4)
Perform DFS from A A B C D E F G. Perform BFS from A A B C D E F G.
An Introduction to SQL For CS Overview of SQL  It is the standard language for relational systems, although imperfect  Supports data definition.
Unit 2 DB2 and SQL. 2-2 Wei-Pang Yang, Information Management, NDHU Outline of Unit Overview 2.2 Data Definition 2.3 Data Manipulation 2.4 The System.
Database Management Systems 1 Raghu Ramakrishnan Relational Algebra Chpt 4 Jianping Fan.
Views Prof. Yin-Fu Huang CSIE, NYUST Chapter 10. Advanced Database System Yin-Fu Huang 10.1Introduction Example: Var Good_Supplier View (S Where Status.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4, Part A.
1 Relational Algebra and SQL. 2 Relational Query Languages Languages for describing queries on a relational database Relational AlgebraRelational Algebra.
Relational Algebra COMP3211 Advanced Databases Nicholas Gibbins
Relations with Stored and Inherited Attributes Witold Litwin Dauphine University June 13, 2016.
Structured Query Language IV Asma Ahmad (C. J. Date) Database Systems.
RELATIONAL ALGEBRA (II)
STRUCTURE OF PRESENTATION :
Lecture 3 : Structured Query Language (SQL)
COMP3017 Advanced Databases
CS4432: Database Systems II
Relational Algebra Chapter 4 1.
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.
Relational Algebra 1.
LECTURE 3: Relational Algebra
Relational Algebra Chapter 4 1.
پايگاه داده ها.
Φροντιστήριο SQL (από το βιβλίο του Date)
Unit 7 Normalization (表格正規化).
STRUCTURE OF PRESENTATION :
Question 1: Basic Concepts (45 %)
CENG 351 File Structures and Data Managemnet
STRUCTURE OF PRESENTATION :
Relational Algebra Chapter 4 - part I.
Presentation transcript:

Algebra1 After this lecture, you should be able to:  Understand the differences between SQL (Structured Query Language) and Relational Algebra expressions.  Build queries in Relational Algebra.  Understand how DBMS’s process the SQL queries.  Make simple query optimization.  Be ready for Quiz 4 and Complete Assignment 7 (Part I). Relational Algebra

Algebra2 Relational Algebra  Relational algebra deals with a set of relations closed under operators. Operators operate on one or more relations to yield a relation.  Algebraic Expressions (a + b) * c (A  B)  C  Relational algebra is a basis for database query languages.

Algebra3 Characteristics of Relational Algebra  Operational  The order of operations can be specified.  Set-at-a-time  Multiple rows are processed by one operation.  Not too detailed  How to access data is not specified.  Good for query optimization.  Who supplied P2?  SQL Select SName from S, SP where S.S# = SP.S# and P# = 'P2' ;  Relational Algebra  SName (S ⋈ (  p#=‘P2‘ SP ))

Algebra4 Relational algebra …  Union: A  B  Intersection: A  B  Difference: A ̶ B  Cartesian Product: S × SP  Division: A / B  Selection:  city= ‘ Rome ‘ S  Projection:  SName,Status S  Join: S ⋈ SP  Renaming ρ(city  pcity) P

Algebra5 Terms: Relational algebra ~ Database  Relation ~ Table  Attribute ~ Column  Tuple ~ Row (Record)  Relation schema ~ Structure of table  Relation Instance ~ Data of table

Algebra6 Union and Intersection AB a1 b1 AB a2 b1 c2 AB a1 a2 b1 c2 AB b1 PQ P  Q P  Q

Algebra7 Union and Intersection: Exercise AB a1 b1 AB a1 c1 AB a1 b1 c1 AB a1 PQ P  Q = ?P  Q = ?

Algebra8 Difference (-) AB a1 b1 AB a2 b1 c2 AB a1 P Q P ̶ Q

Algebra9 Difference: Exercise AB a1 b1 c1 AB a1 c1 d1 AB b1 P Q P ̶ Q = ?

Algebra10 Cartesian Product ( × ) AB a1 b1 CD u2 v2 w2 P Q ABCD a1u2 a1v2 a1w2 b1u2 b1v2 b1w2 P × Q = {(p, q)|p  P and q  Q}

Algebra11 Cartesian Product: Exercise A a b CD u2 v2 P Q P × Q = ? ACD au2 av2 bu2 bv2

Algebra12 Selection (  ) ABC a1u a2u b2v ABC a2u b2v R  B = 2 R

Algebra13 Get all the information of the suppliers located in Paris. select * from s where city = ‘Paris’; Table S sno | sname | status | city s1 | Smith | 20 | London s2 | Jones | 10 | Paris s3 | Blake | 30 | Paris s4 | Clark | 20 | London s5 | Adams | 30 | Athens  city = ‘Paris‘ S

Algebra14 Projection (  ) ABC a1u a2u b2v CA ua vb R  C, A R

Algebra15 What are the S#’s and statuses of the suppliers? sno | status s1 | 20 s2 | 10 s3 | 30 select sno, status from s; Table S sno | sname | status | city s1 | Smith | 20 | London s2 | Jones | 10 | Paris s3 | Blake | 30 | Paris  sno, status S

Algebra16 What are the S#’s and statuses of the suppliers located in Paris? sno | status s2 | 10 s3 | 30 select sno, status from s where city = 'Paris; Table S sno | sname | status | city s1 | Smith | 20 | London s2 | Jones | 10 | Paris s3 | Blake | 30 | Paris s4 | Clark | 20 | London s5 | Adams | 30 | Athens  sno, status (  city = ‘Paris‘ S)

Algebra17 Selection and Projection: Exercise pno pname color weight city p1 nut red 12 London p2 bolt green 15 Paris p3 screw green 17 Rome select pname, weight from p where color = ‘green’; pname weight bolt 15 screw 17 Get the names and weights of the green parts.  pname, weight (  color = ‘green‘ P)

Algebra18  Join A1B1 a1 b2  B1 ≥ B2 (P × Q) A1B1A2B2 a1c1 b2a2 b2b2 b2c1 P A2B2 a2 b2 c1 Q

Algebra19 Equijoin A1B1 a1 b2 A1B1A2B2 a1c1 b2a2 b2b2 P A2B2 a2 b2 c1 Q  B1 = B2 (P × Q)

Algebra20 Natural Join ( ⋈ ) A1B a1 b2 P A2B a2 b2 c1 Q P ⋈ Q A1BA2 a1c b2a b2b

Algebra21 Natural Join: Example S#SNameCity S1AX S2BY S#P#QTY S1P110 S1P220 S2P130 S#SNameCityP#QTY S1AXP110 S1AXP220 S2BYP130 SSP S ⋈ SP = ?

Algebra22 Selection, Join, and Projection: Example What are the part numbers of the parts supplied by the suppliers located in Taipei? select SP.pno from S, SP where S.city = ‘Taipei’ and S.sno = SP.sno;  pno ((  S.City=‘Taipei’ S) ⋈ SP) SQL: Algebra:

Algebra23 Selection, Join, and Projection: Exercise What are the names of the suppliers who supplied green parts? select sname from P, SP, S where P.color = ‘green’ and P.pno = SP.pno and SP.sno = S.sno;  sname ((  pno (  color=‘green‘ P)) ⋈ SP ⋈ S) SQL: Algebra:

Algebra24 Subtraction Examples Get the supplier numbers of the suppliers who did not supply red parts.  The supplier numbers of all the suppliers: AS =  s# (S)  The supplier numbers of the suppliers who supplied some red parts: RPS =  s# (SP ⋈ (  Color = ‘Red‘ (P))  The answer is: AS - RPS

Algebra25 Subtraction Examples (cont’d) Get the names of the suppliers who supplied only red parts.  The supplier numbers of the suppliers who supplied some parts: SS =  s# (SP)  The supplier numbers of the suppliers who supplied at least one non-red parts: NRS =  s# (SP ⋈ (  color <> ‘Red‘ (P))  The answer is:  SName ((SS – NRS) ⋈ S)

Algebra26 Rename ( ρ ) ABCD a1u2 b1v2 c1w2 Ρ(B->B2, 3  C2) AB2C2D a1U2 b1v2 c1w2

Algebra27 Join with Renaming: Example I Get the pairs of the supplier numbers of the suppliers who supply at least one identical part. select SPX.sno, SPY.sno from SP SPX, SP SPY where SPX.pno = SPY.pno and SPX.sno < SPY.sno SQL: Algebra:  snox,snoy (  snox < snoy ((ρ(sno  snox)  sno,pno SP) ⋈ (ρ(sno  snoy)  sno,pno SP) )

Algebra28 Join with Renaming: Example II What are the colors of the parts supplied by the suppliers located in Taipei? Table S Table P sno | sname | sts | city pno | pname | color | wgt | city s1 | Smith | 20 | London p1 | nut | red | 12 | London s2 | Jones | 10 | Taipei p2 | bolt | green | 17 | Taipei p3 | screw | blue | 17 | Rome Table SP sno | pno | qty s1 | p1 | 300 s2 | p3 | 200

Algebra29 Join with Renaming: Example II What are the colors of the parts supplied by the suppliers located in Taipei? select color From S, SP, P where S.city = ‘Taipei’ and S.sno = SP.sno and SP.pno = P.pno;  color (  S.City=‘Taipei‘ (S ⋈ SP ⋈ P)) Joined also by S.city = P.city ! SQL: Algebra:

Algebra30 Join with Renaming: Example II (cont’d)  color (  City=‘Taipei‘ (S ⋈ SP ⋈ ρ(city  pcity)P)) Renamed: ((  color ((  pno ((  sno (  City=‘Taipei‘ S)) ⋈ SP)) ⋈ P) Optimized:  color (  S.City=‘Taipei‘ (S ⋈ SP ⋈ P)) Not Correct:

Algebra31 Join Example: Query Plan P SSP ⋈  city=‘Taipei‘ S  sno ⋈  color  pno

Algebra32 SQL Query Processing  An SQL query is transformed into an algebraic form for query optimization.  Query optimization is the major task of an SQL query proccessor.  select A 1, A 2, …, A m from T 1, T 2, …, T n where C is converted to  A1, A2, …, Am  C (T 1 x T 2 x … x T n ) and then optimized.

Algebra33 Query Optimization Guidelines  JOIN operations are associative and commutative  (R ⋈ S) ⋈ T = R ⋈ ( S ⋈ T)  R ⋈ S = S ⋈ R  JOIN and SELECT operations are associative and commutative if the SELECT operations are still applicable.  PROJECTIONs can be performed to remove column values not used.  These properties can be used for query optimization.  The order of operations can be changed to produce smaller intermediate results.

Algebra34 Optimization: Example What are the part numbers of the parts supplied by the suppliers located in Taipei?  pno ((  sno (  S.City=‘Taipei‘ S)) ⋈ (  sno,pno SP)) Optimized:  pno (  S.City=‘Taipei‘ (S ⋈ SP)) Not Optimized:  pno ((  S.City=‘Taipei‘ S) ⋈ SP) Improved: