Lesson 03
The Relational Model
June 2, 2024 1
Entities
Entity – a person, place, thing, or event
Entities become our categories of
information
Customers
Products
Orders
We organize our information in these major
entities (categories)
These categories become the tables, that
will store our data
2
Relationships
Relationship – an association between
entities
Relationships define how our data
files will talk to each other
Customers “Place” Orders
Orders “Are For” Products
There are several key types of
relationships that we will talk about
later on
3
More about Entities
What does everyone have in common?
Entities have basic properties too
Properties of entities are called Attributes
Attributes describe our categories
A “Customer” has some basic properties
Name
Phone Number
Address
4
Relational Databases
Relational Databases
represent data as a collection of tables
each row in a table represents a collection of related values
Example
Supplier S
S# SNAME STATUS CITY
S1 Smith 20 London
S2 Jones 10 Paris
S3 Blake 30 Paris
S4 Clark 20 London
S5 Adams 30 Athens
June 2, 2024 5
Relational Model
Why is it called relational?
a table is similar to the concept of a relation in mathematics
June 2, 2024 S M Irteza / Dr Rafi Ullah 6
Relation
Set/Subset
Examples of sets
D1 = {blue; yellow; white} and D2 ={ 1 , 2 }
Tuple
Examples of tuples
– order of the elements within the tuple is important
< blue, blue > and < yellow, 1 , Paris >
June 2, 2024 S M Irteza / Dr Rafi Ullah 7
Relation
Consider D1 = {blue; yellow; white} and D2 = {1; 2}
Cartesian Product
The set of all (binary) tuples such that the first argument
belongs to D1 and the second argument belongs to D2 is
denoted D1xD2.
A relation on D1xD2 is a collection of tuples < x, y > where all x’s
are taken from D1 and all y’s are taken from D2.
In other words, a relation on D1xD2 is a subset of D1xD2.
June 2, 2024 8
Example:
D1 = {blue, yellow, white} and D2 = { 1 , 2}
D1xD2
{ < blue, 1 >,< blue, 2 >,
< yellow, 1 >,< yellow, 2 >,
< white, 1 >,< white, 2 > }
June 2, 2024 9
Relational Model
Association between tables and relations
Rows in a table are associated with the tuples in the relation
Columns in a table are associated with the arguments in the
tuples
The columns are usually called attributes
Number of attributes = degree of the relation
June 2, 2024 10
Relational Model – Quick Recap
Tables (relations)
Rows (tuples)
Columns (attributes)
Unordered tuples
Atomic attributes
No duplicate tuples
Domains
June 2, 2024 11
Relational Data Structure
Supplier S
S# SNAME STATUS CITY
S1 Smith 20 London
S2 Jones 10 Paris
S3 Blake 30 Paris
S4 Clark 20 London
S5 Adams 30 Athens
Table is the Data Structure for relational model
Relation = Table
name of this relation is S.
Tuple = Row
This relation has 5 tuples
Each tuple represents a record of one supplier, so tuples
are seldom called records also.
June 2, 2024 12
Relational Data Structure
Supplier S
S# SNAME STATUS CITY
S1 Smith 20 London
S2 Jones 10 Paris
S3 Blake 30 Paris
S4 Clark 20 London
S5 Adams 30 Athens
Cardinality = # of rows with data in the relation
so for S, cardinality = 5.
Attribute = Columns or fields.
Degree = # of columns or fields of a relation.
The relation S has a degree of 4.
June 2, 2024 13
Relational Data Structure
Supplier S
S# SNAME STATUS CITY
S1 Smith 20 London
S2 Jones 10 Paris
S3 Blake 30 Paris
S4 Clark 20 London
S5 Adams 30 Athens
Domain = A pool of legal values
For example:
For S# we have S followed by a positive number.
For Year we might have a four digit positive number between
1000 and 2003.
Primary Key = A unique identifier, used to identify one
specific record from among all other records.
June 2, 2024 14
Relations
A relation consists of 2 parts
Heading:
Consists of a fixed set of attributes or columns or fields.
Defined through the DDL, and usually remains the
same.
Body
Consists of the tuples or rows or records.
Time varying set, i.e., at different intervals of time
there may be different contents in the body of a
relation.
June 2, 2024 15
2 Key Properties of Relations
No Duplicate tuples
In other words, not all fields are same
All cells have a single value
A relation which follows this rule is said to be in the
first normal form.
June 2, 2024 16
Relational Integrity
The ultimate purpose of a DB is to store
information about the real world.
It is possible that some information submitted to
the DB does not correspond to any possible
configuration of the real world.
negative salaries
impossible dates: 30/02/1999
negative quantities in stock, etc
June 2, 2024 17
Relational Integrity Rules
- To prevent invalid or illegal values from occurring in the
database we have some rules
Specific Integrity Rules
Apply to a specific database, I.e. years may not be
less than zero, and 0 < age < 200 .
General Integrity Rules
Apply to all the relational databases.
Like no two same tuples etc.
June 2, 2024 18
Candidate Keys
A relation might have more than one unique identifiers. We
simply choose one as our primary key.
Attribute K (possibly composite) of relation R is a candidate
key for R , if it satisfies the following two properties.
UNIQUENESS: no 2 tuples of relation R have the
same values of K.
MINIMILITY: If K is composite then no subset of K
has the unique property.
- i.e. if you find any column that is unique, don’t add
any more column to the primary key, because this
column by itself is a suitable primary key
June 2, 2024 19
Candidate Keys
Composite Key Example
(S#, sname)
(P#,S#)
(pname, sname, address, S#)
Atomic Key Example
S#
P#
sname
pname
June 2, 2024 20
Primary Keys and Alternate Keys
A Unique Identifier.
The primary key is chosen from a set of candidate
keys.
The primary key is the candidate key chosen to be the
main key for the relation
The other candidate keys are called alternate keys
Not duplicable
NULLS are not allowed in the primary key field.
June 2, 2024 21
Primary Keys
Supplier S
S# SNAME STATUS CITY
S1 Smith 20 London
S2 Jones 10 Paris
S3 Blake 30 Paris
S4 Clark 20 London
S5 Adams 30 Athens
In the relation/table primary key is “S#”
As sname, status and city are assumed to be
repeatable or can be duplicate.
June 2, 2024 22
Primary Keys
The primary key in SP is (S#, Supplier & Parts SP
P#)
S# P# QTY
It is composite, as there is no
single choice for P.K due to the S1 P1 300
format of the table or data. S1 P2 200
S1 P3 400
So P.K = (S#, P#) S1 P4 200
//composite P.K S1 P5 100
S1 P6 100
CAUTION: S2 P1 300
S2 P2 400
Do not be mistaken by seeing
S3 P2 200
the data at a time interval, in S4 P2 200
the table and assuming your S4 P4 300
primary keys. S4 P5 400
June 2, 2024 23
Foreign Keys
A foreign key is an attribute of one
relation R2 whose values are required to
match those values of the primary key
of some relation R1.
June 2, 2024 24
Foreign Keys
Foreign Keys are used to link data in
two relations. A set of attributes in
the first (referencing) relation is a
foreign key if its value always either:
Matches a candidate key value in the
second (referenced) relation; or
Is wholly NULL.
This is called Referential Integrity
June 2, 2024 25
Foreign Keys: Example
Department: Employee:
DID DName EID EName DID
13 Marketing 15 Yunus Sami 13
14 Accounts 16 Mirza Barki 14
15 Personnel 17 Malik Jahan 13
18 Jan Sher NULL
DID is a Candidate Key for the DID is a Foreign Key in the Employee
Department relation. Each entry relation– each Employee’s DID value is
has a unique value for DID either NULL, or matches an entry in
the Department relation. This links
each employee to (at most) one
Department
June 2, 2024 26
Foreign Keys: Example
Student: Enrolment:
ID First Last First Last Course
S01 Ahmad Khan Ahmad Khan CS101
S02 Athar Khalid Ahmad Khan CS222
S03 Sameer Shahid Sameer Shahid CS101
S04 Sameer Khan Sameer Khan CS222
{First, Last} is a Candidate Key for {First, Last} is a Foreign Key in the
the Student relation – no entries Enrolment relation - each {First, Last}
have the same value for both First pair matches exactly one entry in the
and Last. Student relation.
June 2, 2024 S M Irteza / Dr Rafi Ullah 27
Relational Integrity Rules
To prevent invalid or illegal values from occurring in the
database we have some rules
Specific Integrity Rules
Apply to a specific database, i.e., Years may not be
less than zero, and 0 < Age < 200 .
General Integrity Rules
Apply to all the relational databases.
Like no two same tuples etc.
June 2, 2024 S M Irteza / Dr Rafi Ullah 28
General Integrity Rules
We have basically two general integrity
rules
1. Entity Integrity Rule.
2. Referential Integrity Rule.
June 2, 2024 S M Irteza / Dr Rafi Ullah 29
1: Entity Integrity Rule
“No Component of the P.K of a relation is allowed to
accept NULLs”.
NULL (null value) = unknown or missing value.
Note that a NULL value is not zero, it is not infinity, or
negative, or FI value. It is simply non existent for the
system.
P.K = (S#,NULL) is illegal.
June 2, 2024 S M Irteza / Dr Rafi Ullah 30
2: Referential Integrity Rule
“The DB must not contain any unmatched foreign key values”.
e.g., shipment should not have S6 which is not in the suppliers
table as a P.K
Any state of the DB that doesn't satisfy the integrity rules is
ILLEGAL.
Supplier S Supplier & Parts SP
S# SNAME STATUS CITY S# P# QTY
S1 Smith 20 London S1 P1 300
S2 Jones 10 Paris S1 P2 200
S3 Blake 30 Paris S1 P3 400
S4 Clark 20 London S1 P4 200
S5 Adams 30 Athens S1 P5 100
S1 P6 100
Parts P S2 P1 300
S2 P2 400
P# PNAME COLOR WEIGHT CITY
S3 P2 200
P1 Nut Red 12 London S4 P2 200
P2 Bolt Green 17 Paris S4 P4 300
P3 Screw Blue 17 Rome S4 P5 400
P4 Screw Red 14 London
P5 Cam Blue 12 Paris
P6 Cog Red 19 London
June 2, 2024 S M Irteza / Dr Rafi Ullah 31
How to Avoid Illegal States?
1. Simply reject any operation that would result in an
illegal state, for example:
if we try to store “S5” in table S with an “S5” entry
existing, then the system should reject that.
2. Accept the operation but perform some additional
compensating operations in order to guarantee the
overall result is still in a legal state.
E.g. to delete a supplier for which there exists some
shipments, lets say we try to delete “S3” from
supplier, so violation at shipment table occurs as
shipment had “S3” entry as a F.K.
June 2, 2024 S M Irteza / Dr Rafi Ullah 32
End
END OF LESSON 03
(these slides were adapted from original slides of M Ali Shahid)
June 2, 2024 S M Irteza / Dr Rafi Ullah 33