You are on page 1of 35

Chapter 2 [Part 2]:

DATABASE BUILDING
BLOCKS
Nulls
• Nulls:
– No data entry/something is unknown, therefore, insert
NULL to a particular attribute
– Not permitted in primary key
– Should be avoided in other attributes
– Can represent
• An unknown attribute value Solution: assign
default value
• A known, but missing, attribute value
• A “not applicable” condition if(price == 0 || price == NULL)

– Can create problems when functions such as COUNT,


AVERAGE, and SUM are used
– Can create logical problems when relational tables are
linked
Nulls
Examples:
Table name: member
IDmembe name street city postcod telephone datejoined
r e
10001 Syakirin 123 Desa Jaya Jengka 26400 09-575755 2/1/1998
10002 Islah 00000000 3/4/1997
10003 Ihsan 5 Skudai Kiri Johor 81300 07-564233 12/31/2001

** null
ENTITY INTEGRITY &
REFERENTIAL INTEGRITY
ENTITY INTEGRITY

• Entity integrity is an integrity rule which


states that every table must have:
• a primary key and that the column or
columns chosen to be the primary key
should be unique and not null.
Entity Integrity
• When all rows of a database table are
uniquely identified.
PK value must
be unique

PK value can’t be NULL


ENTITY INTEGRITY

• PK null value can have any of the


following meanings:
1. The value does not exist
2. The value exist, but it is unknown
3. The value is not applicable
4. The value is unknown or it is not applicable
ENTITY INTEGRITY
• A direct consequence of this integrity rule
is that duplicate rows are forbidden in a
table. If each value of a primary key must
be unique no duplicate rows can logically
appear in a table.
• The NOT NULL characteristic of a primary
key ensures that its value can be used to
identify all rows in a table.
Referential Integrity
• Proper FK values in one table must either
match PK values in the related table or
must be null.
• If a table’s FK values meet the “match or
null” requirements, they are said to exhibit
referential integrity.
• Referential integrity ensures that it will be
impossible to point to PK values that do
not exist. Example:
Referential Integrity
• database concept that ensures that
relationships between tables remain
consistent.
• When one table has a foreign key to
another table, the concept of referential
integrity states that you may not add a
record to the table that contains the
foreign key unless there is a
corresponding record in the linked table.
Referential Integrity
• Example: Table: Department
Table: Employee
deptID deptName
empID empName deptID
1 Finance
001 Siti 1
2 IT
002 Nisa 2

003 Adam NULL


Referential Integrity
• any field in a table that is declared a
foreign key can contain only values from a
parent table's primary key or a candidate
key.
• For instance, deleting a record that
contains a value referred to by a foreign
key in another table would break
referential integrity.
An example of a database that has not enforced
referential integrity.

• There is a foreign key (artist_id) value in the album table that


references a non-existent artist — in other words there is a foreign
key value with no corresponding primary key value in the referenced
table. What happened here was that there was an artist called
"Aerosmith", with an artist_id of 4, which was deleted from the artist
table. However, the album "Eat the Rich" referred to this artist. With
referential integrity enforced, this would not have been possible.
REFERENTIAL INTEGRITY

• It also includes the techniques known as


cascading update and cascading delete,
which ensure that changes made to the
linked table are reflected in the primary
table.
EXAMPLE 2: REFERENTIAL
INTEGRITY
• Consider the situation where we have two tables:
EMPLOYEE and MANAGER. The EMPLOYEE table has
a foreign key attribute entitled ManagedBy which points
to the record for that employee’s manager in the
Managers table.
Table: Employee Table: Manager
empID ManageBy ManageBy managerName
001 1 1

002 2 2

003
Referential integrity enforces the
following three rules:
• We may not add a record to the EMPLOYEE
table unless the ManagedBy attribute points to a
valid record in the MANAGER table.
• If the primary key for a record in the MANAGER
table changes, all corresponding records in the
EMPLOYEE table must be modified using a
cascading update.
• If a record in the Managers table is deleted, all
corresponding records in the EMPLOYEE table
must be deleted using a cascading delete.
Why are entity integrity and referential
integrity important in a database?

• It's the key to correct data being entered in


the database. It's the rules that govern
what can be inserted and what cannot.
BENEFITS OF REFERENTIAL
INTEGRITY
• Improved data quality
– An obvious benefit is the boost to the quality
of data that is stored in a database. There can
still be errors, but at least data references are
genuine and intact.
• Faster development
– Referential integrity is declared. This is much
more productive (one or two orders of
magnitude) than writing custom programming
code.
BENEFITS OF REFERENTIAL
INTEGRITY
• Fewer bugs
– The declarations of referential integrity are
more concise than the equivalent programming
code. In essence, such declarations reuse the
tried and tested general-purpose code in a
database engine, rather than redeveloping the
same logic on a case-by-case basis.
• Consistency across applications
– ensures the quality of data references across
the multiple application programs that may
access a database.
Putting It All Together in a Small
Database
Table name: CUSTOMER
CUS_NUM CUS_LNAME CUS_FNAME CUS_ADDRESS CUS_BALANCE
112 Chasteen Anne xxxxxxxx $0.00
114 Willimson kirby yyyyyyyy $235,68
115 Guahtoty Hrman zzzzzzzzz $415,53
118 Smith Mary aaaaaaaa $376,97
122 taylor Cgristopher bbbbbbbb $0,00
123 Trevors Anne cccccccccc $300,22

Table name: INVOICE


INV_NUM INV_DATE CUS_NUM INV_SUBTOTAL INV_TAX INV_TOTAL INV_PAYMENT INV_BALACE
100541 11-Jun-02 112 $86,90 $6,95 $93,85 $93,85 $0,00
100542 11-Jun-02 122 $24,95 $2,00 $26,95 $26,95 $0,00
100543 11-Jun-02 112 $91,15 $7,29 $98,44 $98,44 $0,00

Table name: LINE


INV_NUM LINE_NUM PROD_CODE LINE_UNITS LINE_PRICE LINE_AMOUNT
100541 1 SME-123456 8 $10,05 $80,40
100541 2 FRT-765432 1 $6,50 $6,50
100542 1 DFR-345678 1 $24,95 $24,95
100543 1 SME-123456 4 $10,05 $40,20
100543 2 DFR-345678 1 $24,95 $24,95
100543 3 GRD-998877 4 $6,50 $26,00

Table name: PRODUCT


PROD_CODE PROD_DESCR PROD_COST PROD_PRICE PROD_QOH PROD_MIN_QTY
DDE-876543 Torque wrench & socket set $15,40 $21,05 67 50
DFR-345678 Cordless drill, 1/4 in chuck $18,00 $24,95 23 15
FRT-765432 Pipe wrench, adjustable head $3,85 $6,50 44 35
GRD-998877 Screwdriver, ratchet head $3,52 $6,50 61 40
KLG-325421 8-in saw blade, carbide tips $8,19 $14,45 32 25
SME-123456 16-in chain saw bar $5,95 $10,05 49 30
Database building blocks
CONTENTS

• Documentation: selecting and naming the


attributes
• Attribute Characteristics/Type
Documentation: Selecting and naming
the attributes
• Documentation is a process that yields a
precise definition and description of the
database’s components and the
relationships between those
components.
• Good documentation facilitates system
maintenance and modifications.
• Attributes (fields) associated with a table
should be named to identify both their
function and location.
Documentation: Selecting and naming
the attributes
• Useful naming conventions
1. Table names are singular and they are
CAPITALIZED
2. Table names are all nouns
3. Example: PRODUCT, CUSTOMER,
INVOICE
4. Table column (attribute) names consists of
two parts: prefix identified table_attribute
5. Example: EMP_DOB, EMP_LNAME,
PROD_CODE
ATTRIBUTES
CHARACTERISTICS/TYPE

• Simple Attributes
• Composite Attributes
• Derived Attributes
• Single value and Multivalue attributes
Simple Attributes

• These are atomic values, which cannot be


divided further.
• Example: STUDENT_AGE
Composite Attributes

• An attribute that can be further subdivided


into other meaningful components.
• Example: EMP_NAME is not simple.
• EMP_NAME can be split to EMP_FNAME,
EMP_MNAME,EMP_LNAME
Derived Attributes
• These attributes do not exist in the physical
database but their values are derived from
other attributes present in database.
• Example:
EXAMPLE OF DERIVED
ATTRIBUTES
Single –Valued & Multivalued

• Single-valued can only have one value.


• Example: your one first name, one birth
date, one IC number
• Multivalued can have many values in one
attibutes
• Example: EMP_DEGREE (Bsc. Msc, PhD)
Composite Table

• A composite table is defined by a diagram with simple


tables as nodes connected with arc – relations.
Composite Table (cont..)

• composite table to link the tables together


to resolve the many-to-many relationship.
• For example, a STUDENT table and a
COURSE table has many-to-many
relationship.
• Therefore, a REGISTRATION table can be
created with a composite keys to solve the
issue.
Summary of Chapter 2

5 Important Keys: PK,


Composite PK, Entity integrity and Documentation:
Candidate Key, referential integrity Selecting and naming att.
Secondary Key, FK

Attribute Characteristics:
simple (atomic) and
composite, derived, Composite tables
single-valued &
multivalued

You might also like