You are on page 1of 39

Chapter Two

The Relational Model


Chapter 2 - Objectives
Terminology of relational model.
How tables are used to represent data.
Properties of database relations.
How to identify CK, PK, and FKs (key
Constraints).
Meaning of entity integrity and referential
integrity.
Purpose and advantages of views.
Building Blocks of Relational Data Model
Database Schemas, Instances and Database States

2 Fundamentals of Database Systems (INSY2031) - c 02/06/2024


hapter 2
Relational Model Terminology
Relation is a table with columns and rows.
Only applies to logical structure of the database,
not the physical structure.
Relationship: is the association between
records
Attribute is a named column of a relation.

Domain is the set of allowable values for one or


more attributes.

3 Fundamentals of Database Systems (INSY2031) - c 02/06/2024


hapter 2
Relational Model Terminology
Tuple is a row of a relation. ( a record )

Degree is the number of attributes in a relation.

Cardinality is the number of tuples in a relation.

Relational Database is a collection of normalized


relations with distinct relation names.

4 Fundamentals of Database Systems (INSY2031) - c 02/06/2024


hapter 2
Instances of Branch and Staff Relations

5 Fundamentals of Database Systems (INSY2031) - c 02/06/2024


hapter 2
Examples of Attribute Domains

6 Fundamentals of Database Systems (INSY2031) - c 02/06/2024


hapter 2
Alternative Terminology for Relational
Model

7 Fundamentals of Database Systems (INSY2031) - c 02/06/2024


hapter 2
Database Relations
Relation schema
Named relation defined by a set of attribute and
domain name pairs.

Relational database schema


Set of relation schemas, each with a distinct name.

8 Fundamentals of Database Systems (INSY2031) - c 02/06/2024


hapter 2
Relational Databases
Relational Database: a collection of normalized relations
with distinct relation names.
Relation Schema: a uniquely named relation defined by a
set of attribute-domain name pair
Let A1, A2...........An be attributes with domain D 1, D2
………,Dn.
Then the sets {A1:D1, A2:D2… An:Dn} is a Relation Schema.
A relation R, defined by a relation schema S, is a set of
mappings from attribute names to their corresponding
domains. Thus a relation is a set of n- tuples of the form
(A1:d1, A2:d2 ,…, An:dn) where d1 є D1, d2 є D2,…….. dn є Dn,

9 Fundamentals of Database Systems (INSY2031) - c 02/06/2024


hapter 2
Relational Databases
Relational Database schema: a set of relation schema
each with distinct names.
Suppose R1, R2,……, Rn is the set of relation schema
in a relational database then the relational database
schema (R) can be stated as
R={ R1 , R2 ,……., Rn}

10 Fundamentals of Database Systems (INSY2031) - c 02/06/2024


hapter 2
Relational Schema –Convention
The common convention for representing a relation
schema is to give the name of the relation followed by
the attribute names in parentheses. Normally, the
primary key is underlined.
The conceptual model, or conceptual schema, is the set
of all such schemas for the database.

11 Fundamentals of Database Systems (INSY2031) -


chapter 2 02/06/2024
Relational DB Schema- an Example
Branch (branchNo, street, city, postcode)
Staff (staffNo, fName, lName, position, sex, DOB, salary,
branchNo)
PropertyForRent (propertyNo, street, city, postcode,
type, rooms, rent, ownerNo, staffNo,branchNo)
Client (clientNo, fName, lName, telNo, prefType,
maxRent)
PrivateOwner (ownerNo, fName, lName, address, telNo)
Viewing (clientNo, propertyNo, viewDate, comment)
Registration (clientNo, branchNo, staffNo, dateJoined)

12 Fundamentals of Database Systems (INSY2031) -


chapter 2 02/06/2024
Properties of Relations
Relation name is distinct from all other relation
names in relational schema.

Each cell of relation contains exactly one atomic


(single) value.

Each attribute has a distinct name.

Values of an attribute are all from the same domain.

13 Fundamentals of Database Systems (INSY2031) - c 02/06/2024


hapter 2
Properties of Relations
Each tuple is distinct; there are no duplicate tuples.

Order of attributes has no significance.

Order of tuples has no significance, theoretically.


However, in practice, the order may affect the efficiency of
accessing tuples.

14 Fundamentals of Database Systems (INSY2031) - c 02/06/2024


hapter 2
Relational Keys
Super key
An attribute, or set of attributes, that uniquely identifies
a tuple within a relation.

Candidate Key
Super key (K) such that no proper subset is a super key
within the relation.
Must ensure two properties to be a candidate key
 In each tuple of R, values of K uniquely identify that tuple
(uniqueness).
 No proper subset of K has the uniqueness property (irreducibility).
Composite Candidate key- CK. that has more than one
attribute
15 Fundamentals of Database Systems (INSY2031) - c 02/06/2024
hapter 2
Relational Keys
Primary Key
Candidate key selected to identify tuples uniquely
within relation.
Alternate Keys
Candidate keys that are not selected to be primary
key.
Foreign Key
Attribute, or set of attributes, within one relation
that matches candidate key of some (possibly same)
relation.

