Joining Relations in SQL Objectives of the Lecture : To consider the Natural & Generalised Joins using the SQL1 standard; To consider the Natural & Generalised.

Slides:



Advertisements
Similar presentations
Chapter 4 Joining Multiple Tables
Advertisements

Chapter 6 The Relational Algebra
พีชคณิตแบบสัมพันธ์ (Relational Algebra) บทที่ 3 อ. ดร. ชุรี เตชะวุฒิ CS (204)321 ระบบฐานข้อมูล 1 (Database System I)
Copyright © C. J. Date 2005page 97 S#Y S1DURINGS3DURING [d04:d10][d08:d10] S2DURINGS4DURING [d02:d04][d04:d10] [d08:d10] WITH ( EXTEND T2 ADD ( COLLAPSE.
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.
1 CHAPTER 4 RELATIONAL ALGEBRA AND CALCULUS. 2 Introduction - We discuss here two mathematical formalisms which can be used as the basis for stating and.
Referential Integrity & Foreign Keys Objectives of the Lecture : To consider Referential Integrity & Foreign Keys; To consider Referential Integrity Constraints.
Relational Algebra 1 Chapter 5.1 V3.0 Napier University Dr Gordon Russell.
Candidate Keys Objectives of the Lecture : To consider Integrity Constraints; To consider the principles of Candidate Keys; To consider Candidate Keys.
Multiple-Column Subqueries 12. Objectives After completing this lesson, you should be able to do the following: Write a multiple-column subquery Describe.
Query Evaluation. An SQL query and its RA equiv. Employees (sin INT, ename VARCHAR(20), rating INT, age REAL) Maintenances (sin INT, planeId INT, day.
Query Evaluation. SQL to ERA SQL queries are translated into extended relational algebra. Query evaluation plans are represented as trees of relational.
Introduction to Oracle9i: SQL1 Basic SQL SELECT Statements.
Database Principles Relational Algebra. Database Principles What is Relational Algebra? It is a language in which we can ask questions (query) of a database.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 8 Advanced SQL.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 8 Advanced SQL.
Cs3431 Relational Algebra : #I Based on Chapter 2.4 & 5.1.
Query Design Objectives of the Lecture : To learn a strategy for designing queries. To learn how to use relational algebra concepts to implement the strategy.
Objectives of the Lecture :
Relational databases.  Retrieving data from a database requires pulling data from multiple tables  Tables relate to each other in distinct ways, modelled.
Retrievals & Projections Objectives of the Lecture : To consider retrieval actions from a DB; To consider using relational algebra for defining relations;
Database Systems: Design, Implementation, and Management Tenth Edition Chapter 8 Advanced SQL.
SQL. Basic Structure SQL is based on set and relational operations with certain modifications and enhancements A typical SQL query has the form: select.
Database Systems: Design, Implementation, and Management Tenth Edition Chapter 8 Advanced SQL.
1 CS 430 Database Theory Winter 2005 Lecture 12: SQL DML - SELECT.
M Taimoor Khan Course Objectives 1) Basic Concepts 2) Tools 3) Database architecture and design 4) Flow of data (DFDs)
Ad Hoc Constraints Objectives of the Lecture : To consider Ad Hoc Constraints in principle; To consider Ad Hoc Constraints in SQL; To consider other aspects.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 The Relational Algebra.
The Data in a Relation To consider atomic data in relations; To consider data types in a relation; To consider missing data & NULLs in relations. Objectives.
CSE314 Database Systems The Relational Algebra and Relational Calculus Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson Ed Slide Set.
M Taimoor Khan Course Objectives 1) Basic Concepts 2) Tools 3) Database architecture and design 4) Flow of data (DFDs)
Amendments & Transactions Objectives of the Lecture : To consider amendments to a relation; To consider transactions in SQL.
JOI/1 Data Manipulation - Joins Objectives –To learn how to join several tables together to produce output Contents –Extending a Select to retrieve data.
NULLs & Outer Joins Objectives of the Lecture : To consider the use of NULLs in SQL. To consider Outer Join Operations, and their implementation in SQL.
Chapter 4Introduction to Oracle9i: SQL1 Chapter 4 Joining Multiple Tables.
8 1 Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
603 Database Systems Senior Lecturer: Laurie Webster II, M.S.S.E.,M.S.E.E., M.S.BME, Ph.D., P.E. Lecture 17 A First Course in Database Systems.
Set Operations Objectives of the Lecture : To consider the Set Operations Union, Difference and Intersect; To consider how to use the Set Operations in.
Further GroupBy & Extend Operations Objectives of the Lecture : To consider “whole relation” Grouping; To consider the SQL Grouping option Having; To consider.
Topics Related to Attribute Values Objectives of the Lecture : To consider sorting relations by attribute values. To consider Triggers and their use for.
Slide 6- 1 CARTESIAN (or cross) Product Operation Defines a relation Q that is the concatenation of every tuple of relation R with every tuple of relation.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 6- 1.
Relational Algebra Operators
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 Part a The Relational Algebra and Relational Calculus Hours 1,2.
Presented By: Miss N. Nembhard. Relation Algebra Relational Algebra is : the formal description of how a relational database operates the mathematics.
1 CS 430 Database Theory Winter 2005 Lecture 5: Relational Algebra.
Relations, Functions, and Matrices Mathematical Structures for Computer Science Chapter 4 Copyright © 2006 W.H. Freeman & Co.MSCS Slides Relations, Functions.
Query Processing – Query Trees. Evaluation of SQL Conceptual order of evaluation – Cartesian product of all tables in from clause – Rows not satisfying.
Insert & Delete Objectives of the Lecture : To consider the insertion of tuples into a relation; To consider the deletion of tuples from a relation; To.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Grouping These slides are licensed under.
Restrictions Objectives of the Lecture : To consider the algebraic Restrict operator; To consider the Restrict operator and its comparators in SQL.
Manipulating Data Lesson 3. Objectives Queries The SELECT query to retrieve or extract data from one table, how to retrieve or extract data by using.
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
STRUCTURE OF PRESENTATION :
Relational Algebra - Part 1
Database Systems: Design, Implementation, and Management Tenth Edition
LECTURE 3: Relational Algebra
Relational Algebra : #I
CS 3630 Database Design and Implementation
CS4222 Principles of Database System
Chapter 8 Advanced SQL.
CS222P: Principles of Data Management Notes #13 Set operations, Aggregation, Query Plans Instructor: Chen Li.
Database Systems: Design, Implementation, and Management Tenth Edition
Objectives of the Lecture :
Relational Algebra & Calculus
Manipulating Data Lesson 3.
Unit Relational Algebra 1
Presentation transcript:

