Vous êtes sur la page 1sur 45

Author: Muhammad Hamiz Mohd Radzi

Edited By: Zuhri Arafah Binti Zulkifli


Author: Muhammad Hamiz Mohd Radzi Edited By: Zuhri Arafah Zulkifli

Chapter Outline
 Basic Concepts

 Keys

 Integrity Rules

 Data Dictionary and System Catalog

 Relationships within the Relational Database

 Data Redundancy Revisited

 Indexes
Author: Muhammad Hamiz Mohd Radzi Edited By: Zuhri Arafah Zulkifli

Chapter Objectives

 At the end of this lesson, you should be able to:

 Describe tables and their characteristics

 Describe files and relations.

 Describe the super, candidate, primary, alternate and foreign keys

 Explain the entity and referential integrity

 Explain the no action, restrict, set to null, set default and cascade rules for reference
rows

 Define the one-to-one, one-to-many, many-to-many, and associative relationships


Author: Muhammad Hamiz Mohd Radzi Edited By: Zuhri Arafah Zulkifli

Basic Concepts
 The relational model was first proposed by E. F. Codd in his seminal paper ‘A
relational model of data for large shared data banks’ (Codd, 1970).

 Data appears to be stored in what we have been referring to as simple,


linear files.

 Relational databases are based on mathematics.

 A relational database is a collection of relations that, as a group, contain


the data that describes a particular business environment.
Author: Muhammad Hamiz Mohd Radzi Edited By: Zuhri Arafah Zulkifli

 The relational model’s objectives were specified as follows:

 To allow a high degree of data independence.

 To provide substantial grounds for dealing with data semantics,


consistency, and redundancy problems.

 To enable the expansion of set-oriented data manipulation


languages.

 A relation is a table with columns and rows.

 Attribute is a named column of a relation.


Author: Muhammad Hamiz Mohd Radzi Edited By: Zuhri Arafah Zulkifli

Records and Files

 Previously, data is stored in different files.

 File is the entire structure of records and fields.

 Files are vulnerable to changes, often not shared which could lead
to data redundancy and hard to retrieved if there are damages to
the files.

 Records can be defined as rows of a file and Field is the column


representing the fact.
Author: Muhammad Hamiz Mohd Radzi Edited By: Zuhri Arafah Zulkifli

 Record type - a structural description of each and every record in the file

 Record occurrence / Record instance - a specific record of the


salesperson file

 Row = record (files) = tuple (relation)

 Column = field (files) = attribute (relation).


Author: Muhammad Hamiz Mohd Radzi Edited By: Zuhri Arafah Zulkifli

Files vs Relations
FILES RELATIONS
Meaning of data WILL BE Meaning of data will NOT be
AFFECTED if the column is affected if the column is rearranged
rearranged to another order to another order

The rows of data CANNOT be The rows of data CAN be


rearranged to any order rearranged to any order

Every row/column position (a cell) Every row/column position (a cell)


can have a single OR multiple can have only a single value
value.

Redundancy may happen in a file Redundancy will not happen in a


relation
Author: Muhammad Hamiz Mohd Radzi Edited By: Zuhri Arafah Zulkifli

Tables and Their Characteristics


Relational model enables us to view data logically rather than physically
Table: two-dimensional structure composed of rows and columns
Contains group of related entities --> an entity set
Terms entity set and table are often used interchangeably.
 Table also called a relation because the relational model’s creator, Codd,
used the term relation as a synonym for table
Think of a table as a persistent relation:
A relation whose contents can be permanently saved for future use
Author: Muhammad Hamiz Mohd Radzi Edited By: Zuhri Arafah Zulkifli
Author: Muhammad Hamiz Mohd Radzi Edited By: Zuhri Arafah Zulkifli

Student table with attributes


Author: Muhammad Hamiz Mohd Radzi Edited By: Zuhri Arafah Zulkifli

Relational Keys

 As stated previously, there are no duplicate tuples within a relation.

 Therefore, we need to be able to identify one or more attributes (called


relational keys) that uniquely identifies each tuple in a relation.

 The important keys in a relation are:

 Superkey
 Candidate Key
 Primary Key
 Alternate Key
 Foreign Key
