17
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.

Joining Relations in SQL

  • Upload
    aimon

  • View
    26

  • Download
    0

Embed Size (px)

DESCRIPTION

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. - PowerPoint PPT Presentation

Citation preview

Page 1: Joining Relations in SQL

Joining Relationsin 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.

Page 2: Joining Relations in SQL

Joins in SQL Expressing joins in SQL has been particularly affected by

two particular SQL standards : SQL1 Standard SQL, introduced in 1989 - this has no

specific support for joins; SQL2 Standard SQL introduced in 1992 - this has

special support for joins. The SQL3 Standard SQL introduced in 1999 maintains this support.

It is important to know how to use both variants of SQL.

As the SQL1 standard is a subset of SQL2, it is always possible to write an SQL1 style join in SQL2.

Oracle SQL 9i supports SQL2 standard joins, but earlier versions of Oracle only meet the SQL1 standard.

Page 3: Joining Relations in SQL

SQL1 : Generalised Join Syntax

The Generalised Join of relations R and S has the syntax :

Select * From R, S Where theta-join condition ;

Principles : Put Select * to get all the attributes in the result. Put both relation names in the From phrase. 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.

Page 4: Joining Relations in SQL

SQL1 : Natural Join Syntax

The Natural Join of relations R and S has the syntax :

Select the result’s column names From R, S Where equi-join condition ;

Principles : Put Select COLUMN_NAMES to get all the columns in

the result. Put both relation names in the From phrase. 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 duplicatecolumns.

Always prefixed withTABLE_NAME.

Page 5: Joining Relations in SQL

Examples : SQL1 Generalised Joins

SQL1 equivalents of previous examples :

Select *From R, SWhere B < C ;

Select *From R, S Where 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.

Page 6: Joining Relations in SQL

Example : SQL1 Natural Join

SQL equivalent of previous example :

Select PNo, Qty, SHIP.SNo, SName, From SHIP, SUPPWhere SHIP .SNo = SUPP .SNo ;

Or

Select PNo, Qty, SUPP.SNo, SName, From SHIP, SUPPWhere SHIP .SNo = SUPP .SNo ;

Doesn’t matter fromwhich 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.

Page 7: Joining Relations in SQL

Combining Algebra Operators 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 ]

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 ) ;

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.

Restrictcondition

Join condition

Page 8: Joining Relations in SQL

Examples of Combining OperatorsExample :

Get the supplier’s name who supplies parts in quantities of 10.SHIP Join[ SNo ] SUPP

Restrict[ Qty = 10 ] Project[ SName ]

Select Distinct SnameFrom SHIP, SUPPWhereSHIP.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 ]

Select Distinct ENameFrom CAR, EMPLOYEEWhereOwner = ENo And Type = ‘Corsa 1.3’ ;

Page 9: Joining Relations in SQL

Designing SQL Queries 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).

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.

Remove any unrequired attributes with a Project operation; only one Project operation will be necessary.

Complete the appropriate SQL phrases with the relevant information from the algebra operations :

Select ……From .……Where ( ……… ) And ( ……… ) ;

Restrictcondition

Joincondition

Project attributes

Tables tobe joined

Page 10: Joining Relations in SQL

SQL : Cartesian Product SQL1 executes a Cartesian Product operation given the

following syntax : Select *From R, S ;

Hence the absence of a join condition in the Where phrase causes SQL to execute a Cartesian Product :

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.

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 !

SQL2 actually has a Cartesian Product operator, with syntax :Select *From R Cross Join S ;

Page 11: Joining Relations in SQL

SQL2 : Generalised Join Syntax

The Generalised Join of relations R and S has the syntax :Select *From R Join S On ( theta-join condition ) ;

Principles : Put Select * to get all the attributes in the result. 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.

No Where phrase is required.

The SQL statement also retrieves the result from the DB.SQL fulfills all the Generalised Join requirements.

Page 12: Joining Relations in SQL

Examples : SQL2 Generalised Joins

SQL2 equivalents of previous examples :

Select *From R Join S On ( B < C ) ;

Select *From R 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.

Page 13: Joining Relations in SQL

SQL2 : Natural Join Syntax

There are 2 ways of writing a Natural Join of operands R and S in SQL2 :

Select *From R Natural Join S ;

Select *From R Join S Using ( AttributeName(s) ) ;

Principles : 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.

Page 14: Joining Relations in SQL

Examples : SQL2 Natural Joins

SQL2 equivalents of a previous example :

Select *From SHIP Natural Join SUPP ;

Select *From SHIP Join SUPP Using ( SNo ) ;

Page 15: Joining Relations in SQL

SQL2 : Join Problem (1) Select *

From CAR Natural Join EMPLOYEE ;

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 :

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.

Page 16: Joining Relations in SQL

SQL2 : Join Problem (2) Consider the join expressed as :

R Join S Using ( J1 )

Suppose there are two attributes, named J1 and J2, both of which appear in R and in S, and are type compatible.

The join will be carried out just using J1, as specified. ==> the result will have two attributes called J2 in it.

There are 2 considerations concerning the result : 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).

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.

Page 17: Joining Relations in SQL

Combining Algebra OperatorsFollow the same procedure is as before, but using SQL2 syntax.

Example : SHIP Join[ SNo ] SUPPRestrict[ Qty = 10 ] Project[ SName ]

Select Distinct SnameFrom SHIP Natural Join SUPPWhere Qty = 10 ;

Example : CAR Gen[ Owner = ENo ] EMPLOYEE Restrict[ Type = ‘Corsa 1.3’ ] Project[ EName ]

Select Distinct ENameFrom CAR Join EMPLOYEE On (Owner = ENo)Where Type = ‘Corsa 1.3’ ;

OrSHIP Join SUPP Using(SNo)

becomes

becomes