Joining Relations in SQL Objectives of the Lecture : To consider the Natural & Generalised Joins using the SQL1 standard; To consider the Natural & Generalised Joins using the SQL2 standard.

Joins in SQL l Expressing joins in SQL has been particularly affected by two particular SQL standards : l SQL1 Standard SQL, introduced in this has no specific support for joins; l SQL2 Standard SQL introduced in this has special support for joins. The SQL3 Standard SQL introduced in 1999 maintains this support. l It is important to know how to use both variants of SQL. l As the SQL1 standard is a subset of SQL2, it is always possible to write an SQL1 style join in SQL2. l Oracle SQL 9i supports SQL2 standard joins, but earlier versions of Oracle only meet the SQL1 standard.

SQL1 : Generalised Join Syntax The Generalised Join of relations R and S has the syntax : Select* FromR, S Wheretheta-join condition ; Principles : l Put Select * to get all the attributes in the result. l Put both relation names in the From phrase. l Put the complete theta join condition in the Where phrase. The SQL statement also retrieves the result from the DB. SQL fulfills all the requirements of the Generalised Join operation.

SQL1 : Natural Join Syntax The Natural Join of relations R and S has the syntax : Selectthe result’s column names From R, S Where equi-join condition ; Principles : l Put Select COLUMN_NAMES to get all the columns in the result. l Put both relation names in the From phrase. l Put the complete equi join condition in the Where phrase. The SQL statement also retrieves the result from the DB. SQL fulfills all the requirements of the Natural Join operation. Prefixed by TABLE_NAME. Omit duplicate columns. Always prefixed with TABLE_NAME.

Examples : SQL1 Generalised Joins SQL1 equivalents of previous examples : l Select* FromR, S WhereB < C ; l Select* FromR, S WhereA > E And B <> D ; As the operands have no column names in common, it is safe to use “*” in the Select phrase and omit table name prefixes in the Where phrase.

Example : SQL1 Natural Join SQL equivalent of previous example : l Select PNo, Qty, SHIP.SNo, SName, From SHIP, SUPP Where SHIP.SNo = SUPP.SNo ; Or l Select PNo, Qty, SUPP.SNo, SName, From SHIP, SUPP Where SHIP.SNo = SUPP.SNo ; Doesn’t matter from which table the “SNo” column comes. The order in which the tables appear in the From phrase, and which “SNo” column appears on which side of the “=”, don’t matter.

Combining Algebra Operators l Typically we want to join together 2 relations holding relevant data, and then prune the result down with a projection and restriction to yield just the required data : R Join[ Att ] S Restrict[ condition ] Project[ AttNames ] l In SQL, put the Projected attributes in the Select phrase, the Joined relations in the From phrase, and And the Join and Restrict conditions together in the Where phrase, as follows : Select Distinct AttNames From R, S Where ( R.Att = S.Att ) And (condition ) ; l SQL’s built-in sequence of operations will execute a Cartesian Product of R and S, then a Restrict on the result using the entire Where condition, & finally a Project on that result using the Select attributes. Restrict condition Join condition

