WDM3304 week 6 Table Joins. Primary keys, foreign keys Primary key: A field of a table designated to...

19
WDM3304 week 6 Table Joins

Transcript of WDM3304 week 6 Table Joins. Primary keys, foreign keys Primary key: A field of a table designated to...

Page 1: WDM3304 week 6 Table Joins. Primary keys, foreign keys Primary key: A field of a table designated to provide a unique identifier for a specific row of.

WDM3304 week 6

Table Joins

Page 2: WDM3304 week 6 Table Joins. Primary keys, foreign keys Primary key: A field of a table designated to provide a unique identifier for a specific row of.

Primary keys, foreign keys

Primary key: A field of a table designated to provide a unique identifier for a specific row of the table

Foreign key: A field of a table whose value matches the primary key of another table. From foreign keys, we see relationships between one table and another.

Page 3: WDM3304 week 6 Table Joins. Primary keys, foreign keys Primary key: A field of a table designated to provide a unique identifier for a specific row of.

JoinsA join is the cross

product of all the rows of a set of tables.

By selecting a subset of the rows of the join that match some criteria, we can answer simple questions about our data. This is the core of the relational model.

Two tables

Inner Join of two tables

Left outer join

Left outer join

Page 4: WDM3304 week 6 Table Joins. Primary keys, foreign keys Primary key: A field of a table designated to provide a unique identifier for a specific row of.

A simple table

Name Age

Mark 40

Matthew 11

Brian 38

Fields

The implied relation of this row is Mark is 40 years old.

Page 5: WDM3304 week 6 Table Joins. Primary keys, foreign keys Primary key: A field of a table designated to provide a unique identifier for a specific row of.

More complex tables

Picture PictureID

Class1.jpg P1

Class2.jpg P2

Student StudentID

Katie S1

Brittany S2

Carrie S3

PictureID StudentID

P1 S1

P1 S2

P2 S3

Page 6: WDM3304 week 6 Table Joins. Primary keys, foreign keys Primary key: A field of a table designated to provide a unique identifier for a specific row of.

How to use complex tablesWhat picture is

Brittany in?Look up her ID in the

student tableLook up the

corresponding PictureID in the PictureID-StudentID table

Look up the picture in the Picture tableAnswer: Class1.jpg

Student StudentID

Katie S1

Brittany S2

Carrie S3

Picture PictureID

Class1.jpg P1

Class2.jpg P2

PictureID StudentID

P1 S1

P1 S2

P2 S3

Page 7: WDM3304 week 6 Table Joins. Primary keys, foreign keys Primary key: A field of a table designated to provide a unique identifier for a specific row of.

Another UseWho is in

“Class1.jpg”?Look up the picture in

the Picture table to get the ID

Look up the corresponding PictureID in the PictureID-StudentID table

Look up the StudentNames in the Student picture Answer: Katie and

Brittany

StudentName StudentID

Katie S1

Brittany S2

Carrie S3

Picture PictureID

Class1.jpg P1

Class2.jpg P2

PictureID StudentID

P1 S1

P1 S2

P2 S3

Page 8: WDM3304 week 6 Table Joins. Primary keys, foreign keys Primary key: A field of a table designated to provide a unique identifier for a specific row of.

A Database Join

We call this kind of access across multiple tables a join

By joining tables, we can represent more complex relationships than with just a single table.

Most database systems provide the ability to join tables.

Joining works better if the tables are well-formed:SimpleContaining only a single relation per row

Page 9: WDM3304 week 6 Table Joins. Primary keys, foreign keys Primary key: A field of a table designated to provide a unique identifier for a specific row of.

Querying from a join Answering: What picture is Brittany in?

Select

p.picture,

s.studentName

From

Students as s,

IDs as i,

Pictures as p

Where

(s.studentName=“Brittany”) and

(s.studentID=i.studentID) and

(i.pictureID=p.pictureID)

Picture PictureID

Class1.jpg P1

Class2.jpg P2

StudentName StudentID

Katie S1

Brittany S2

Carrie S3

PictureID StudentID

P1 S1

P1 S2

P2 S3

Page 10: WDM3304 week 6 Table Joins. Primary keys, foreign keys Primary key: A field of a table designated to provide a unique identifier for a specific row of.

Join Types inner join (only those records from tables on both

sides of the join that match the join criteria… Inner joins are the most common type of join )

left outer join (Retrieve all records from the table on the left side of the join and only those records that match the join criteria from the table on the right side of the join)

