You are on page 1of 37

RELATIONAL DATA MODEL

LECTURE
THREE
BACKGROUND
• Relational model was introduced by E.F.Codd in 1970
Objectives of the relational model include:
- It allows a high degree of data independence i.e users’
interactions with the database through application
programs must not be affected by modifications to
the internal data representation
- To provide substantial grounds for dealing with the
problems of data semantics, consistency and
redundancy ( of special reference is the concept of
normalised relations which will be discussed later)
MAIN ASPECTS OF A RELATIONAL MODEL
The relational model can be considered as having three parts and these are
covered in sequence below:
1. Structural: defines the core of the data and the relationships involved.
The model structure is described in terms of relations, tuples, attributes,
schemas,keys and domains.
2. Manipulative: defines how the data in the model will be accessed and
manipulated.
This concerns how relations in the model will be manipulated to produce
other relations, which in turn provide the answer to some question posed
by a user of the data. The manipulation is achieved though relational
algebra or relational calculus.
3. Integrity Constraints: defines limits on the model.
The data integrity portion of the relation model defines mechanisms for
ensuring that stored data is valid. The integrity constraints include: entity
integrity, referential integrity and semantic constraints which are used to
enforce rules on a relational database.
STRUCTURAL ASPECT
RELATION
• A relation which is the most basic part of the data
structure model is a table with columns and rows
• It has two parts: relation heading and relation body:
• Relation Heading
A relation heading is a fixed set of (attribute,
domain) pairs
• Relation Body
A relation body is a set of tuples (i.e., rows). A tuple
(row) can be thought of as an instance of a relation
C# Customer Customer City Customer
name Phone
1 Codd Harare 0773113445
ROW 2 Martin Gweru 0778990332
3 Deen Mutare 0771654876
COLUMN
ATTRIBUTE
• It is a named column of a relation aka column
or field
• Attributes can appear in any order and the
relation will still be the same relation and
therefore convey the same meaning
• What do we mean???? Let us go to the next
slide to get an idea….
REGNO NAME ADDRESS PROGRAMME LEVEL

PROGRAMME LEVEL REGNO ADDRESS NAME

These 2 tables are the same what is


different is the order in which the
attributes appear
DOMAIN
• It is defined as a set of atomic values of the same
type
• Domain also refers to the current set of values found
under an attribute name
• A value is the smallest unit of data in the relational
model
• An atomic value is one that non-decomposable i.e.
there can only be one set of value for a given
attribute e.g. a student has only one registration
number(atomic) whilst they can have more than one
contact number(non-atomic)
• An attribute always has a domain associated with it
Domain continued….
Reg # Name
12376 Tawanda
87604 Tanatswa
55321 Tinevimbo

THE DOMAIN FOR THE REG # IN THIS TABLE WILL BE THE SET { 12376;
87604; 55321}
TUPLE
• Is an ordered set of values that describe data
characteristics at one moment in time
• Informal terms used to refer to a tuple include
a row or record
• Tuples are an extension( or instance) of a
relation which changes over time
• A Relational DBMS requires only that the
database be perceived by the user as tables
• However this perception applies only to the
logical structure of the database that is the
external and conceptual levels of the ANSI-
SPARC architecture
• It does not apply to the physical structure of the
database which can be implemented using a
variety of storage structures
PROPERTIES OF A RELATION
1) Each cell of a relation may contain at most 1
value i.e. you can not store more than one
value in the same cell; relations that satisfy
this property are said to be normalised in 1NF
2) Each record within a relation is distinct i.e no
rows have exactly the same values(no
duplicates exist) How is this possible????
3) The name of the relation is unique ie. No 2
relations can have the same name.
PROPERTIES continued.....
4) The name of the attribute is unique only within the
relation
5) Values of an attribute are all from the same domain
6) The order of attributes within a relation has no
significance i.e if we re-order the columns of a
relation, it does not become a different relation
7) The order of the rows within a relation has no
significance
RELATION ELEMENTS
• Relations have three important elements.
Each relation has a name, a cardinality
• and a degree. These elements help us to
further define and describe relations.
• The three elements introduced above are
defined as follows:
ELEMENTS continued….
Name: The first element is that a relation has a name which
identifies it
Cardinality: The second element of a relation it its cardinality.
This refers to the number of tuples in the relation
Degree/ Arity: The third and final element of a relation is its
degree. The degree of a relation refers to the number of
attributes in each relation.
EXAMPLE

For the relation above, the relation heading is: { (ID, integer), (Name, string), (Dept,
string) }. The number of these pairs is known as the relation degree, therefore, the
relation above is a degree 3 relation.

