0% found this document useful (0 votes)
7 views

STADVDB Slides 02a - Working with Multiple Tables

The document provides an overview of working with multiple tables in relational databases, emphasizing the importance of primary and foreign keys to prevent data redundancy. It explains various types of joins, including inner, outer, and self joins, and how they are used to query data across multiple tables. Additionally, it covers concepts like referential integrity constraints and Cartesian products, highlighting best practices for generating reports from scattered data.

Uploaded by

joe goldberg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

STADVDB Slides 02a - Working with Multiple Tables

The document provides an overview of working with multiple tables in relational databases, emphasizing the importance of primary and foreign keys to prevent data redundancy. It explains various types of joins, including inner, outer, and self joins, and how they are used to query data across multiple tables. Additionally, it covers concepts like referential integrity constraints and Cartesian products, highlighting best practices for generating reports from scattered data.

Uploaded by

joe goldberg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

WORKING WITH MULTIPLE TABLES

Introduction to Database
Software Technology Department
College of Computer Studies
De La Salle University
Recall: Relational Database
• Data is scattered across multiple tables

Primary Key

Foreign Keys
Recall: Relational Database
City Language

Country

Why are the data scattered?


Recall: Relational Database
Foreign Key Primary Key

To prevent data redundancy that may lead to inconsistency!


Foreign Key

• Motivation:
– To reduce redundancy, data are stored in several
tables in a database
– A foreign key is used to relate the “scattered” data

• Definition:
– A set of at least 1 attribute within one table that
matches the candidate key of another table
Referential Integrity Constraints
– When a Foreign Key exists, the value must match a
candidate key value of some tuple (row) in its home /
parent table
– Or, the Foreign Key can have a NULL value
Department No. Name
D01 Software Technology
D02 Information Technology

Employee ID Name SSS # Department No.


(foreign key)
E0001 Thomas Conolly 000-000-011 D01

E0002 Carolyn Begg 000-000-012 D02


How to generate reports containing
data from multiple sources?
List all countries & their capital. List the details of all cities in Asia.
• country.name, • city.name, city.population,
• country.capital => city.name • city.countrycode =>
country.name,
• country.continent = “Asia”
How to generate reports containing
data from multiple sources?

• List the country name and


population count who speaks the
“English” language.
• country.name,
• country.population *
countrylanguage.percentage,
• countrylanguage.language =
“English”
Joining Tables
• The JOIN feature enables you to
– Query data from more than one table
– Gather and manipulate data from across the multiple
tables
• Using common values, typically primary and foreign key values
SQL Join

Outer
Inner Join Self Join Cross Join
Join

Non- Right
Equi-Join Left outer Full outer
Equijoin outer

https://www.javatpoint.com/sql-server-joins
https://medium.com/@cmukesh8688/data-science-sql-join-4472e70ff21c
Cartesian or Cross Join
• Combines all rows from all the tables in
the FROM clause
SELECT *
FROM table1, table2

Each row in table1 is combined with each


row in table 2
If table1 has 5 rows and table2 has 6 rows,
the query above would produce 5 x 6 or 30
rows
Cartesian Product

• A Cartesian product is formed when --


– A join condition is omitted
– A join condition is invalid
– All rows in the first table are joined to all
rows in the second table
• To avoid a Cartesian product, always include a
valid join condition in a WHERE clause
Natural Join or Equi-Join
Matches tuples from two tables based on
same attribute names and data types, and
retains only one copy of each common column

Three main Inner Join


Matches tuples from two tables based on an
types of explicitly specified Join condition in the ON
Joins clause, and includes both columns in the result

Outer Join
Avoids loss of information by adding tuples
from one relation that does not match tuples
in the other relation to the result of the join
Equi-joins

SELECT columna, columnb


FROM table1, table2
WHERE columnx = columny

• Values in a column of table1 is matched to


the corresponding values in table2
• The Join Condition is in the WHERE clause
Equi-joins
SERVER

SALES_EMP SALES_DEPT
LNAME DEPT_ID ID NAME
--------- ------- -- ---------------
Velasquez 50 50 Administration
Ngao 41 41 Operations
Nagayama 31 31 Sales
Ropeburn 50 50 Administration
Urguhart 41 41 Operations
Menchu 42 42 Operations
Biri 43 43 Operations
Havel 45 45 Operations
... ...
Equi-joins
• Three or more tables can be joined together
• Given X tables in the FROM clause, the total number
of join conditions in the WHERE clause is X-1

List the current managers of all departments.

SELECT E.last_name, E.first_name, D.dept_name


FROM employees E, departments D, dept_manager DM
WHERE E.emp_no = DM.emp_no AND
D.dept_no = DM.dept_no AND
DM.to_date IS NOT NULL
Other constructs for Equi-Joins
• NATURAL JOIN
select productname, categoryname
from products P natural join categories C

• INNER JOIN
select productname, categoryname
from products P inner join categories C
on P.categoryid = C.categoryid
Self Joins
Server

SALES_EMP (WORKER) SALES_EMP (MANAGER)


LAST_NAME MANAGER_ID ID LAST_NAME
--------- ---------- -- ----------
Ngao 1 1 Velasquez
Nagayama 1 1 Velasquez
Ropeburn 1 1 Velasquez
Urguhart 2 2 Ngao
Menchu 2 2 Ngao
Biri 2 2 Ngao
Magee 3 3 Nagayma
Giljum 3 3 Nagayma
... ...
Self Joins

• Join rows in a table to rows in the same table.


• Simulate two tables in the FROM clause by
creating two aliases for the table.

List all employees and their supervisors.

SELECT worker.lname “worker”,


manager.lname “superior”
FROM sales_emp worker, sales_emp manager
WHERE worker.manager_id = manager.id
Non-Equi-joins
• When no column in one table corresponds
directly to a column in the second table
• The Join Condition contains an operator
other than equal (=)

SELECT E.lname, E.title, E.salary,


S.grade
FROM sales_emp E, salary_grade S
WHERE E.salary BETWEEN
(S.lo_salary AND S.hi_salary)
Outer Joins
• Used to see rows that do not normally
meet the Join Condition, i.e.,
“Show me all rows even if something is
missing”

• Occurs when the foreign key has the


NULL value
Outer Joins
Server

SALES_EMP SALES_CUSTOMER

SALES_
LAST_NAME ID REP_ID NAME
--------- -- ------ --------------
Magee 11 11 Womansport
Magee 11 11 Beisbol Si!
Magee 11 11 Ojibway Retail
Giljum 12 12 Unisports
Giljum 12 12 Futbol Sonora
No Sales Rep Sedeghi 13 13 Hamada Sport
Assigned to Dumas 15 15 Sportique
Sweet Rock Sweet Rock Sports
Sports
Types of Outer Joins
Left Outer Join

Course

Right Outer Join

Pre-req

Full Outer Join

Silberschatz, Korth and Sudarshan, 2019

You might also like