16 Fundamentals of Database Systems (INSY2031) - c 02/06/2024


hapter 2
Relational Keys- example
Consider Employee and Department
Entities

EID SSNO Fname Lname DOB Salary DID

DID DeptName Location ChairPersonID

17 Fundamentals of Database Systems (INSY2031) - c 02/06/2024


hapter 2
Integrity Constraints
Domain Integrity:
Defines the Constraints on the type, length, format or
range of values a field( an attribute) can have
Entity Integrity
In a base relation, no attribute of a primary key can be
null.
And No Primary key value should be duplicate
Referential Integrity
If foreign key exists in a relation, either foreign key value
must match a candidate key value of some tuple in its
home relation or foreign key value must be wholly null.

18 Fundamentals of Database Systems (INSY2031) - c 02/06/2024


hapter 2
Integrity Constraints
General Constraints (Enterprise Constraints)
Additional rules specified by users or database
administrators that define or constrain some aspect
of the enterprise.
Example: No Clerk should manage more than 100
Properties at a time.
Nulls (Nullity)
Null Represents a value for an attribute that is
currently unknown or is not applicable for this tuple.

19 Fundamentals of Database Systems (INSY2031) - c 02/06/2024


hapter 2
Relations – Two Types
Base Relation
Named relation corresponding to an entity in
conceptual schema, whose tuples are physically
stored in database.

View
Dynamic result of one or more relational operations
operating on base relations to produce another
relation.

20 Fundamentals of Database Systems (INSY2031) - c 02/06/2024


hapter 2
Views
A virtual relation that does not necessarily actually
exist in the database but is produced upon request,
using some DML( eg SQL).

Contents of a view are defined as a query on one or


more base relations.

Views are dynamic, meaning that changes made to


base relations that affect view attributes are
immediately reflected in the view.

21 Fundamentals of Database Systems (INSY2031) - c 02/06/2024


hapter 2
Purpose of Views
Provides powerful and flexible security mechanism
by hiding parts of database from certain users.

Permits users to access data in a customized way,


so that same data can be seen by different users in
different ways, at same time.

Can simplify complex operations on base relations.

22 Fundamentals of Database Systems (INSY2031) - c 02/06/2024


hapter 2
Updating Views
All updates to a base relation should be
immediately reflected in all views that reference
that base relation.

If view is updated, underlying base relation should


reflect change.

23 Fundamentals of Database Systems (INSY2031) - c 02/06/2024


hapter 2
Updating Views
There are restrictions on types of modifications
that can be made through views:
Updates are allowed if query involves a single
base relation and contains a candidate key of
base relation.
Updates are not allowed involving multiple base
relations.
Updates are not allowed involving aggregation or
grouping operations.

24 Fundamentals of Database Systems (INSY2031) - c 02/06/2024


hapter 2
Building blocks of the relational data
model
Entities: real world physical or logical object
Attributes: properties used to describe each Entity or
real world object-pertinent to the business applications.
Relationship: the association between Entities
Constraints: rules that should be obeyed while
manipulating the data.

25 Fundamentals of Database Systems (INSY2031) - c 02/06/2024


hapter 2
Entities
ENTITIES (persons, places, events, things etc.)
which the organization has to deal with. Relations
can also describe relationships
AKA-Entity Types
Existence Dependency: the dependence of an entity
on the existence of one or more entities.
Weak entity: an entity that can not exist without the
entity with which it has a relationship
Strong Entity: Can exist without any relationship
instance
Entity Instance: specific Occurrence of an entity type

26 Fundamentals of Database Systems (INSY2031) - c 02/06/2024


hapter 2
Attributes
Piece of information which characterize and describe
entities.
Attributes are pieces of information ABOUT entities.
The analysis must of course identify those which are
actually relevant to the proposed application.
Attributes will give rise to recorded items of data in
the database
Attribute name (be explanatory words or phrases)
The domain from which attribute values are taken (A
DOMAIN is a set of values from which attribute
values may be taken.)
27 Fundamentals of Database Systems (INSY2031) - c 02/06/2024
hapter 2
Attributes Cont’d…
entity identifier (attributes which just describe an
entity and those which help to identify it uniquely)
permanent or time-varying (which attributes may
change their values over time)
required or optional

28 Fundamentals of Database Systems (INSY2031) - c 02/06/2024


hapter 2
Types of Attributes
 Simple (atomic) Vs Composite attributes
 Simple : contains a single value (not divided into sub parts)
E.g. Age, gender
 Composite: Divided into sub parts (composed of other attributes)
E.g. Name, address
 Single-valued Vs multi-valued attributes
 Single-valued : have only single value(the value may change but
has only one value at one time)
E.g. Name, Sex, Id. No. color_of_eyes
 Multi-Valued: have more than one value
E.g. Address, dependent-name
Person may have several college degrees

29 Fundamentals of Database Systems (INSY2031) - c 02/06/2024


hapter 2
Types of Attributes
Stored vs. Derived Attribute
Stored : not possible to derive or compute
 E.g. Name, Address
Derived: The value may be derived (computed) from the
values of other attributes.
 E.g. Age (current year – year of birth)

