Relational example using donor, donation and drive tables For additional information see the speaker notes!

Slides:



Advertisements
Similar presentations
Relational database - student system As always please use speaker notes!
Advertisements

© Abdou Illia MIS Spring 2014
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.
© 2007 by Prentice Hall (Hoffer, Prescott & McFadden) 1 Joins and Sub-queries in SQL.
A Guide to SQL, Seventh Edition. Objectives Use joins to retrieve data from more than one table Use the IN and EXISTS operators to query multiple tables.
Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi.
Displaying Data from Multiple Tables
PL/SQL User Defined Types Record and Table Please use speaker notes for additional information!
Reports Using SQL Script Please check speaker notes for additional information!
Order Entry System Please use speaker notes for additional information!
Group functions using SQL Additional information in speaker notes!
More on IF statements Use speaker notes for additional information!
Inner join, self join and Outer join Sen Zhang. Joining data together is one of the most significant strengths of a relational database. A join is a query.
Relational example using donor, contrib and drive tables Please use speaker notes for additional information!
Displaying Data from Multiple Tables. Obtaining Data from Multiple Tables Sometimes you need to use data from more than one table. In the example, the.
SQL Use of Functions Character functions Please use speaker notes for additional information!
SQL functions - numeric and date Speaker notes contain additional information!
Cursors in PL/SQL Includes cursor example and continuation of first cursor example Please use speaker notes for additional information!
Relational databases and third normal form As always click on speaker notes under view when executing to get more information!
More on variables with Oracle’s SQL*Plus As always, speaker notes will contain additional information!
Chapter 9 Joining Data from Multiple Tables
ITBIS373 Database Development
More on relational databases, including 1 to 1, 1 to many and many to many relationships Please use speaker notes for additional information!
XML, DTD, ENTITY Please see speaker notes for additional information!
M Taimoor Khan Course Objectives 1) Basic Concepts 2) Tools 3) Database architecture and design 4) Flow of data (DFDs)
Relationships and Advanced Query Concepts Using Multiple Tables Please use speaker notes for additional information!
XML and DTD Please you speaker notes for additional information!
Created by, Author Name, School Name—State FLUENCY WITH INFORMATION TECNOLOGY Skills, Concepts, and Capabilities.
SQL- DQL (Oracle Version). 2 SELECT Statement Syntax SELECT [DISTINCT] column_list FROM table_list [WHERE conditional expression] [GROUP BY column_list]
Examples dealing with cursors and tables Please use speaker notes for additional information!
Join, Subqueries and set operators. Obtaining Data from Multiple Tables EMPLOYEES DEPARTMENTS … …
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
More on views Please refer to speaker notes for additional information!
SQL and Conditions Speaker notes will provide additional information!
Manipulating data within PL/SQL Please use speaker notes for additional information!
Exceptions in PL/SQL Please use speaker notes for additional information!
Indexes in Oracle An Introduction Please check speaker notes for additional information!
Database Programming Section 15 – Oracle Proprietary Join Syntax and Review 1.
Introduction to Oracle - SQL Additional information is available in speaker notes!
Introduction to Databases. What is a database?  A database program is nothing more than an electronic version of a 3x5 card file  A database is defined.
More on Primary and Foreign Keys Please see speaker notes for additional information!
Relational Database in Access Student System As always please use speaker notes!
Introduction to PL/SQL As usual, use speaker notes for additional information!
Using a Database Access97 Please use speaker notes for additional information!
PHP with MYSQL Please use speaker notes for additional information!
More SQL functions As usual,there is additional information in the speaker notes!
Copyright © 2016 Pearson Education, Inc. CHAPTER 7: ADVANCED SQL (PART I) Modern Database Management 12 th Edition Jeff Hoffer, Ramesh Venkataraman, Heikki.
Lec-7. The IN Operator The IN operator allows you to specify multiple values in a WHERE clause. SQL IN Syntax SELECT column_name(s) FROM table_name WHERE.
CSC314 DAY 9 Intermediate SQL 1. Chapter 6 © 2013 Pearson Education, Inc. Publishing as Prentice Hall USING AND DEFINING VIEWS  Views provide users controlled.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
Oracle Join Syntax.
Introduction to Triggers
More on Procedures (Internal/Local procedures)
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
Introduction to Procedures
Introduction to the Array
MySQL - Creating donorof database offline
Introduction to Functions
Displaying Data from Multiple Tables Using Joins
JOINs JOINs can be used to combine tables A CROSS JOIN B
Using SQL with Access I am adding queries to the stu table in SecondDB.accdb – a database that I created in class. SQL stands for structured query language.
Introduction to Views and Reports
Oracle Join Syntax.
Looking at using multiple tables
More and Still More on Procedures and Functions
Oracle Join Syntax.
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
Presentation transcript:

