Relational Databases
Relational Databases
Brian Panulla
Web 2004 Pre-Conference
Administrivia
Objectives
Agenda
Prerequisites
Schedule
Logistics
Presenter Introduction
Attendee Introductions
Objectives
The primary objective of this session is to
teach the basics of working with common
Relational Databases, including data
modeling and database design techniques.
Start 1:00 PM
Break (approximate) 3:00 PM
Adjourn 5:00 pm
Logistics
• Restrooms • Messages/phones
• Drinking fountains, • Security
refreshments, snacks • Emergency measures
• Laptops
Presenter
Brian Panulla
B.S. Science, PSU (2000)
E-mail: [email protected]
Attendees
Your name
Organization name
Current position
Background in databases
Expectations
Questions?!?!
Ask away!
Lesson 1:
Database Concepts
Lesson 1 Objectives
A. Discuss database concepts and
terminology
B. Learn database design principles
Database Terminology
Database
Table (or relation, entity)
Row (or record, tuple)
Column (or field, attribute)
Data value
What is a database?
Common databases
Database tasks:
Retrieving
Sorting
Summarizing
Inserting
Updating
Deleting
Common DB software
What is a table?
Tables are the fundamental component of
any relational database.
A table stores the data corresponding to a
specific type of object
For example:
A Students table would store student information
An Orders table would store customer order
information
What is a table?
Tables are made up of rows and columns
The columns of a table describe the
characteristics of the information stored in
that table
The data in each row belongs to a given
instance of the type of data stored in a
particular table
What is a table?
Each row contains one data value per
column.
The range of values that can go into a
particular column of a row is called the
domain of that column, and is generally
restricted to data of a specific type (integers,
character data, dates, etc.)
Sample Database Design
Beth Adams 5251 Electric Avenue Lewistown, PA 17044 (814) 555-0165 Engineering
Maggie Taylor 227 S. Allen Street State College, PA 16803 (814) 555-0771 Marketing
Matt Peterson 212 S. Allen Street State College, PA 16803 (814) 555-1111 Sales
What is a
“Relational Database?”
The term “relational database” comes from
the mathematical definition of a relation, or
set. All objects in a relation must have the
same properties or characteristics
The point? Tables group similar data or
objects, with use one table per set of objects
Student
Course
Professor
Exercise
Discuss some of the entities that would exist
in databases for:
ID
Name Email
Phone GPA
Students
Exercise
Discuss some of the attributes that would
describe the following entities:
Name ID Email
Teaches
Date
Enrolls
In
Grade
Course
Exercise
Devise the relationships that connect the
entities in the databases discussed in one of
the previous exercises:
Beth Adams 5251 Electric Avenue Lewistown, PA 17044 (814) 555-0165 Engineering
Maggie Taylor 227 S. Allen Street State College, PA 16803 (814) 555-0771 Marketing
Matt Peterson 212 S. Allen Street State College, PA 16803 (814) 555-1111 Sales
Sample Database Design
1) No multi-part or multi-value fields
Beth Adams 5251 Electric Avenue Lewistown, PA 17044 (814) 555-0165 Engineering
Maggie Taylor 227 S. Allen Street State College, PA 16803 (814) 555-0771 Marketing
Matt Peterson 212 S. Allen Street State College, PA 16803 (814) 555-1111 Sales
Sample Database Design
2) Eliminate redundant information
Beth Adams 5251 Electric Avenue Lewistown, PA 17044 (814) 555-0165 Engineering
Maggie Taylor 227 S. Allen Street State College, PA 16803 (814) 555-0771 Marketing
Matt Peterson 212 S. Allen Street State College, PA 16803 (814) 555-1111 Sales
Sample Database Design
3) Avoid designs that call for the addition of
columns to store new data
Name Office Address Locality Phone Department 1 Department 2
Bob Walker 212 S. Allen Street State College, PA 16801 (814) 555-1111 Sales Marketing
Beth Adams 5251 Electric Avenue Lewistown, PA 17044 (814) 555-0165 Engineering
Maggie Taylor 227 S. Allen Street State College, PA 16803 (814) 555-0771 Marketing
Matt Peterson 212 S. Allen Street State College, PA 16803 (814) 555-1111 Sales
Sample Database Design
4) Avoid anomalies: Update Anomaly
Beth Adams 5251 Electric Avenue Lewistown, PA 17044 (814) 555-0165 Engineering
Maggie Taylor 227 S. Allen Street State College, PA 16803 (814) 555-0771 Marketing
Matt Peterson 212 S. Allen Street State College, PA 16803 (814) 555-1111 Sales
Sample Database Design
Delete Anomaly
Beth Adams 5251 Electric Avenue Lewistown, PA 17044 (814) 555-0165 Engineering
Maggie Taylor 227 S. Allen Street State College, PA 16803 (814) 555-0771 Marketing
Matt Peterson 212 S. Allen Street State College, PA 16803 (814) 555-1111 Sales
Sample Database Design
Insert Anomaly
Beth Adams 5251 Electric Avenue Lewistown, PA 17044 (814) 555-0165 Engineering
Maggie Taylor 227 S. Allen Street State College, PA 16803 (814) 555-0771 Marketing
Matt Peterson 212 S. Allen Street State College, PA 16803 (814) 555-1111 Sales
Manufacturing
Keys
A key is a field or set of fields that can be used to
uniquely identify a particular record in a database
table.
Examples:
Name, Department
SSN
EID Last Name First Name OID EID DID DID Department
1 1
1 Walker Bob 1 1 Sales
2 Adams Beth 2 2 2 2 Engineering
3 Taylor Maggie 3 3 3 3 Marketing
4 Peterson Matt 1 4 1 4 Manufacturing
1 3
[0,n]
[0,n]
tblUsers tblInstitutions
npkUserID SERIAL not null
npkInstitutionID SERIAL not null
nfkAccountID INTEGER not null
vcFullName VARCHAR(255) not null
vcUsername VARCHAR(24) not null
vcSortName VARCHAR(255) not null
vcPassword VARCHAR(32) not null vcCity VARCHAR(255) null
vcNameFirst VARCHAR(24) null
cfkState CHAR(2) not null
vcNameLast VARCHAR(24) null
CREATE TABLE
You can build a table in a SQL-compatible database with the
CREATE TABLE statement
DeptID DeptName
1 Engineering
2 Education
3 Marketing
4 Sales
Diagramming Joins
Cartesian Product
ID Name Dept DeptID DeptName
1 Brian 1 1 Engineering
1 Brian 1 2 Education
1 Brian 1 3 Marketing
1 Brian 1 4 Sales
2 Karen 2 1 Engineering
2 Karen 2 2 Education
2 Karen 2 3 Marketing
2 Karen 2 4 Sales
3 Mary 3 1 Engineering
3 Mary 3 2 Education
3 Mary 3 3 Marketing
3 Mary 3 4 Sales
4 Dave 1 1 Engineering
4 Dave 1 2 Education
4 Dave 1 3 Marketing
4 Dave 1 4 Sales
Inner Joins
Inner Join
SELECT * FROM
tblAccounts A, tblUsers U
WHERE A.npkAccountID = U.nfkAccountID
AND dtCreated > ‘1/1/2003’
SELECT * FROM
tblAccounts A INNER JOIN tblUsers U
ON A.npkAccountID = U.nfkAccountID
WHERE dtCreated > ‘1/1/2003’
SELECT * FROM
tblAccounts A INNER JOIN tblUsers U
ON A.npkAccountID = U.nfkAccountID
WHERE dtCreated > ‘1/1/2003’
Inner Joins - more tables
SELECT * FROM
For example:
SELECT count(*) as RecordCount,
max(nRevenue) as MaxRevenue,
min(nRevenue) as MinRevenue,
avg(nRevenue) as AvgRevenue
FROM tblModuleData_FoodSvc
New tables
tblModuleData_FoodSvc
nfkDemogID INTEGER not null
nMealPlanEnrollment INTEGER not null
nPotentialMeals INTEGER not null
nActualMeals INTEGER not null
nTotalMeals INTEGER not null
fMealPlanSales NUMERIC(15,2) not null
npkDemogID = nfkDemogID fCateringSales NUMERIC(15,2) not null
[0,n] fConferenceSales NUMERIC(15,2) not null
fRevenue NUMERIC(15,2) not null
fFoodBevCosts NUMERIC(15,2) not null
tblDemographics fLaborHours FLOAT not null
npkDemogID SERIAL not null fSalariesWages NUMERIC(15,2) not null
nfkAccountID INTEGER not null fLaborCosts NUMERIC(15,2) not null
nfkAcadYearID INTEGER not null fNonFoodCosts NUMERIC(15,2) not null
nFTEEnrollment INTEGER not null fDirectCosts NUMERIC(15,2) not null
dtUpdated TIMESTAMP null fOtherCosts NUMERIC(15,2) not null
nfkMgmtArrangeID SMALLINT not null
[0,n] nfkMgmtFirmID INTEGER null
npkAccountID = nfkAccountID
tblAccounts tblAccountReps
npkAccountID SERIAL not null npkAccountRepID SERIAL not null
nfkAccountRepID INTEGER not null [0,n] vcNameFirst VARCHAR(24) null
nfkInstitutionID INTEGER npkAccountRepID
not null = nfkAccountRepID
vcNameLast VARCHAR(24) null
dtCreated TIMESTAMP not null
Grouping Records
You may split your recordset into two or more
groups according to the values of one of the
columns in your query:
SELECT nfkAccountID,
count(*) as RecordCount,
max(nRevenue) as MaxRevenue,
min(nRevenue) as MinRevenue,
avg(nRevenue) as AvgRevenue
FROM tblModuleData_FoodSvc M INNER JOIN tblDemographics D
ON M.nfkDemogID = D.npkDemogID
GROUP BY nfkAccountID
Subqueries
Many RDBMSs allow you to nest queries into
a single operation.