chapter 10: joins - faculty.ccri.edufaculty.ccri.edu/mkelly/comi1260/ch10.pdf · chapter 10: joins...

5
Chapter 10: Joins o Joins DML Component extended to allow working with multiple tables The true core of Relational Database Referential integrity o Primary & Foreign keys Dispel the myth that MySQL is lacking here Supports SQL2-compliant syntax Join Types Simple o Use of WHERE SELECT * FROM team; SELECT * FROM player; SELECT playerid, playerlname, playerfname, teamcity, teamname FROM player, team WHERE player.teamid = team.teamid; o Note the table prefix in the join Used when same field name in multiple tables o Without further criteria you will have a large result set o Does logically limit your result set Matching values between the tables SELECT playerid, playerlname, playerfname, teamcity, teamname, teamwin, teamloss FROM player, team WHERE player.teamid = team.teamid and teamwin > 8;

Upload: volien

Post on 04-Jul-2018

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Chapter 10: Joins - faculty.ccri.edufaculty.ccri.edu/mkelly/COMI1260/CH10.pdf · Chapter 10: Joins o Joins DML Component extended to allow working with multiple tables The true core

Chapter 10: Joins

o Joins

DML Component extended to allow working with multiple tables

The true core of Relational Database

Referential integrity

o Primary & Foreign keys

Dispel the myth that MySQL is lacking here

Supports SQL2-compliant syntax

Join Types

Simple o Use of WHERE

SELECT * FROM team;

SELECT * FROM player;

SELECT playerid, playerlname, playerfname, teamcity,

teamname FROM player, team

WHERE player.teamid = team.teamid;

o Note the table prefix in the join

Used when same field name in multiple

tables

o Without further criteria you will have a large

result set

o Does logically limit your result set

Matching values between the tables

SELECT playerid, playerlname, playerfname, teamcity,

teamname, teamwin, teamloss FROM player, team

WHERE player.teamid = team.teamid and teamwin > 8;

Page 2: Chapter 10: Joins - faculty.ccri.edufaculty.ccri.edu/mkelly/COMI1260/CH10.pdf · Chapter 10: Joins o Joins DML Component extended to allow working with multiple tables The true core

Cross

o Multiplies the tables to create an all-inclusive

result set

o PRODUCT

SELECT * FROM team;

SELECT * FROM state;

SELECT * FROM team, state order by teamid;

o All possible combinations between the two

tables

Implications when a large volume of

records

Inner o Most commonly used

o Symmetrical

Referential integrity

Matching values between JOIN

arguments

o EQUI-JOINS

What we looked at in simple joins earlier

Based upon matching values

SELECT teamcity, teamname, c_fname, c_lname FROM

team, coaches WHERE teamid = c_team;

SELECT teamcity, teamname, c_fname, c_lname,

playerfname, playerlname FROM team, coaches, player

WHERE team.teamid = c_team AND team.teamid =

player.teamid ORDER BY team.teamid;

SELECT c_fname, c_lname, c_salary AS 'Old Salary',

IF(teamwin>8,c_salary * 1.15,c_salary) AS 'New Salary'

FROM coaches INNER JOIN team;

SELECT teamcity, teamname, c_fname, c_lname,

playerfname, playerlname FROM team INNER JOIN

coaches INNER JOIN player

WHERE team.teamid = c_team AND team.teamid =

player.teamid ORDER BY team.teamid;

Multiple joins, will result in slower

response

Page 3: Chapter 10: Joins - faculty.ccri.edufaculty.ccri.edu/mkelly/COMI1260/CH10.pdf · Chapter 10: Joins o Joins DML Component extended to allow working with multiple tables The true core

o THETA-JOINS

Inner Joins based on inequalities

SELECT * FROM player;

SELECT * FROM freeagents;

Use Ted Bruschi as example

Find all freeagents younger than

Ted Bruschi

SELECT f.PlayerFName, f.PlayerLName, f.DOB,

p.playerid, p.playerfname, p.playerlname, p.dob

FROM freeagents f, player p WHERE p.playerid =

00002 AND f.dob > p.dob ORDER BY f.dob DESC;

Notice table alias

Outer o Asymmetrical

Result includes all rows from one table

and matching rows (if any) from the

other(s)

o LEFT OUTER JOIN

All rows from table on the left will be

included (any WHERE conditions will

factor in) along with any matching rows

in the second table

SELECT * FROM freeagents;

SELECT * FROM team;

Notice there are no free agents

from team 11, the Dallas Cowboys

or team 13, the Seattle Seahawks

SELECT teamid, teamcity, teamname, playerfname,

playerlname FROM team LEFT JOIN freeagents ON

team.teamid = freeagents.priorteam ORDER BY teamid;

Notice NULL for freeagent data

o RIGHT OUTER JOIN

Page 4: Chapter 10: Joins - faculty.ccri.edufaculty.ccri.edu/mkelly/COMI1260/CH10.pdf · Chapter 10: Joins o Joins DML Component extended to allow working with multiple tables The true core

All rows from table on the right will be

included (any WHERE conditions will

factor in) along with any matching rows

in the second table

Following rules of referential

integrity this should not occur

The order of the tables determines the

join

Previous example in reverse order

SELECT teamid, teamcity, teamname, playerfname,

playerlname FROM team RIGHT JOIN freeagents ON

team.teamid = freeagents.priorteam ORDER BY teamid;

Notice there are now no entries for

Dallas or Seattle

Self o Recursive / Unary relationship

o Joins table to itself

Used to extract data from a table whose

records contain internal links to each

other

See pages 262 – 265

o We need to do some prep 1st

ALTER TABLE freeagents ADD teamid CHAR(2);

UPDATE freeagents SET teamid = ‘10’ WHERE

playerid = ‘01013’;

UPDATE freeagents SET teamid = ‘04’ WHERE

playerid = ‘01005’;

UPDATE freeagents SET teamid = ‘08’ WHERE

playerid = ‘01004’;

UPDATE freeagents SET teamid = ‘11’ WHERE

playerid = ‘01000’

;

o Let’s look at what we have now

SELECT PlayerFName, PlayerLName, priorteam, teamid

FROM freeagents;

Page 5: Chapter 10: Joins - faculty.ccri.edufaculty.ccri.edu/mkelly/COMI1260/CH10.pdf · Chapter 10: Joins o Joins DML Component extended to allow working with multiple tables The true core

SELECT a.playerfname, a.playerlname, teamcity,

teamname FROM freeagents a, freeagents b, team t

WHERE a.priorteam = b.teamid AND

b.teamid = t.teamid;

Union o Used to combine the output of multiple

SELECT queries into a single result set

o Must have number same number of rows and

corresponding data types

o Duplicate records are automatically eliminated

(DISTINCT)

Use ALL keyword if you wish to see all

results

CREATE TABLE MyTeams LIKE team;

SELECT teamcity, teamname FROM team UNION

SELECT teamcity, teamname FROM Myteams;