Relational example using donor, donation and drive tables For additional information see the speaker notes!

SQL> DESC donor; Name Null? Type IDNO VARCHAR2(5) NAME VARCHAR2(15) STADR VARCHAR2(15) CITY VARCHAR2(10) STATE VARCHAR2(2) ZIP VARCHAR2(5) DATEFST DATE YRGOAL NUMBER(7,2) CONTACT VARCHAR2(12) SQL> SELECT * FROM donor; IDNO NAME STADR CITY ST ZIP DATEFST YRGOAL CONTACT Stephen Daniels 123 Elm St Seekonk MA JUL John Smith Jennifer Ames 24 Benefit St Providence RI MAY Susan Jones Carl Hersey 24 Benefit St Providence RI JAN-98 Susan Jones Susan Ash 21 Main St Fall River MA MAR Amy Costa Nancy Taylor 26 Oak St Fall River MA MAR John Adams Robert Brooks 36 Pine St Fall River MA APR Amy Costa 6 rows selected. Donor table

Donation table SQL> DESC donation; Name Null? Type IDNO VARCHAR2(5) DRIVENO VARCHAR2(3) CONTDATE DATE CONTAMT NUMBER(6,2) SQL> SELECT * FROM donation; IDNO DRI CONTDATE CONTAMT JAN FEB MAR MAR MAR JUN JUN JUN rows selected.

SQL> DESC drive; Name Null? Type DRIVENO VARCHAR2(3) DRIVENAME VARCHAR2(15) DRIVECHAIR VARCHAR2(12) LASTYEAR NUMBER(8,2) THISYEAR NUMBER(8,2) SQL> SELECT * FROM drive; DRI DRIVENAME DRIVECHAIR LASTYEAR THISYEAR Kids Shelter Ann Smith Animal Home Linda Grant Health Aid David Ross Half Way Robert Doe 0 0 Drive table

Relationships SQL> SELECT * FROM donor; IDNO NAME STADR CITY ST ZIP DATEFST YRGOAL CONTACT Stephen Daniels 123 Elm St Seekonk MA JUL John Smith Jennifer Ames 24 Benefit St Providence RI MAY Susan Jones Carl Hersey 24 Benefit St Providence RI JAN-98 Susan Jones Susan Ash 21 Main St Fall River MA MAR Amy Costa Nancy Taylor 26 Oak St Fall River MA MAR John Adams Robert Brooks 36 Pine St Fall River MA APR Amy Costa SQL> SELECT * FROM donation; IDNO DRI CONTDATE CONTAMT JAN FEB MAR MAR MAR JUN JUN JUN SQL> SELECT * FROM drive; DRI DRIVENAME DRIVECHAIR LASTYEAR THISYEAR Kids Shelter Ann Smith Animal Home Linda Grant Health Aid David Ross Half Way Robert Doe 0 0 The relationship or link between the donor table and the donation table is based on idno. In other words, the idno appears in both tables. The relationship or link between the donation table and the drive table is based on driveno. In other words, the driveno appears in both tables. NOTE: There is no direct link between the donor table and the drive table.

Types of relationships In a relational database there are the following kinds of relationships: One to One One to Many Many to Many

