CS 623 – Database Management Systems
Session 4 Agenda
Chapter 3 - Individual Laboratory Exercises Questions
Chapter 3 - Team Assignments Questions
Oracle Installation Questions
Chapter 4: The Relational Model
Chapter 4 - Individual Laboratory Exercises Questions
Chapter 4 - Team Assignment Review
Team time
CS 623 – Database Management Systems
Reminder on Team Participation and Participation Points
I have received some complaints regarding lack of participation from
some team members.
Team members are expected to contribute equally on team
assignments.
I cannot award participation points for any weeks where team
members have not participated.
In order to receive participation points for that week you would need
to contact your team and coordinate with your team to make up the
time on another week. The team would then need to confirm your
participation to receive those participation points.
CS 623 – Database Management Systems
Reminder on Late Assignments
A fair amount of students did not submit their Chapter 3 laboratory
exercise assignment.
Assignments are due on the dates specified in the syllabus.
Please ensure that you submit individual and team assignments on
time so weekly assignments do not conflict with each other and you
have enough time to complete each assignment.
As indicated in the syllabus, late assignments will be penalized with a
five (5) point deduction for each day late unless arrangements are
made prior to the due date.
Chapter 4: The Relational Model
Chapter 4: The Relational Model
Relational Model
The relational model replaced earlier hierarchical and network
database systems.
Earlier models were based on filesystems
Therefore, the databases created using them required that the
application programs contain details about the layout of data in the
database.
As a result, whenever the database was reorganized, the
applications had to be rewritten.
There was no data independence.
Chapter 4: The Relational Model
Advantages of Relational Model
The relational model removed such details from the application
providing a logical view and creating data independence.
Based on mathematical notion of relation
Can use power of mathematical abstraction
Can develop body of results using theorem and proof method of
mathematics
Can use expressive, exact mathematical notation
Theory provides tools for improving design
Basic structure is simple, easy to understand
Data operations are easy to express, using a few powerful
commands
Operations do not require user to know storage structures used
Chapter 4: The Relational Model
Relational Data Structures
The relational model is based on the concept of a relation, which is
physically represented as a table or two-dimension array. In this
model:
Tables are related to one another
Table hold information about the objects to be represented in the
database
Rows (tuples) correspond to individual records
Columns correspond to attributes
A column contains values from one domain
A domain are the atomic (acceptable) values that a column is
allowed to contain.
Chapter 4: The Relational Model
Properties of a Table
Each cell contains at most one value
Each column has a distinct name which is the name of the attribute it
represents
Values in a column all come from the same domain
Each tuple (row) is distinct – no duplicate tuples
Order of tuples is immaterial
Chapter 4: The Relational Model
Example of a Relational Model
Student table tells facts about students
Faculty table shows facts about faculty
Class table shows facts about classes, including what faculty
member teaches each class
Enroll table relates students to classes
Chapter 4: The Relational Model
University Sample Database
Student Table Faculty Table
Enroll Table
Class Table
Chapter 4: The Relational Model
Mathematical Relations
To understand the strict meaning of the term relation, we need to
review some notions from mathematics
For two sets D1 and D2, we could form the Cartesian product (cross-
join): D1 x D2 , which is the set of all ordered pairs where the first
element is from D1 and the second from D2
A relation is any subset of the Cartesian product
Could form Cartesian product of 3 sets; relation is any subset of the
ordered triples so formed
Could extend to n sets, using n-tuples
Chapter 4: The Relational Model
Database Relations
When applying these concepts to databases:
A relation schema, R, is a set of attributes {A1, A2,…,An} with their
corresponding domains {D1, D2,…Dn}
A relation r on relation schema R is a set of mappings from the attribute
names to their corresponding domains
r is a set of n-tuples (A1:d1, A2:d2, …, An:dn) such that
d1ϵ D1, d2 ϵ D2 , …, dn ϵ Dn
Each element in one of these n-tuples consists of an attribute and a value of that
attribute.
In a table to represent the relation, we list the attribute names as column
headings, and let the (d1, d2, …dn) become the n-tuples, the rows of the table
Chapter 4: The Relational Model
Properties of Relations
Most of the characteristics specified for tables result from the properties of
mathematical relations.
Degree of the relation is the number of attributes (columns)
2 attributes: binary; 3 attributes: ternary; n attributes: n-ary
Ex: the degree of student is 5 because it has 5 columns.
And each row of the table is a 5-tuple - containing 5 values
A property of the intension – does not change unless database design changes
Cardinality of the relation: the number of tuples (rows)
Changes as tuples are added or deleted
A property of the extension – changes often
The relational model allows us to define Keys and integrity constraints to
enforce and ensure data integrity.
Chapter 4: The Relational Model
Relation Keys
Relations never have duplicate tuples, therefore, you can always tell
tuples apart. This implies there is always an attribute or set of
attributes that make the tuples distinct.
Superkey is a set of attributes that uniquely identifies tuples
Candidate key is the minimum set of attributes which can uniquely
identify a tuple
Primary key is the candidate key chosen for unique identification of
tuples
Foreign key is an attribute or combination of attributes that is the
primary key of some relation (called its home relation)
Chapter 4: The Relational Model
Integrity Constraints
It is important to preserve integrity, which means correctness and internal consistency
of the data in the database
Integrity constraints - rules or restrictions that apply to all instances of the
database
Enforcing integrity constraints ensures that any data entered creates a legal instance
of the database.
Types of constraints:
Domain constraint: limits set of values for attributes
Entity integrity: no attribute of a primary key can have a null value
Referential integrity: each foreign key value must match the primary key value of
some tuple in its home relation or be completely null
General constraints or business rules: enforced by database whenever changes
are made to the data
Chapter 4: The Relational Model
Representing Relational Database Schemas
Relational database can have any number of relation schemas
For each schema, list name of relation followed by list of attributes in
parentheses
Underline primary key in each relation schema
Indicate foreign keys in italics and use arrows to point to the primary
keys that they refer to.
Example: University database schema
Chapter 4: The Relational Model
Types of Relational Data Manipulation Languages
There are a variety of languages used by relational database management systems.
Types include:
Procedural or proscriptive – user tells system how to manipulate data - e.g.
relational algebra
Non-procedural or declarative – user tells system what data is needed, not how
to get it. Ex: relational calculus, SQL
Other types:
Graphical: user provides illustration of what data is needed Ex: Query By
Example (QBE)
Fourth-generation (4GL): uses user-friendly environment to generate custom
applications
Natural language: 5GL accepts restricted version of English or other natural
language
Chapter 4: The Relational Model
Relational Algebra
Relational Algebra is a theoretical language with operators that apply to
one or two relations to produce another relation
Therefore, both the operands and results are tables
Can assign name to resulting table (rename)
SELECT, PROJECT, JOIN allow many data retrieval operations
Chapter 4: The Relational Model
SELECT Operation
The SELECT command is applied to a single table, returns rows that
meet a specified predicate, copies them to new table
Returns a horizontal subset of original table
SELECT tableName WHERE condition [GIVING newTableName]
Symbolically, [newTableName = ] predicate (table-name)
Predicate is called a theta-condition, as in (tablename)
Result table is horizontal subset of operand
Predicate can have operators
<, <=, , =, =, <>, (AND), (OR), (NOT)
Ex: select student where studId = ‘S1013’ GIVING Result
stuId lastName firstName major credits
S1013 McCarthy Owen Math 0
Chapter 4: The Relational Model
PROJECT Operation
The PROJECT command also operates on single table
Returns unique values in a column or combination of columns
PROJECT tableName OVER (colName,...,colName)
[GIVING newTableName]
Symbolically
[newTableName =] colName,...,colName (tableName)
Can compose SELECT and PROJECT, using result of first as
argument for second
Ex: PROJECT class OVER (facid, room) GIVING Result
facId room
F101 H221
F105 M110
F115 H221
F110 H225
Chapter 4: The Relational Model
Product, A x B
Binary operation – applies to two tables
Cartesian product; cross-product of A and B; written A x B
Concatenates all rows of A with all rows of B
Columns are the columns of A followed by the columns of B
Degree of result is deg of A + deg of B
Cardinality of result is (card of A) * (card of B)
Can be formed by nested loops algorithm\
Example Student x Enroll
stuId lastName firstName major credits classNumber grade
S1001 Smith Tom History 90 HST205A C
S1001 Smith Tom History 90 ART103A A
S1002 Chin Ann Math 36 MTH103C B
S1002 Chin Ann Math 36 CSC201A F
S1002 Chin Ann Math 36 ART103A D
S1010 Burns Edward Art 63 MTH103C
S1010 Burns Edward Art 63 ART103A
S1020 Rivera Jane CSC 15 MTH101B A
S1020 Rivera Jane CSC 15 CSC201A B
Chapter 4: The Relational Model
Theta Join and Equi Join
Theta join allows you to merge two tables based on the condition
represented by a theta (Θ)
Ex: selecting the tuples from student and enroll where credits is greater
than 50.
Equi Join can be formed when tables have common columns. We
then compare each tuple and only retrieve where the values of those
columns are equal.
Ex: selecting the tuples from student and enroll where they have
matching stuid
Chapter 4: The Relational Model
Natural Join
Same as Equijoin, but we drop repeated column(s) from result.
Ex: for the join between student and enroll, we only show stuId once
symbolized by |x| as in
[newTableName = ] Student |x| Enroll
or
Student JOIN Enroll [GIVING newTableName]
Chapter 4: The Relational Model
Left Semi Join
Left Semi Join A |x B
take the natural join of A and B and then project the result onto the
attributes of A
result is just those tuples of A that participate in the natural join
Ex: the left semijoin of Student and Enroll is written:
Student LEFT SEMIJOIN Enroll
The following just shows the rows of Students that have the same Stuid
matches in the Enroll table:
Chapter 4: The Relational Model
Right Semi Join
Right Semi Join A x| B
take the natural join of A and B and then project the result onto the
attributes of B
Result is just those tuples of B that participate in the natural join
Ex: the right semijoin of Student and Enroll is written:
Student RIGHT SEMIJOIN Enroll.
This is the projection onto the Enroll table of the natural join, therefore,
those tuples of Enroll that participate in the join (namely, all of them
since this is a child table).
Chapter 4: The Relational Model
Outer Join
Left Outer Join of A and B
Form natural join, but add rows for all the tuples of A with no
matches in B
Fill in the B attributes for those unmatched tuples with null values
Right Outer Join of A and B
Add unmatched B tuples to natural join, filling in null values for the A
attributes
Full Outer Join of A and B
Add unmatched tuples for both A and B, filling in null values for
unmatched tuples on both sides
Chapter 4: The Relational Model
SET Operations
For a set operation to be possible, tables must be union compatible – meaning
have same schema
A UNION B: set of tuples in either or both of A and B,
written A B
Ex: It shows the results of both tables removing any duplicates
A INTERSECTION B: set of tuples in both A and B simultaneously, written A
∩B
Ex: It only shows data from both tables where the stuid exists in both.
A MINUS B: set of tuples in A but not in B,
written A - B
Ex: is used to return all rows in the first SELECT table that are not returned by
the second table.
Chapter 4: The Relational Model
SET Operation Example
Faculty Table 1 UNION
facId name department rank
facId name Department rank
F101 Adams Art Professor
F101 Adams Art Professor
F105 Tanaka CSC Instructor
F105 Tanaka CSC Instructor F110 Byrne Math Assistant
F221 Smith CSC Professor F115 Smith History Associate
F221 Smith CSC Professor
Faculty Table 2 INTERSECTION
facId name department rank facId name Department rank
F101 Adams Art Professor F101 Adams Art Professor
F110 Byrne Math Assistant F221 Smith CSC Professor
F115 Smith History Associate
F221 Smith CSC Professor
MINUS
facId name department rank
F105 Tanaka CSC Instructor
Chapter 4: The Relational Model
Views
An external view is the structure of the database as it appears to a user
Views act as a window into a base table (subset)
Can contain data from more than one table
Can contain calculated data
Can hide portions of database from users
Can rename attributes for users
Chapter 4: The Relational Model
Mapping ER Diagrams to Relational Schema
An E-R diagram can be converted to a relational model fairly easily.
Each strong entity set becomes a table (rectangle)
Non-composite, single-valued attributes become attributes of table
(ovals)
Composite attributes: either make the composite a single attribute or
use individual attributes for components, ignoring the composite –
address or break into street, city, state, zip
Multi-valued attributes: remove them to a new table along with the
primary key of the original table; also keep key in original table
Weak entity sets become tables-add primary key of owner entity
Chapter 4: The Relational Model
Mapping ER Diagrams to Relational Schema (Con’t)
Binary Relationships:
For 1:M - place primary key of A (the 1 side) in table B (the many
side) where it becomes the foreign key
1:1- use either key as foreign key in the other table
M:M-create a relationship table with primary keys of related
entities, along with an y relationship attributes
Ternary or higher degree relationships: construct relationship table of
keys, along with any relationship attributes
Recursive relationship depends on the cardinality:
use foreign key if 1:M
use relationship table if M:M
Chapter 4: The Relational Model
Relational Schema - University Example
Chapter 4 Questions?
Chapter 4: The Relational Model
Chapter 4: Individual Laboratory Exercises Assignment
Step 4.1 - Examine the following ER diagram for CustomerOrder
example, which also appears in Figure 4.9 (located in the textbook)
Step 4.2 - Write a relational schema for this E-R diagram.
Underline the primary keys, show the foreign keys in italics, and draw
arrows from the foreign keys to the primary keys they refer to.
Use the guidelines presented in Section 4.7 and Figure 4.7 of the
textbook.
Steps 4.3 – 4.6 – Ignore.
Chapter 4: The Relational Model
Chapter 4: Individual Laboratory Exercises Assignment (Con’t)
Chapter 4: The Relational Model
Chapter 4: Team Assignment
Step 4.1 - Map the E-R Diagram developed at the end of Chapter 3 to a
relational model, using the guidelines presented in Section 4.7 and
Figure 4.7 of the textbook.
For this assignment, submit your E-R Diagram (from chapter 3) and
your relational model (from chapter 4)