right outer join (Retrieve only those records from the table on the left side of the join condition that match the join criteria but all records from the right side of the join condition)

full outer join (Retrieve all records from tables on both sides of the join condition regardless of whether records match the join criteria)

Page 11: WDM3304 week 6 Table Joins. Primary keys, foreign keys Primary key: A field of a table designated to provide a unique identifier for a specific row of.

• Relation loan

Relation borrower

customer-name loan-number

Jones

Smith

Hayes

L-170

L-230

L-155

amount

3000

4000

1700

branch-name

Downtown

Redwood

Perryridge

loan-number

L-170

L-230

L-260

Note: borrower information missing for L-260 and loan information missing for L-155

Page 12: WDM3304 week 6 Table Joins. Primary keys, foreign keys Primary key: A field of a table designated to provide a unique identifier for a specific row of.

loan inner join borrower onloan.loan-number = borrower.loan-number

loan left outer join borrower onloan.loan-number = borrower.loan-number

branch-name amount

Downtown

Redwood

3000

4000

customer-name loan-number

Jones

Smith

L-170

L-230

loan-number

L-170

L-230

branch-name amount

Downtown

Redwood

Perryridge

3000

4000

1700

customer-name loan-number

Jones

Smith

null

L-170

L-230

null

loan-number

L-170

L-230

L-260

Page 13: WDM3304 week 6 Table Joins. Primary keys, foreign keys Primary key: A field of a table designated to provide a unique identifier for a specific row of.

• loan natural inner join borrower

loan natural right outer join borrower

branch-name amount

Downtown

Redwood

3000

4000

customer-name

Jones

Smith

loan-number

L-170

L-230

branch-name amount

Downtown

Redwood

null

3000

4000

null

customer-name

Jones

Smith

Hayes

loan-number

L-170

L-230

L-155

Page 14: WDM3304 week 6 Table Joins. Primary keys, foreign keys Primary key: A field of a table designated to provide a unique identifier for a specific row of.

loan full outer join borrower using (loan-number)

Find all customers who have either an account or a loan (but not both) at the bank. (customer_number, account_number, loan_number)

branch-name amount

Downtown

Redwood

Perryridge

null

3000

4000

1700

null

customer-name

Jones

Smith

null

Hayes

loan-number

L-170

L-230

L-260

L-155

select customer-namefrom (depositor natural full outer join borrower)where account-number is null or loan-number is null

Page 15: WDM3304 week 6 Table Joins. Primary keys, foreign keys Primary key: A field of a table designated to provide a unique identifier for a specific row of.

Select In

If you only want to copy a few fields, you can do so by listing them after the SELECT statement:

select customer-name into borrower_backup from Borrower

You can also add a where clause. The following example creates a " Borrower_backup" table with one column (customer-name) by extracting those with acct # = “1120” from the “Accounts" table:select customer-name into borrower_backup from borrower where loan-number=‘1120'

Page 16: WDM3304 week 6 Table Joins. Primary keys, foreign keys Primary key: A field of a table designated to provide a unique identifier for a specific row of.

Selecting data from more than one table is also possible. The following example creates a new table “Borrower_Rec_backup" that contains data from the two tables Borrower and Loan:

select customer-name,amount into borrower_rec_backup from (borrower inner join loan on borrower.loan-number=loan.loan-number)

Page 17: WDM3304 week 6 Table Joins. Primary keys, foreign keys Primary key: A field of a table designated to provide a unique identifier for a specific row of.

Schemas By creating several

tables, with the appropriate Primary keys, foreign keys, we can model the data needs of our application

Database especially suited for representing the relationship between our entities (objects) when the number of rows in our schemas very large compared to the number of schemas

Page 18: WDM3304 week 6 Table Joins. Primary keys, foreign keys Primary key: A field of a table designated to provide a unique identifier for a specific row of.

Database merging

In order to merge two databases together, you need to make sure that they share the same coordinate system

In addition, you often have to link tables togetherThis is referred to as “relating tables”The key is to make sure that they have a common field

Page 19: WDM3304 week 6 Table Joins. Primary keys, foreign keys Primary key: A field of a table designated to provide a unique identifier for a specific row of.

Joining vs. relating

Typically, tables are joined to display additional data no in the layer’s attribute table

Joining brings two tables together “virtually” so they appear as one but remain 2 separate files

Relating, however, establishes a linkage between two tables but they remain separate