Lesson03 the Relational Model
Lesson03 the Relational Model
January 5, 2025 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
January 5, 2025 5
Relational Model
Tuple
Examples of tuples
– order of the elements within the tuple is important
< blue, blue > and < yellow, 1 , Paris >
January 5, 2025 8
Example:
D1 = {blue, yellow, white} and D2 = { 1 , 2}
D1xD2
{ < blue, 1 >,< blue, 2 >,
< yellow, 1 >,< yellow, 2 >,
< white, 1 >,< white, 2 > }
January 5, 2025 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
January 5, 2025 10
Relational Model – Quick Recap
Tables (relations)
Rows (tuples)
Columns (attributes)
Unordered tuples
Atomic attributes
No duplicate tuples
Domains
January 5, 2025 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
January 5, 2025 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
January 5, 2025 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
January 5, 2025 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.
January 5, 2025 15
2 Key Properties of Relations
No Duplicate tuples
In other words, not all fields are same
January 5, 2025 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
January 5, 2025 17
Relational Integrity Rules
- To prevent invalid or illegal values from occurring in the
database we have some rules
January 5, 2025 18
Candidate Keys
A relation might have more than one unique identifiers. We
simply choose one as our primary key.
January 5, 2025 19
Candidate Keys
Composite Key Example
(S#, sname)
(P#,S#)
(pname, sname, address, S#)
January 5, 2025 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.
January 5, 2025 21
Primary Keys
Supplier S
January 5, 2025 22
Primary Keys
The primary key in SP is (S#, P#) Supplier & Parts SP
It is composite, as there is no
S# P# QTY
single choice for P.K due to the
format of the table or data. S1 P1 300
So P.K = (S#, P#) S1 P2 200
S1 P3 400
//composite P.K S1 P4 200
S1 P5 100
CAUTION: S1 P6 100
Do not be mistaken by seeing S2 P1 300
S2 P2 400
the data at a time interval, in
S3 P2 200
the table and assuming your S4 P2 200
primary keys. S4 P4 300
S4 P5 400
January 5, 2025 23
Foreign Keys
January 5, 2025 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
January 5, 2025 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
January 5, 2025 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)