SQL> SELECT * FROM one_donor; IDNO NAME STADR CITY ST ZIP DATEFST YRGOAL CONTACT Stephen Daniels 123 Elm St Seekonk MA JUL John Smith Jennifer Ames 24 Benefit St Providence RI MAY Susan Jones Carl Hersey 24 Benefit St Providence RI JAN-98 Susan Jones Susan Ash 21 Main St Fall River MA MAR Amy Costa Nancy Taylor 26 Oak St Fall River MA MAR John Adams Robert Brooks 36 Pine St Fall River MA APR Amy Costa 6 rows selected. One to One relationship One to One Relationship: If the donation table only allowed one donation for every donor then we would have a one to one relationship. SQL> SELECT * 2 FROM one_donation 3 ORDER BY idno; IDNO DRI CONTDATE CONTAMT JAN FEB MAR MAR MAR NOV rows selected.

One to Many relationship SQL> SELECT * FROM one_donor; IDNO NAME STADR CITY ST ZIP DATEFST YRGOAL CONTACT Stephen Daniels 123 Elm St Seekonk MA JUL John Smith Jennifer Ames 24 Benefit St Providence RI MAY Susan Jones Carl Hersey 24 Benefit St Providence RI JAN-98 Susan Jones Susan Ash 21 Main St Fall River MA MAR Amy Costa Nancy Taylor 26 Oak St Fall River MA MAR John Adams Robert Brooks 36 Pine St Fall River MA APR Amy Costa 6 rows selected. One to Many Relationship: In this example, one donor can give multiple donations but each donation only has one donor. SQL> SELECT * 2 FROM donation 3 ORDER BY idno; IDNO DRI CONTDATE CONTAMT JAN JUN FEB JUN MAR MAR JUN MAR rows selected. NOTE: There is no donation for in this example.

SQL> SELECT * 2 FROM rel_movie; MOVIENO MOVIENAME SQL movie 2222 Oracle movie 3333 BCC movie Many to Many Relationship: In this example, each movie can have many stars and each star can have many movies, therefore this is a many to many relationship. Many to many relationship SQL> SELECT * 2 FROM 3 movie_star; MOVIENO STARNO SQL> SELECT * 2 FROM rel_star; STARNO STARNAME Stephen Davis 2345 Jennifer Ames 3456 Carl Hersey 4567 Maureen Taylor Bridge table - used with many to many.

Keys - primary PRIMARY KEY Each table should have a primary key that identifies one and only one record. Sometimes the primary key is one column/field, sometimes it is a combination of column/fields. In this example it is one column/field, the IDNO. SQL> SELECT * FROM donor; IDNO NAME STADR CITY ST ZIP DATEFST YRGOAL CONTACT Stephen Daniels 123 Elm St Seekonk MA JUL John Smith Jennifer Ames 24 Benefit St Providence RI MAY Susan Jones Carl Hersey 24 Benefit St Providence RI JAN-98 Susan Jones Susan Ash 21 Main St Fall River MA MAR Amy Costa Nancy Taylor 26 Oak St Fall River MA MAR John Adams Robert Brooks 36 Pine St Fall River MA APR Amy Costa 6 rows selected In the donor table, it can be seen that there is a different IDNO for each record/row. In other words, the IDNO uniquely identifies a record/row. If I do a SELECT using a where for a particular IDNO, it will only return one record.

Keys - primary SQL> SELECT * 2 FROM donation 3 ORDER BY idno; IDNO DRI CONTDATE CONTAMT JAN JUN FEB JUN MAR MAR JUN MAR rows selected. In this example, the primary key is not as easy to determine. Clearly the IDNO does not work as the primary key because there are multiple of the same IDNO. This means IDNO needs to be combined with something else. If we combine it with DRIVENO, it will work for these 8 records because there is no record/row where the IDNO plus the DRIVENO are the same. However, it is very possible that a donor could contribute multiple times to the same drive so this is not sufficient. If we combine IDNO plus DRIVE plus CONTDATE as the primary key we will be as long as we decide that the same donor cannot contribute to the same drive on the same date. That is reasonable so we will go with the combination of those three fields to make the primary key.

Keys - primary SQL> SELECT * FROM drive; DRI DRIVENAME DRIVECHAIR LASTYEAR THISYEAR Kids Shelter Ann Smith Animal Home Linda Grant Health Aid David Ross Half Way Robert Doe 0 0 In this example, the primary key is DRIVENO. DRIVENO uniquely defines each record/row in the table. Another drive with the same drive number would not make sense in this table.