The tuple represented by ID of 1, is actually this set { (ID, 1), (Name, Smith), (Dept,
Sales) }. The number of tuples is known as the relation cardinality. For the relation above,
it has a cardinality of 2.
KEYS
• Keys are used to enforce rules and or constraints on
database data and these constraints are essential for
maintaining data consistency and correctness
• There are 3 types of keys:
• Candidate key; primary key and foreign key
Candidate keys
• Candidate Keys- is a unique identifier for the
tuples of a relation hence it is sometimes
called a unique key
• Every relation has at least one candidate key
• A relation CAR with the following attributes
(State, Reg#, SerialNo, Make, Model, Year)
• The attributes Reg# and SerialNo are
candidate keys
PRIMARY KEYS
• Is unique identifier of the relation tuples
• It is a candidate key chosen to represent the
relation and every relation has one primary
key
• If a relation has several candidate keys, one is
chosen arbitrarily to be the primary key. The
primary key attributes are underlined
STUDENT_DETAILS RELATION
STUDENT_REG NATIONAL_ID# NAME ADDRESS
#
123456Z 45-123456-D12 TAWANDA SENGA
789101A 56-135790-F34 NETSAI MKOBA

THERE ARE 2 CANDIDATE KEYS STUDENT REG# AND


NATIONAL ID#. OF THESE THE STUDENT REG# IS THEN
CHOSEN TO BE THE PRIMARY KEY AND UNDERLINED.
FOREIGN KEY
• Is an attribute or set of attributes in one relation that
matches the candidate key of some ( possibly the same)
relation
• For example we have 2 tables:
Student_details ( student_reg#, national_id#, name and
address)
Registration_details( student_reg#, modulecode,modulename)
The student_reg# attribute is a FK. In the table student details
it appears as a PK and in registration_details it appears as a
CK
At the data definition level, a foreign key and the corresponding primary key
should be defined on the same underlying domain
e.g.
Create table Student_details
(
Student_reg# int UNIQUE NOT NULL,
National_id# varchar(11) UNIQUE,
Name varchar(15),
Address varchar (10),
Primary Key (student_reg#)
)

Create table Registration_details


(
Module_code varchar(6) UNIQUE NOT NULL,
Student_reg# int UNIQUE,
Module_name varchar(15)
Primary Key ( Module_code),
Foreign Key (Student_reg#) REFERENCES Student_details (Student_reg#)
)

Note the student_reg# attribute is declared as an integer with unique keyword in both tables.
But in table registration_details it does not have the NOT NULL keyword because the values
have to be the same and leaving it blank gives us provisions to update the table later with the
correct values
INTEGRITY ASPECT
• Data integrity means that all data within the
database adheres to the guidelines or
structure of the database e.g a record in the
database should have a unique identifier.
• It also refers to data being accurate and
consistent
• Certain rules must be built into the database
e.g if you have a date field and you do not
want users to enter random text, it means
that a constraint should be put in place to
ensure that the data entry in the field is
limited to dates only
• You should also establish best practices for
regularly cleaning the database for example
you can review the data by running a query
against the database and then checking the
output to find out if there are any
inconsistencies
• Data integrity can be achieved using integrity
rules or constraints
• These rules are specified at the database
schema level
• If a user attempts to execute an operation
that would violate the constraint then the
system must either reject the operation or in
some complicated situations perform some
compensating action
ENTITY INTEGRITY
• It states that no attribute participating in the
primary key of a relation is allowed to accept
null values (achieved by using the NOT NULL
keyword)
• This is so because the primary key is used to
identify individual tuples in a relation and if
we have a null value for the primary key it
implies that we cannot identify some tuples
and there might be duplicate entries in the
primary key column
The justification for the entity integrity
constraint is:
•Database relations correspond to entities from
the real world and by definition entities in the
real world are distinguishable, they have a
unique identification of some kind
•Primary keys perform the unique identification
function in the relational model
Therefore, a null primary key value would be a
contradiction in terms because it would be
saying there is some entity that has no identity.
REFERENTIAL INTEGRITY
• It states that if a relation R2 includes a foreign
key matching the primary key of another
relation R1 then every value of the FK in R2
must either be equal to the value of the PK in
some tuple of R1 or be wholly null
The justification for referential integrity
constraint is:
•If some tuple t2 from relation R2 references
some tuple t1 from relation R1, then tuple t1
must exist otherwise it does not make sense
•Therefore a given foreign key value must have
a matching primary key value somewhere in the
referenced relation if that foreign key value is
different from null
•Sometimes for practical reasons, it is necessary
to permit the foreign key to accept null values
example
ItemID WarehouseID Qty

A 1 10

A 2 5

B 1 30

C 2 60

WarehouseID Warehouse Address

1 Street1

2 Street2

1 Street1

2 Street2
SEMANTIC INTEGRITY
• It refers to the correctness of the meaning of
the data
• E.g the street number attribute value must be
positive because in the real world street
numbers are positive
There are several semantic integrity constraints
which include the following:
•Domain constraint- implies that a particular
attribute of a relation is defined on a particular
domain; it simply states that values of the
attribute in question are required to belong to
the set of values constituting the underlying
domain eg the id attribute can be declared as id
int (the int is the data type for the attribute)
• Null constraint- specifies that attribute values
cannot be null and this constraint is specified
with the NOT NULL construct ( the attribute
name is followed by the words NOT NULL for
that e.g. id int NOT NULL
• Unique constraint- specifies that attribute
values must be different; it is not possible to
have two tuples in a relation with the same
values for that attribute; a unique constraint is
usually specified with an attribute name
followed by the word UNIQUE
• Check constraint- it specifies a condition or
predicate on a relation data which is always
checked when data is manipulated; the predicate
states what to check and optionally what to do if
the check fails( violation response). If this
violation response is omitted, the operation is
rejected with a suitable return code; when the
constraint is executed, the system checks to see
whether the current state of the database
satisfies the specified constraint;
• If it does not the constraint is rejected
otherwise it is accepted and enforced from
that time on; this constraint is specified in the
database using a CHECK construct or a trigger
and it is checked by the system before or after
operations like insert, update and delete for
example when inserting employee details an
employee’s date of birth can not be greater
than the current system date.
READING ASSIGNMENT
With the aid of appropriate SQL statements
explain how the above integrity constraints are
enforced.

You might also like