Lesson03 The Relational Model
Lesson03 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
June 2, 2024 5
Relational Model
Tuple
Examples of tuples
– order of the elements within the tuple is important
< blue, blue > and < yellow, 1 , Paris >
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
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
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
June 2, 2024 14
Relations
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
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
June 2, 2024 18
Candidate Keys
A relation might have more than one unique identifiers. We
simply choose one as our primary key.
June 2, 2024 19
Candidate Keys
Composite Key Example
(S#, sname)
(P#,S#)
(pname, sname, address, S#)
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
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
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:
{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.
END OF LESSON 03
(these slides were adapted from original slides of M Ali Shahid)