Chapter (4) Relational
Chapter (4) Relational
Ahmed M. Zeki
ITIS 216
ITBIS 385
Objectives
Terminology of relational model
How tables are used to represent data
Connection between mathematical relations
and relations in the relational model
Properties of database relations
How to identify CK, PK, and FKs
Meaning of entity integrity and referential
integrity
Purpose and advantages of views
2 / 36
Relational Model Terminology
RDBMS
3 / 36
Relational Model Terminology
First generation of DBMS (the network and
hierarchical DBMS)
Objective of RDBMS:
◦ To allow a high degree of data independence
◦ To provide substantial grounds of dealing with data
semantic, consistency, and redundancy problems
4 / 36
Relational Model Terminology
Some extensions to the relational model:
◦ Capture more closely the meaning of data
◦ Support OO concepts
◦ Support deductive capabilities
6 / 36
Relational Model Terminology
Attributes can appear in any order
7 / 36
Relational Model Terminology
The structure of the table together with a
specification of the domains and any other
restrictions on possible values is called its
intension, which is usually fixed unless the
meaning of a relation is changed to include
additional attributes.
8 / 36
Relational Model Terminology
Degree is the number of attributes in a
relation.
◦ Unary
◦ Binary
◦ Ternary
◦ n-ary
D1 ´ D2 = {(2, 1), (2, 3), (2, 5), (4, 1), (4, 3), (4, 5)}
10 / 36
Mathematical Definition of Relation
Any subset of Cartesian product is a relation; e.g.
R = {(2, 1), (4, 1)}
11 / 36
Mathematical Definition of Relation
Consider three sets D1, D2, D3 with Cartesian Product
D1 ´ D2 ´ D3; e.g.
12 / 36
Mathematical Definition of Relation
Cartesian product of n sets (D1, D2, . . ., Dn) is:
13 / 36
Relation Schema
Relational Database is a collection of
normalized (appropriate structure) relations
with distinct relation names.
14 / 36
Relation Schema
◦ A relation R defined by a relation schema S is a set
of mapping from the attribute names to their
corresponding domains.
16 / 36
Properties of Relations
Relation name is distinct from all other relation
names in relational schema
Each cell of relation contains exactly one value
Each attribute has a distinct name
Values of an attribute are all from same domain
Each tuple is distinct; there are no duplicate
tuples
Order of attributes has no significance
Order of tuples has no significance,
theoretically
17 / 36
Properties of Relations
Relations do not contain repeating groups.
18 / 36
Relational Keys
Superkey: An attribute, or set of attributes, that
uniquely identifies a tuple within a relation.
Candidate Key: Superkey (K) such that no proper
subset is a superkey within the relation.
◦ In each tuple of R, values of K uniquely identify
that tuple (uniqueness).
◦ No proper subset of K has the uniqueness
property (irreducibility).
◦ There may be several keys for a relation. When a
key consists of more than one attribute, we call it
a composite key.
19 / 36
Relational Keys
Primary Key: Candidate key selected to identify
tuples uniquely within the relation.
◦ A relation always has a primary key.
20 / 36
Relational Keys Eg.
◦ We say that the
attribute
branchNo in the
Staff relation
targets the
primary key
attribute
branchNo in the
home relation
Brach. These
common
attributes play an
important role in
performing data
manipulation. 21 / 36
Representing Relational Schema
Give the name of the relation followed by the
attribute names in parentheses. The primary
key is underlined
Branch
22 / 36
Representing Relational Schema
Client
◦ (clientNo, fName, Iname, telNo, prefType, maxRent)
PrivateOwner
◦ (ownerNo, fName, Iname, address, comment)
Viewing
◦ (clientNo, propertyNo, viewData, comment)
Registration
◦ (clientNo, branchNo, staffNo, dataJoined)
23 / 36
Integrity Constraints
A data model has two other parts:
◦ A manipulative part: defining the types of
operation that are allowed on the data
◦ A set of integrity constraints: ensure that the data
is accurate
Since every attribute has an associated
domain, there are constraints (called domain
constraints) that form restrictions on the set
of values allowed for the attributes of
relations.
24 / 36
Integrity Constraints
In addition, there are two important integrity
rules:
◦ Constraints or restrictions that apply to all
instances of the DB.
The two principal rules for the relational
model are known as:
◦ entity integrity
◦ referential integrity
Other types of integrity constraint are:
◦ multiplicity
◦ general constraints
25 / 36
Integrity Constraints
Null
◦ Represents value for an attribute that is
currently unknown or not applicable for tuple.
26 / 36
Integrity Constraints
Entity Integrity
◦ In a base relation, no attribute of a primary key can
be null.
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.
27 / 36
Integrity Constraints
General Constraints
◦ Additional rules specified by users or database
administrators that define or constrain some aspect
of the enterprise.
◦ Eg. Upper limit is 20
28 / 36
Views
View is a virtual relation that does not
necessarily exist in the DB but is produced
upon request, at time of request.
29 / 36
Views
Views are dynamic, meaning that changes
made to base relations that affect view
attributes are immediately reflected in the
view.
31 / 36
Views
Examples:
◦ Some members of staff should see Staff tuples
without the salary attributes.
◦ Attributes may be renamed, or the order of
attributes changed.
◦ Some members of staff should see only property
records for those properties that they mange.
32 / 36
Views
Views provides logical data independence
33 / 36
Views
All updates to a base relation should be
immediately reflected in all views that
reference that base relation.
34 / 36
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.
35 / 36
Views
Classes of views are defined as:
◦ theoretically not updateable;
◦ theoretically updateable;
◦ partially updateable.
36 / 36