Author: Muhammad Hamiz Mohd Radzi Edited By: Zuhri Arafah Zulkifli

 Superkey is an attribute, or set of attributes, that uniquely identifies


a tuple within a relation.

 Candidate key is a minimal superkey with no redundancy.

 Primary key is the candidate key that is selected to identify


uniquely within the key relation.

SUPERKEY

CANDIDATE
KEY
PRIMARY KEY
Author: Muhammad Hamiz Mohd Radzi Edited By: Zuhri Arafah Zulkifli

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

Superkey:

 vehicleID  vehicleID,  vehicleID, carPlateNo, engineID


 carPlateNo carPlateNo  vehicleID, carPlateNo, carName
 engineID  vehicleID, engineID  vehicleID, engineID, carName
 vehicleID, carName  carPlateNo, engineID, carName
 carPlateNo,  vehicleID, carPlateNo, engineID,
engineID carName
 carPlateNo,
carName
 engineID, carName
Author: Muhammad Hamiz Mohd Radzi Edited By: Zuhri Arafah Zulkifli

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,  vehicleID, carPlateNo,


carPlateNo engineID
 carPlateNo
 engineID  vehicleID, engineID
 carPlateNo,
engineID

Primary Key: vehicleID


Author: Muhammad Hamiz Mohd Radzi Edited By: Zuhri Arafah Zulkifli

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:

 carPlateNo  vehicleID,  vehicleID, carPlateNo,


carPlateNo engineID
 engineID
 vehicleID, engineID
 carPlateNo,
engineID

 Alternate Key is a candidate key that was not chosen to be the primary
key of the relation.
Author: Muhammad Hamiz Mohd Radzi Edited By: Zuhri Arafah Zulkifli

 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).
Author: Muhammad Hamiz Mohd Radzi Edited By: Zuhri Arafah Zulkifli

Integrity Rules
 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.

 Null value represents a value for an attribute that is currently


unknown or is not applicable for this tuple.
Author: Muhammad Hamiz Mohd Radzi Edited By: Zuhri Arafah Zulkifli

Entity Integrity
 The first integrity rule applies to the primary keys of base relations.

 Entity integrity can be defined as; in a base relation, no attribute of a primary


key can be null.

 For example, in a STUDENT relation, studentID is considered as primary


key.

 studentID cannot be null as it is the determinant for the other attributes like
studentName, studentAddress etc.

 Hence, a primary key cannot be null and this kind of rule is called entity
integrity.
Author: Muhammad Hamiz Mohd Radzi Edited By: Zuhri Arafah Zulkifli

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.

 By definition, referential integrity means that if a foreign key exists in a


relation, either the foreign key value must match a primary key value of
some tuple in its home relation or the foreign key value must be wholly null.

 For example, branchNo in the Staff relation is a foreign key targeting the
branchNo attribute in the home relation, Branch.
Author: Muhammad Hamiz Mohd Radzi Edited By: Zuhri Arafah Zulkifli

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 a No Problem


match foreign key in child’s table

UPDATE Cannot be done if there is a Cannot be done if there is no


match foreign key in child’s table match existing primary key in
parent’s table
Author: Muhammad Hamiz Mohd Radzi Edited By: Zuhri Arafah Zulkifli

Delete & Update Actions for Reference


Rows
 Early relational DBMSs did not provide any control mechanisms for
referential integrity.

 Modern relational DBMSs provide sophisticated control mechanisms for


referential integrity:

 Delete rules

 Insert rules

 Update rules
Author: Muhammad Hamiz Mohd Radzi Edited By: Zuhri Arafah Zulkifli

 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".

 If an action is not explicitly specified, it defaults to "NO ACTION".

 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.
Author: Muhammad Hamiz Mohd Radzi Edited By: Zuhri Arafah Zulkifli

 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.

 CASCADE: A "CASCADE" action propagates the delete or update


operation on the parent key to each dependent child key.

 For an "ON DELETE CASCADE" action, this means that each row in the
child table that was associated with the deleted parent row is also
deleted.

 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.
Author: Muhammad Hamiz Mohd Radzi Edited By: Zuhri Arafah Zulkifli
Author: Muhammad Hamiz Mohd Radzi Edited By: Zuhri Arafah Zulkifli

