STADVDB Slides 02a - Working with Multiple Tables
STADVDB Slides 02a - 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
• 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
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
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
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
• INNER JOIN
select productname, categoryname
from products P inner join categories C
on P.categoryid = C.categoryid
Self 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
Pre-req