Keys - foreign SQL> SELECT * 2 FROM donation 3 ORDER BY idno; IDNO DRI CONTDATE CONTAMT JAN JUN FEB JUN MAR MAR JUN MAR rows selected. SQL> SELECT * FROM drive; DRI DRIVENAME DRIVECHAIR LASTYEAR THISYEAR Kids Shelter Ann Smith Animal Home Linda Grant Health Aid David Ross Half Way Robert Doe 0 0 The DRIVENO on the donation table is called a foreign key because it links into the primary key on the drive table.

SQL> SELECT idno, donation.driveno, drivename, contamt 2 FROM donation, drive 3 WHERE donation.driveno = drive.driveno; IDNO DRI DRIVENAME CONTAMT Kids Shelter Kids Shelter Kids Shelter Kids Shelter Animal Home Animal Home Health Aid Health Aid 10 8 rows selected. Donation table: IDNO DRI CONTDATE CONTAMT JAN JUN FEB JUN MAR MAR JUN MAR Select Drive table: DRI DRIVENAME DRIVECHAIR LASTYEAR THISYEAR Kids Shelter Ann Smith Animal Home Linda Grant Health Aid David Ross Half Way Robert Doe 0 0 The select takes the idno from the donation table, the driveno from the donation table, the drivename from the drive table and the contamt from the donation table. The driveno from the donation table links to the matching driveno in the drive table and retrieves the drive name from that row.

SQL> SELECT donation.idno, donation.driveno, drive.drivename, donation.contamt 2 FROM donation, drive 3 WHERE donation.driveno = drive.driveno; IDNO DRI DRIVENAME CONTAMT Kids Shelter Kids Shelter Kids Shelter Kids Shelter Animal Home Animal Home Health Aid Health Aid 10 8 rows selected. Select In the SELECT for this example, all columns are presented with the table name.column name format. This is a good way to code for clarity. The person reading the code knows exactly where the data is coming from. The link is made between the two tables. The donation.driveno will be linked by driveno to the drive.driveno so that the name for the drive can be retrieved. The FROM clause lists the table used for this query.

Select SQL> SELECT d.idno, d.driveno, r.drivename, d.contamt 2 FROM donation d, drive r 3 WHERE d.driveno = r.driveno; IDNO DRI DRIVENAME CONTAMT Kids Shelter Kids Shelter Kids Shelter Kids Shelter Animal Home Animal Home Health Aid Health Aid 10 8 rows selected. In this example, I am using aliases. The alias gets set up in the FROM clause when the table names are listed. The alias is then used in the SELECT and in the WHERE. This provides a shortened version of the table name.

Select SQL> SELECT donation.idno, donor.name, donation.driveno, drive.drivename, donation.contamt 2 FROM donation, donor, drive 3 WHERE donation.idno = donor.idno AND donation.driveno = drive.driveno; IDNO NAME DRI DRIVENAME CONTAMT Stephen Daniels 100 Kids Shelter Susan Ash 100 Kids Shelter Carl Hersey 100 Kids Shelter Jennifer Ames 100 Kids Shelter Jennifer Ames 200 Animal Home Stephen Daniels 200 Animal Home Nancy Taylor 300 Health Aid Susan Ash 300 Health Aid 10 8 rows selected. This involves three tables: donation donor drive The donation table is being processed, for each donation I want to link into the donor table to get the donor name and link into the drive table to get the drive name. These links are handled in the WHERE clause where I establish the relationship between the idno on the donation table and the idno on the donor table as well as the relationship between the driveno on the donation table and the driveno on the drive table. Once the links have been established the SELECT will retrieve the appropriate data according to the link paths.

JOIN In a relational database, frequently you need to connect the data in two or more tables. The process that is used to do this is called a join. There are four kinds of joins that we will look at in this presentation: equijoins non-equijoins outer joins self joins

Equijoin SQL> SELECT donation.idno, donation.driveno, drive.drivename, donation.contamt 2 FROM donation, drive 3 WHERE donation.driveno = drive.driveno; IDNO DRI DRIVENAME CONTAMT Kids Shelter Kids Shelter Kids Shelter Kids Shelter Animal Home Animal Home Health Aid Health Aid 10 8 rows selected. The examples that we have been looking at are equijoins. The WHERE clause establishes the link between tables and the values being compared in the two tables must be equal.

Non-equijoin SQL> SELECT * from grade; NAME GRADELEVEL First Grade 1 Second Grade 2 Third Grade 3 Seventh Grade 7 Freshman 9 Senior 12 SQL> SELECT * FROM schools; GROUPNAME LOWGRADE HIGHGRADE Elementary 1 6 Junior High 7 8 High School 9 12 SQL> SELECT grade.name, grade.gradelevel, schools.groupname 2 FROM grade, schools 3 WHERE grade.gradelevel BETWEEN schools.lowgrade AND schools.highgrade; NAME GRADELEVEL GROUPNAME First Grade 1 Elementary Second Grade 2 Elementary Third Grade 3 Elementary Seventh Grade 7 Junior High Freshman 9 High School Senior 12 High School 6 rows selected. Looking at the second record on the grade, the 2 is checked against the lowgrade and highgrade on schools. It falls between 1 and 6 so the name associated with the 1 and 6, Elementary is displayed.

Outer join SQL> SELECT donation.driveno, donation.contamt, drive.driveno, drive.drivename 2 FROM donation, drive 3 WHERE donation.driveno(+) = drive.driveno; DRI CONTAMT DRI DRIVENAME Kids Shelter Kids Shelter Kids Shelter Kids Shelter Animal Home Animal Home Health Aid 400 Half Way 9 rows selected. SQL> SELECT * FROM donation; IDNO DRI CONTDATE CONTAMT JAN FEB MAR MAR MAR JUN JUN JUN SQL> SELECT driveno, drivename FROM drive; DRI DRIVENAME Kids Shelter 200 Animal Home 300 Health Aid 400 Half Way Driveno 400 is not listed on the donation table. It is listed on the drive table. Driveno 400 is missing on the donation side but it is present on the drive side. Therefore in setting up the test, the (+) is put on the donation side to indicate that the data may be missing on this side. In the output, the drive information shows up even though there is no matching donation information.

equijoin SQL> SELECT donation.driveno, donation.contamt, drive.driveno, drive.drivename 2 FROM donation, drive 3 WHERE donation.driveno = drive.driveno; DRI CONTAMT DRI DRIVENAME Kids Shelter Kids Shelter Kids Shelter Kids Shelter Animal Home Animal Home Health Aid 8 rows selected. This is a continuation of the previous slide, notice that there is no (+) in the where. Therefore drive 400 does not appear in the results. This was matching on equality and drive 400 does not have any matches on the donation side.

Self join SQL> SELECT * FROM boss; IDNO NAME MANAGER John Doe Mary Smith David Wall Susan Ash Linda Jones 66 Roger Costa 55 SQL> SELECT fst.idno, fst.name, fst.manager, snd.idno, snd.name 2 FROM boss fst, boss snd 3 WHERE fst.manager = snd.idno 4 ORDER BY fst.idno; IDNO NAME MANAGER IDNO NAME John Doe David Wall 22 Mary Smith Linda Jones 33 David Wall Mary Smith 44 Susan Ash Mary Smith 66 Roger Costa Linda Jones The goal of this select is to list the manager name for each person. Notice that since Linda Jones has no manager, she is not listed.

Self join/outer join SQL> SELECT * FROM boss; IDNO NAME MANAGER John Doe Mary Smith David Wall Susan Ash Linda Jones 66 Roger Costa 55 SQL> SELECT fst.idno, fst.name, fst.manager, snd.idno, snd.name 2 FROM boss fst, boss snd 3 WHERE fst.manager = snd.idno(+) 4 ORDER BY fst.idno; IDNO NAME MANAGER IDNO NAME John Doe David Wall 22 Mary Smith Linda Jones 33 David Wall Mary Smith 44 Susan Ash Mary Smith 55 Linda Jones 66 Roger Costa Linda Jones Manager has a null that does not appear in the idno field. This code assures that Linda Jones who has no manager will appear.