The Data Dictionary and System Catalog


Data dictionary

Used to provide detailed accounting of all tables found within the


user/designer-created database

Contains (at least) all the attribute names and characteristics for
each table in the system

Contains metadata—data about data

Sometimes described as “the database designer’s database”


because it records the design decisions about tables and their
structures
A Sample Data Dictionary

Author: Muhammad Hamiz Mohd Radzi Edited By: Zuhri Arafah Zulkifli
Author: Muhammad Hamiz Mohd Radzi Edited By: Zuhri Arafah Zulkifli

The System Catalog

System catalog

Contains metadata

Detailed system data dictionary that describes all objects within the
database
Terms “system catalog” and “data dictionary” are often used
interchangeably
Can be queried just like any user/designer-created table
Author: Muhammad Hamiz Mohd Radzi Edited By: Zuhri Arafah Zulkifli
Author: Muhammad Hamiz Mohd Radzi Edited By: Zuhri Arafah Zulkifli

Relationships
 There are binary and ternary relationships in DBMS.

 Binary means there are 2 entities connected to each other and ternary
means 3 entities connected to each other.

 These relationships can be divided into

 One-to-one relationship

 One-to-many relationship

 Many-to-many relationship
Author: Muhammad Hamiz Mohd Radzi Edited By: Zuhri Arafah Zulkifli

Binary One-to-one Relationship


Author: Muhammad Hamiz Mohd Radzi Edited By: Zuhri Arafah Zulkifli

Binary One-to-many Relationship


Author: Muhammad Hamiz Mohd Radzi Edited By: Zuhri Arafah Zulkifli

Binary Many-to-many Relationship

*Associative Entity is needed to solve many-to-


many relationship. (also known as Bridge
Entity)
Author: Muhammad Hamiz Mohd Radzi Edited By: Zuhri Arafah Zulkifli

The ERD’s 1:M Relationship between COURSE and CLASS


Author: Muhammad Hamiz Mohd Radzi Edited By: Zuhri Arafah Zulkifli
Author: Muhammad Hamiz Mohd Radzi Edited By: Zuhri Arafah Zulkifli
Author: Muhammad Hamiz Mohd Radzi Edited By: Zuhri Arafah Zulkifli

Ternary Relationship
Author: Muhammad Hamiz Mohd Radzi Edited By: Zuhri Arafah Zulkifli

Unary one-to-one Relationship


Author: Muhammad Hamiz Mohd Radzi Edited By: Zuhri Arafah Zulkifli

Unary one-to-many Relationship


Author: Muhammad Hamiz Mohd Radzi Edited By: Zuhri Arafah Zulkifli

Unary Many-to-many Relationship


Author: Muhammad Hamiz Mohd Radzi Edited By: Zuhri Arafah Zulkifli

Unary Relationship
Author: Muhammad Hamiz Mohd Radzi Edited By: Zuhri Arafah Zulkifli

Three Types of Relationship Degrees


Author: Muhammad Hamiz Mohd Radzi Edited By: Zuhri Arafah Zulkifli

Data Redundancy Revisited


Relational database facilitates control of data redundancies
through use of foreign keys

To be controlled except the following circumstances


Data redundancy must be increased to make the database serve crucial
information purposes

Exists to preserve the historical accuracy of the data


Author: Muhammad Hamiz Mohd Radzi Edited By: Zuhri Arafah Zulkifli

Index
Orderly arrangement to logically access rows in a table

Index key: Index’s reference point that leads to data location


identified by the key

Unique index: Index key can have only one pointer value
associated with it

Each index is associated with only one table


Author: Muhammad Hamiz Mohd Radzi Edited By: Zuhri Arafah Zulkifli

References
Database Systems: Design, Implementation, & Management, 7th Edition, Rob &
Coronel

Database Systems: A Practical Approach to Design, Implementation, and


Management, Thomas Connolly and Carolyn Begg, 5th Edition, 2010, Pearson.

Fundamental of Database Management Systems, Mark L. G., 2nd Edition,


2012, John Wiley.

Vous aimerez peut-être aussi