Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 WDM3304 week 6 Table Joins

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

3 Joins A 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

4 A simple table NameAge Mark40 Matthew11 Brian38 Fields The implied relation of this row is Mark is 40 years old.

5 More complex tables PicturePictureID Class1.jpgP1 Class2.jpgP2 StudentStudentID KatieS1 BrittanyS2 CarrieS3 PictureIDStudentID P1S1 P1S2 P2S3

6 How to use complex tables What picture is Brittany in? Look up her ID in the student table Look up the corresponding PictureID in the PictureID- StudentID table Look up the picture in the Picture table Answer: Class1.jpg StudentStudentID KatieS1 BrittanyS2 CarrieS3 PicturePictureID Class1.jpgP1 Class2.jpgP2 PictureIDStudentID P1S1 P1S2 P2S3

7 Another Use Who 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 StudentNameStudentID KatieS1 BrittanyS2 CarrieS3 PicturePictureID Class1.jpgP1 Class2.jpgP2 PictureIDStudentID P1S1 P1S2 P2S3

8 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: Simple Containing only a single relation per row

9 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) PicturePictureID Class1.jpgP1 Class2.jpgP2 StudentNameStudentID KatieS1 BrittanyS2 CarrieS3 PictureIDStudentID P1S1 P1S2 P2S3

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

11 Relation loan nRelation borrower customer-nameloan-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 nNote: borrower information missing for L-260 and loan information missing for L-155

12 loan inner join borrower on loan.loan-number = borrower.loan-number loan left outer join borrower on loan.loan-number = borrower.loan-number branch-nameamount Downtown Redwood 3000 4000 customer-nameloan-number Jones Smith L-170 L-230 loan-number L-170 L-230 branch-nameamount Downtown Redwood Perryridge 3000 4000 1700 customer-nameloan-number Jones Smith null L-170 L-230 null loan-number L-170 L-230 L-260

13 loan natural inner join borrower nloan natural right outer join borrower branch-nameamount Downtown Redwood 3000 4000 customer-name Jones Smith loan-number L-170 L-230 branch-nameamount Downtown Redwood null 3000 4000 null customer-name Jones Smith Hayes loan-number L-170 L-230 L-155

14 loan full outer join borrower using (loan-number) nFind all customers who have either an account or a loan (but not both) at the bank. (customer_number, account_number, loan_number) branch-nameamount 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-name from (depositor natural full outer join borrower) where account-number is null or loan-number is null

15 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'

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

17 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

18 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 together This is referred to as “relating tables” The key is to make sure that they have a common field

19 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


Download ppt "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."

Similar presentations


Ads by Google