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.

Slides:



Advertisements
Similar presentations
Sometimes you need to use data from more than one table. In example1, the report displays data from two separate tables. Employee IDs exist in the EMPLOYEES.
Advertisements

Chapter 4 Joining Multiple Tables
Relational Databases - Amit Bhawnani & Nimesh Shah.
Chapter 4: Immediate SQL Complex Queries Complex Queries Views Views Modification of the Database Modification of the Database Joined Relations Joined.
Domain Relational Calculus and Query-by-Example CS157a John Eagle.
Relational Algebra – Basis for Relational Query Languages Based on presentation by Juliana Freire.
Chapter 2 Relational Model (part II) Hankz Hankui Zhuo
The Relational Database Model
©Silberschatz, Korth and Sudarshan3.1Database System Concepts Chapter 3: Relational Model Structure of Relational Databases Relational Algebra Tuple Relational.
©Silberschatz, Korth and Sudarshan3.1Database System Concepts Extended Relational-Algebra-Operations Generalized Projection Outer Join Aggregate Functions.
1 Basic SQL Prof. Sin-Min Lee Department of Computer Science.
CSE 190: Internet E-Commerce Lecture 10: Data Tier.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
Slides adapted from A. Silberschatz et al. Database System Concepts, 5th Ed. SQL - part 2 - Database Management Systems I Alex Coman, Winter 2006.
MySQL Tutorial (2) Introduction to Database. Banking Example branch (branch-name, branch-city, assets) customer (customer-name, customer-street, customer-city)
Relational Algebra. Relational Query Languages n Query = “retrieval program” n Language examples: ù Theoretical : 1. Relational Algebra 2. Relational.
Relational Model. 2 Structure of Relational Databases Fundamental Relational-Algebra-Operations Additional Relational-Algebra-Operations Extended Relational-Algebra-Operations.
Relational databases.  Retrieving data from a database requires pulling data from multiple tables  Tables relate to each other in distinct ways, modelled.
©Silberschatz, Korth and Sudarshan6.1Database System Concepts Chapter 6: Integrity and Security Domain Constraints Referential Integrity Assertions Triggers.
International Computer Institute, Izmir, Turkey SQL Asst.Prof.Dr.İlker Kocabaş UBİ502 at
Introduction –All information systems create, read, update and delete data. This data is stored in files and databases. Files are collections of similar.
Structured/System Query Language -- SQL September 20, 2004.
Lecture 6 Structured Query Language SQL Lecture 6 Structured Query Language SQL Instructor: Haya Sammaneh.
Basic Structure Given sets A 1, A 2,..., A n a relation r is a subset of A 1 x A 2 x... x A n Thus a relation is a set of n-tuples (a 1, a 2,..., a n )
PMIT-6102 Advanced Database Systems By- Jesmin Akhter Assistant Professor, IIT, Jahangirnagar University.
Structured Query Language 2 Presented by: Annisa, M.Kom. Source: Database System Concepts 5 th edition.
Relational Model: Examples. Banking Example branch (branch_name, branch_city, assets) customer (customer_name, customer_street, customer_city) account.
Learningcomputer.com SQL Server 2008 – Entity Relationships in a Database.
Advanced SQL Murat Kantarcioglu Adapted from Silberchatz et al. slides.
IE 423 – Design of Decision Support Systems Database development – Relationships and Queries.
Chapter 9 Joining Data from Multiple Tables
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Modification of the Database – Deletion Delete all tuples from the loan relation. delete.
©Silberschatz, Korth and Sudarshan2.1Database System Concepts - 5 th Edition, Oct 5, 2006 Outer Join n An extension of the join operation that avoids loss.
M Taimoor Khan Course Objectives 1) Basic Concepts 2) Tools 3) Database architecture and design 4) Flow of data (DFDs)
Relational Databases Database Driven Applications Retrieving Data Changing Data Analysing Data What is a DBMS An application that holds the data manages.
1 The Relational Model By Elena Ciriani CS157A February 19, 2004 Professor Lee.
Computing & Information Sciences Kansas State University Monday, 04 Feb 2008CIS 560: Database System Concepts Lecture 6 of 42 Monday, 04 February 2008.
©Silberschatz, Korth and Sudarshan5.1Database System Concepts Chapter 5: Other Relational Languages Query-by-Example (QBE)
3.1Database System Concepts Chapter 3: Relational Model Structure of Relational Databases Relational Algebra Tuple Relational Calculus Domain Relational.
® Microsoft Access 2010 Tutorial 9 Using Action Queries and Advanced Table Relationships.
Computing & Information Sciences Kansas State University Monday, 08 Sep 2008CIS 560: Database System Concepts Lecture 5 of 42 Monday, 08 September 2008.
Relational Algebra  Souhad M. Daraghma. Relational Query Languages Query languages: Allow manipulation and retrieval of data from a database. Relational.
Relational Model By Dr.S.Sridhar, Ph.D.(JNUD), RACI(Paris, NICE), RMR(USA), RZFM(Germany) DIRECTOR ARUNAI ENGINEERING COLLEGE TIRUVANNAMALAI.
XP Chapter 3 Succeeding in Business with Microsoft Office Access 2003: A Problem-Solving Approach 1 Analyzing Data For Effective Decision Making Chapter.
Advanced Database Systems By- Jesmin Akhter Assistant Professor, IIT, Jahangirnagar University.
Triggers. Why Triggers ? Suppose a warehouse wishes to maintain a minimum inventory of each item. Number of items kept in items table Items(name, number,...)
Tutorial 9 Using Action Queries and Advanced Table Relationships.
Access Review. Access Access is a database application A database is a collection of records and files organized for a particular purpose Access supports.
IS 230Lecture 6Slide 1 Lecture 7 Advanced SQL Introduction to Database Systems IS 230 This is the instructor’s notes and student has to read the textbook.
Source: Database System Concepts, Silberschatz etc Edited: Wei-Pang Yang, IM.NDHU, Introduction to Database CHAPTER 5 Other Relational Languages.
Chapter 4: SQL Complex Queries Complex Queries Views Views Modification of the Database Modification of the Database Joined Relations Joined Relations.
2.1 Chapter 2: Relational Model. 2.2 Chapter 2: Relational Model Structure of Relational Databases Fundamental Relational-Algebra-Operations Additional.
©Silberschatz, Korth and Sudarshan3.1Database System Concepts Chapter 3: Relational Model Structure of Relational Databases Relational Algebra Extended.
©Silberschatz, Korth and Sudarshan3.1Database System Concepts Extended Relational-Algebra-Operations Generalized Projection Aggregate Functions Outer Join.
Computing & Information Sciences Kansas State University Friday, 08 Sep 2006CIS 560: Database System Concepts Lecture 07 of 42 Tuesday, 30 January 2007.
Computing & Information Sciences Kansas State University Wednesday, 03 Sep 2008CIS 560: Database System Concepts Lecture 3 of 42 Wednesday, 03 September.
MySQL Tutorial Introduction to Database. Introduction of MySQL  MySQL is an SQL (Structured Query Language) based relational database management system.
Relational Algebra HW2 Turn in as a hardcopy at the start of next class period. You may work this assignment in groups.
1 Session 3 Welcome: To session 3-the fourth learning sequence “Relational algebra “ Recap : In the previous learning sequences, we discussed the four.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
ASET Relational Algebra continues…. ASET Rename Operation Allows us to name, and therefore to refer to, the results of relational-algebra expressions.
CHAPTER 2 : RELATIONAL DATA MODEL Prepared by : nbs.
IS 230Lecture 7Slide 1 Relational Algebra Lecture 8 Text Book – Chapter.
Chapter 3: Relational Model III Additional Relational Algebra Operations Additional Relational Algebra Operations Views Views.
Week 2 Lecture The Relational Database Model Samuel ConnSamuel Conn, Faculty Suggestions for using the Lecture Slides.
Database System Concepts ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: SQL.
Query Languages Language in which user requests information from the database. Categories of languages Procedural Non-procedural, or declarative “Pure”
Relational Model By Dr.S.Sridhar, Ph.D.(JNUD), RACI(Paris, NICE), RMR(USA), RZFM(Germany)
Session 3 Welcome: To session 3-the fifth learning sequence
Introduction to Database
Presentation transcript:

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

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

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

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

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

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

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

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

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)

Relation loan nRelation borrower customer-nameloan-number Jones Smith Hayes L-170 L-230 L-155 amount 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

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 customer-nameloan-number Jones Smith L-170 L-230 loan-number L-170 L-230 branch-nameamount Downtown Redwood Perryridge customer-nameloan-number Jones Smith null L-170 L-230 null loan-number L-170 L-230 L-260

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

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

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'

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)

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

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

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