Null Values
NULL applies to attributes which are not applicable or
which do not have values or not available now.
Value of a key attribute can not be null.
Default value - assumed value if no explicit value from
the user
30 Fundamentals of Database Systems (INSY2031) - c 02/06/2024
hapter 2
Entity versus Attributes
When designing the conceptual specification of the
database, one should pay attention to the
distinction between an Entity and an Attribute.
Should address be an attribute of Employees or an
entity (connected to Employees by a relationship)?
 If we have several addresses per employee, address must be an
entity (attributes cannot be set-valued/multi valued)
If the structure (city, Woreda, Kebele, etc) is
important, e.g. want to retrieve employees in a
given city, address must be modeled as an entity
(attribute values are atomic)
31 Fundamentals of Database Systems (INSY2031) - c 02/06/2024
hapter 2
Relationships
In any business processing one object may be
associated with another object due to some event.
Such kind of association is what we call a
RELATIONSHIP between entity objects.
Related entities require setting of LINKS from one
part of the database to another.
A relationship should be named by a word (verb)or
verb phrase which explains its function
Role names are different from the names of entities
forming the relationship: one entity may take on many
roles, the same role may be played by different entities
32 Fundamentals of Database Systems (INSY2031) - c 02/06/2024
hapter 2
Relationships Cont’d…
Relationship Type (Relationship)
Relationship between entity types

Relationship Occurrence/Instance:
Relationship between entity occurrences/Instances

For each RELATIONSHIP, one can talk about the


Number of Entities and the Number of Tuples
participating in the association. These two concepts are
called DEGREE and CARDINALITY of a relationship
respectively.
33 Fundamentals of Database Systems (INSY2031) - c 02/06/2024
hapter 2
Degree of a Relationship
The number of entities participating in a relationship Type
is called the DEGREE of the relationship.
Among the Degrees of relationship, the following are the
basic:
UNARY/RECURSIVE RELATIONSHIP: Tuples/records of a
Single entity are related with each other.
BINARY RELATIONSHIPS: Tuples/records of two entities are
associated in a relationship
TERNARY RELATIONSHIP: Tuples/records of three different
entities are associated
And a generalized one:
 N-ARY RELATIONSHIP: Tuples from arbitrary number of entity sets are
participating in a relationship.

34 Fundamentals of Database Systems (INSY2031) - c 02/06/2024


hapter 2
Cardinality of a Relationship
 Cardinality: maximum number of possible relationship occurrences
for an entity participating in a given relationship type.
 The major cardinalities of a relationship are:
 ONE-TO-ONE: one tuple is associated with only one other tuple.
 E.g. Building – Location as a single building will be located in a single location and as a
single location will only accommodate a single Building.
 ONE-TO-MANY, one tuple can be associated with many other tuples, but not
the reverse.
 E.g. Department-Student as one department can have multiple students.
 MANY-TO-ONE, many tuples are associated with one tuple but not the reverse.
 E.g. Employee – Department: as many employees belong to a single department.
 MANY-TO-MANY: one tuple is associated with many other tuples and from
the other side, with a different role name one tuple will be associated with many
tuples
 E.g. Student – Courseas a student can take many courses and a single course can be
attended by many students.

35 Fundamentals of Database Systems (INSY2031) - c 02/06/2024


hapter 2
Constraints
Constraints -rules that should be obeyed while
manipulating the data.
Different Types of Constraints exist
Key (PK and FK, UK)Constraints
Domain Integrity Constraints ( type, Range, Format,
length of values)
General Constraints (Different Operating/Business
Rules)
 usually implemented by database objects such as
Triggers/Assertion in DBMSs

36 Fundamentals of Database Systems (INSY2031) - c 02/06/2024


hapter 2
Schemas, Instances and Database State
Database Schema- is the structural Definition of a
Database
AKA-Database Intension
Database Instance- is the collection of data in the
database at a particular point of time (snap-shot).
AKA – Database Extension, Database State
 Valid state: the state that satisfies the structure and
business constraints specified in the schema and is
enforced by DBMS
Database sate changes every time we issue a DML
while database Schema changes rarely- maintenance
time ( via DDL)
37 Fundamentals of Database Systems (INSY2031) - c 02/06/2024
hapter 2
Database Schema at three different
Levels(ANSI-SPARC)
External Schema
Many External Schemas per database
Conceptual Schema
One and Only One conceptual schema per
Database
Internal Schema
One and Only One Internal Schema per
Database

38 Fundamentals of Database Systems (INSY2031) - c 02/06/2024


hapter 2
Exercise
Consider the following relational schema
Hotel (hotelNo, hotelName, city)
Room (roomNo, hotelNo, type, price)
Booking (hotelNo, guestNo, dateFrom, dateTo,
roomNo)
Guest (guestNo, guestName, guestAddress)
Answer the following questions based on the Schema
above
Identify the Primary key/Foreign keys in each table
Produce some sample tables for these relations that
observe the relational integrity rules.
39 Fundamentals of Database Systems (INSY2031) - c 02/06/2024
hapter 2

You might also like