Lecture 2 - The Relational Model
Lecture 2 - The Relational Model
LECTURE 2
THE RELATIONAL
MODEL
Muhammad Hamiz Mohd Radzi
Faiqah Hafidzah Halim
Contents
■ Terminology
■ Integrity Constraints
■ Views
Objectives
■ At the end of this lesson, you should be able to:
22 Deer Rd London
1‹S Argyll St Ahercleen
163 Main St Glasgow
BOO4 32 Marine Rd Bristol BSff INZ
BOO2 56 Glover Dr London
Degree
Pri mary key Foreign key
Stafi
staffNo fName IName position sex DOB salary branch No
SL21 John White Manager M 1-Oct-45 3OOOO BOO5
Relation
0
SG14 David Ford Sup›ervisor M 24-far-S 1gooo B0O3
SA9 Mary Howe Assistant F 19-Feb-70 9OOO BOO7
SQL Susan Brancl Manager F 3-Jan-40 24OOO BOO3
SL41 Julie Lee Assistant F 13-Jun-6S 9000 BOOK
Example of Attribute Domains
■ Tuple is a row of a relation.
■ Consider two sets, D1 & D2, where D1 = {2, 4} and D2 = {1, 3, 5}.
■ D1 X D2 = {(2, 1), (2, 3), (2, 5), (4, 1), (4, 3), (4, 5)}
■ May specify which pairs are in relation using some condition for selection; e.g.
■ second element is 1:
R = {(x, y) | x ∈D1, y ∈D2, and y = 1}
■ D1 × D2 × D3 = {(1, 2, 5), (1, 2, 6), (1, 4, 5), (1, 4, 6), (3, 2, 5), (3, 2, 6), (3, 4, 5),
(3, 4, 6)}
■ Any set of n-tuples from this Cartesian product is a relation on the n sets.
Database Relation
■ Relation schema: a named relation defined by a set of attribute and
domain name pairs.
■ Relational database schema: Set of relation schemas, each with a distinct name.
Properties of Relation
• Relation name is distinct from all other relation names in relational schema.
• Superkey
• Candidate Key
• Primary Key
• Alternate Key
• Foreign Key
• Superkey is an attribute, or set of attributes, that uniquely identifies
a tuple within a relation.
SUPERKEY
CANDIDATE KEY
PRIMARY KEY
vehicleID carPlateNo engineID carName
101 WTY 1234 9999876 Proton Kembara
102 WXY 4567 5644321 Perodua Alza
103 CDA 3389 6667889 Proton Kembara
104 MCD 1745 1277653 Honda City
• Candidate
Key:
• vehicleID, • vehicleID, carPlateNo, engineID
• vehicleID carPlateNo
• carPlateNo • vehicleID, engineID
• engineID • vehicleID,
carPlateNo
• vehicleID, engineID
• carPlateNo, engineID Primary Key: vehicleID
vehicleID carPlateNo engineID carName
101 WTY 1234 9999876 Proton Kembara
102 WXY 4567 5644321 Perodua Alza
103 CDA 3389 6667889 Proton Kembara
104 MCD 1745 1277653 Honda City
• Alternate
Key:
• vehicleID, • vehicleID, carPlateNo, engineID
• carPlateNo carPlateNo
• engineID • vehicleID, engineID
• vehicleID,
carPlateNo
• vehicleID, engineID
• carPlateNo, engineID
• Alternate Key is a candidate key that was not chosen to be the primary key
of the relation.
• Foreign Key is an attribute or group of attributes that serves as the
primary key of one relation and also appears in another relation
(foreign key in this relation).
Integrity Constraints
• Integrity constraints is to make sure that the data is accurate.
• Inaccurate of data could leave the whole database system unreliable and
can affect the company from running smoothly.
• There are two important integrity rules, which are constraints or restrictions
that apply to all instances of the database.
• The two principal rules for the relational model are known as entity
integrity and referential integrity.
• The first integrity rule applies to the primary keys of base relations.
• Hence, a primary key cannot be null and this kind of rule is called
entity integrity.
Referential integrity
• The second integrity rule applies to foreign keys.
• Revolves around the circumstance of trying to refer to data in one relation in the
database, based on values in another relation.
• For example, branchNo in the Staff relation is a foreign key targeting the
branchNo attribute in the home relation, Branch.
Insert, delete, update for
referential integrity
PARENT TABLE CHILD TABLE
INSERT No Problem Cannot be done if there is
no match primary key in
parent’s table
DELETE Cannot be done if there is No Problem
a match foreign key in
child’s table
• Delete rules
• Insert rules
• Update rules
• The ON DELETE and ON UPDATE action associated with each foreign key
in database is one of "NO ACTION", "RESTRICT", "SET NULL", "SET DEFAULT"
or "CASCADE".
• NO ACTION: Configuring "NO ACTION" means just that: when a parent key
is modified or deleted from the database, no special action is taken.
• RESTRICT: The "RESTRICT" action means that the application is prohibited from
deleting (for ON DELETE RESTRICT) or modifying (for ON UPDATE RESTRICT)
a parent key when there exists one or more child keys mapped to it.
• SET NULL: If the configured action is "SET NULL", then when a parent
key is deleted (for ON DELETE SET NULL) or modified (for ON UPDATE SET
NULL), the child key columns of all rows in the child table that mapped to
the parent key are set to contain SQL NULL values.
• SET DEFAULT: The "SET DEFAULT" actions are similar to "SET NULL",
except that each of the child key columns is set to contain the columns
default value instead of NULL.
For an "ON UPDATE CASCADE" action, it means that the values stored in
each dependent child key are modified to match the new parent key values.
• Views are dynamic, meaning that changes made to base relations that
affect view attributes are immediately reflected in the view.
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.
• Modifications restrictions
• 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.
• Classes of views are defined as: