You are on page 1of 18

JOINS

&
DATABASE DESIGN
Mdm Hana.
PERFORMING JOINS

A special query statement to


retrieve the information.
Joins – SQL queries performed by
cross referencing tables
Use to extract more usable data from
relational database
Several types of joins
Inner join
Outer or left join
JOINS QUERY

Inner join
 Will retrieve all of the information from both the table
A and table B only where a match is made.
 Example query:
 SELECT * FROM urls, url_types WHERE urls.type_id =
url_types.type_id;

 Retrieve all information from both urls and url_types tables


wherever a urls.type_id is the same as the url_types.type_id.
 When selecting from multiple tables and columns, use
the dot syntax (table.column) if they have columns
with same name.
EXAMPLE INNER JOIN

SELECT url_id, type FROM urls AS u, url_types AS t WHERE


u.type_id = t.type_id;
JOINS QUERY
Outer or left join
 Could return records not matched by a conditional
only where a match is made.
 Example query:
 SELECT * FROM url_types LEFT JOIN urls ON
urls.type_id = url_types.type_id;
 Retrieve all of the url_types record and urls information, if a
match is made.
 If both tables have same column name, simplify the query
into:
 SELECT * FROM url_types LEFT JOIN urls USING
(type_id);
EXAMPLE LEFT JOIN
SELECT url_id,type FROM url_types LEFT JOIN urls ON urls.type_id =url_types.type_id;
DATABASE DESIGN

What kind of information an organization want to


collect information about?
-> Determine the Entities
The common entities are:
 Customers
 Suppliers
 Employees / Staff
 Product
 Etc.
DATABASE DESIGN
What specific information about each entity is
important?
-> Determine the attributes
• Example: For the Employee entity attributes may
include:
Employee name
Address
Hourly rate
Etc.
DATABASE DESIGN

How to organize the data? Which


type of database “model” to
use?
Data is organized as a link set of
tables.
DATABASE DESIGN

For your project add :


Username
Password
to the employees table
DATABASE DESIGN
4. What are the important aspects of a data table?

 Terminology for data tables


 Field (i.e. column)
 Attribute of the entity
 Record (i.e. row)
 An instance of an entity
 File (i.e., table)
 Collection of records for an entity
 Primary key
 Unique identifier for each record
 Often a unique “ID code” is created
DATABASE DESIGN
What are the relationships between the entities?
 A database involves tables that are linked together
 When building a database, need to know how to link
the tables
 i.e., Identify how the entities are related!
 e.g., A Supplier provides a Product.
Identify who the supplier is, then you
can find all the products they have
supplied to you.
For any product, you should be able
to find the supplier’s address.
DATABASE DESIGN

What are the relationships between the entities? (cont.)


 Possible “relationship types”
 A) one to one, B) one to many, c) many to many
 “one” means “at most one”
 “many” means “more than one”
 Example:
 Entities: Supplier, Product
 What is the relationship type? We are told that:
 A supplier can provide many different kinds of products.
 A product can be supplied by a max of one supplier.
 Entity-Relationship Diagram:
DATABASE DESIGN

Relationship symbols: these all mean the same thing”


Some textbooks use “crowfoot”
symbols

Access uses “1 – ∞” symbols

Access uses “ – ∞” symbols


DATABASE DESIGN

 Foreign key
 Serves as a “link” between data files/tables
 A field in one file/table that serves as a primary
key in another file/table
 How to determine the foreign key?
 For a “one to many” relationship, the primary
key on the “one” side of the relationship is
added to the table on the “many” side
DATABASE DESIGN
Supplier ID Company Name Street Address City State

Supplier 1001 ACME Inc. 123 Easy St. Deming WA


table 1002 XYZ Corporation 456 Maple St. Bellingham WA

1003 ProductCo Inc. 389 Main St. Seattle WA

… … … … …

Product
table

Category
table
TERMINOLOGY SUMMARY

Easy to Academic
understand
The terms in each row
Table (File) Entity / have very similar meanings.
Relation

Column Field Attribute

Row Record Instance

You might also like