Examples of Combining Operators Example : Get the supplier’s name who supplies parts in quantities of 10. SHIP Join[ SNo ] SUPP Restrict[ Qty = 10 ] Project[ SName ] SelectDistinct Sname FromSHIP, SUPP WhereSHIP.SNo = SUPP.SNo And Qty = 10 ; Example : Get the names of employees who own a Corsa 1.3. CAR Gen[ Owner = ENo ] EMPLOYEE Restrict[ Type = ‘Corsa 1.3’ ] Project[ EName ] SelectDistinct EName FromCAR, EMPLOYEE WhereOwner = ENo And Type = ‘Corsa 1.3’ ;

Designing SQL Queries l Decide which DB relations contain data that will be required in the answer to the query, and join all those relations together with the appropriate Natural/Generalised Join operation(s). l Remove any unrequired tuples with Restrict operation(s). In principle only one Restrict operation is required, but it may be more convenient to use several. l Remove any unrequired attributes with a Project operation; only one Project operation will be necessary. l Complete the appropriate SQL phrases with the relevant information from the algebra operations : Select …… From.…… Where ( ……… ) And ( ……… ) ; Restrict condition Join condition Project attributes Tables to be joined

SQL : Cartesian Product l SQL1 executes a Cartesian Product operation given the following syntax : Select* FromR, S ; l Hence the absence of a join condition in the Where phrase causes SQL to execute a Cartesian Product : l If a Cartesian Product is actually needed in a query instead of a Natural or Generalised Join, then just omit the Join condition from the Where phrase. l If a Join condition is accidentally omitted from the Where phrase by error, then the result will be unexpectedly (very) large due to a Cartesian Product operation ! l SQL2 actually has a Cartesian Product operator, with syntax : Select* FromR Cross Join S ;

SQL2 : Generalised Join Syntax The Generalised Join of relations R and S has the syntax : Select* FromR Join S On ( theta-join condition ) ; Principles : l Put Select * to get all the attributes in the result. l Put R Join S On ( theta-join condition ) in the From phrase, where R and S are the operands and ( theta-join condition ) is the complete generalised join condition. l No Where phrase is required. The SQL statement also retrieves the result from the DB. SQL fulfills all the Generalised Join requirements.

Examples : SQL2 Generalised Joins SQL2 equivalents of previous examples : l Select* FromR Join S On ( B < C ) ; l Select* FromR Join S On ( A > E And B <> D ) ; As the operands have no column names in common, it is safe to use “*” in the Select phrase and omit table name prefixes in the Where phrase.

SQL2 : Natural Join Syntax There are 2 ways of writing a Natural Join of operands R and S in SQL2 : l Select* FromR Natural Join S ; l Select* FromR Join S Using ( AttributeName(s) ) ; Principles : l These are the same as for Generalised Join, except that a different required expression is put in the From phrase. The SQL statement also retrieves the result from the DB. Both variants fulfill all the Natural Join requirements. The attributes on which the ‘=‘ comparison(s) is/are made.

Examples : SQL2 Natural Joins SQL2 equivalents of a previous example : l Select * From SHIP Natural Join SUPP ; l Select * From SHIP Join SUPP Using ( SNo ) ;

SQL2 : Join Problem (1) l Select * From CAR Natural Join EMPLOYEE ; l Select * From CAR Join EMPLOYEE Using ( Owner, ENo ) ; Neither will work ! Columns “Owner “ and “ENo” don’t appear in both tables. So use an SQL Generalised Join to express the required join, & remove the duplicate data in the Select phrase : l Select RegNo, Type, Owner, EName, M-S, Sal From CAR Join EMPLOYEE On ( Owner = ENo ) ; Could have omitted “Owner” instead of “ENo” in Select phrase.

SQL2 : Join Problem (2) l Consider the join expressed as : R Join S Using ( J1 ) l Suppose there are two attributes, named J1 and J2, both of which appear in R and in S, and are type compatible. l The join will be carried out just using J1, as specified. ==> the result will have two attributes called J2 in it. l There are 2 considerations concerning the result : l If a real join requires both J1 and J2, then SQL will have generated the wrong result (unless by chance the data in the tables avoids this). l If the problem was unhelpful column names, so that the correct result was generated, the two columns can be distinguished with their table name prefix in the Select phrase.

Combining Algebra Operators Follow the same procedure is as before, but using SQL2 syntax. Example : SHIP Join[ SNo ] SUPP Restrict[ Qty = 10 ] Project[ SName ] Select Distinct Sname From SHIP Natural Join SUPP Where Qty = 10 ; Example : CAR Gen[ Owner = ENo ] EMPLOYEE Restrict[ Type = ‘Corsa 1.3’ ] Project[ EName ] Select Distinct EName From CAR Join EMPLOYEE On (Owner = ENo) Where Type = ‘Corsa 1.3’ ; Or SHIP Join SUPP Using(SNo) becomes