Lecture 5
Lecture 5
GHJ - PWR 1
THE BASIC TECHNIQUE OF LOGICAL
RELATIONAL DATABASE DESIGN FROM
AN ENTITY REALTIONSHIP MODEL
What is a database?
• A database is an organized collection of structured information, or
data, typically stored electronically in a computer system. A
database is usually controlled by a database management system
(DBMS). Together, the data and the DBMS, along with the
applications that are associated with them, are referred to as a
database system, often shortened to just database. [ORACLE]
GHJ - PWR 2
TYPES OF DATABASES
GHJ - PWR 3
RELATIONAL DATABASE
GHJ - PWR 4
RELATION AS TWO-DIMENSIONAL
TABLE
GHJ - PWR 5
PRIMARY AND FOREIGN KEY
DEFINITIONS
• Tables can contain primary keys (PK) which differentiate records
from one to another
• The primary key of a relational table uniquely identifies each
record in a table
• It can either be a normal attribute that is guaranteed to be unique
(such as International Standard Book Number (ISBN) in a table with no
more than one record per book) or it can be generated by the DBMS
(such as a globally unique identifier)
• Primary keys may consist of a single attribute (column within a table)
or multiple attributes (columns) in combination
• A foreign key (FK) is a field (column) in a relational table that
matches the primary key column of another table
• the foreign key can be used to cross-reference tables
• the foreign keys generally define parent-to-child relationships
between tables.
GHJ - PWR 6
PRIMARY (PK) AND FOREIGN KEY (FK)
PRESENTATION
Column Products
name PK symbol Types
PK code
name
desciption name
unit_of_measure
Foreign key FK1 types_code
GHJ - PWR 7
RULES OF TRANSFORMATION FROM ERD TO
LOGICAL RELATIONAL DATABASE DESIGN
[BARKER II, PP. F1-F10]
GHJ - PWR 8
A. Step 1 and 2
Step 1
Each simple entity is translated to a table.
The plural form of the entity for the table name is recommended.
Step 2
Each attribute is translated into a candidate column of the same
name, at which time a more precise format may be chosen.
Optional attributes become null columns.
Mandatory attributes become not – null columns.
GHJ - PWR 9
A. Step 3
GHJ - PWR 10
A. Step 4
GHJ - PWR 11
A. Example 1. Simple database design
A simple entity is one which is not a sub – type or has sub – types of its own.
from
CONVEYANCE SPOT
# * number the source of # * code
to
oPOZYCJA ZAM
scheduled departure * name
time
the destination of
operated by
CARRIER within
# * code recursive many to one optional
* name relationship
GHJ - PWR
the parent of 12
A. Example 1. Simple database design
SPOTS
Code not null PK CONVEYANCES
Name not null PK Number SPOTS
PK,FK1 Carrier_Code
PK Code
CARRIERS Scheduled_departure_time
Code not null PK FK2 From_Spot_Code Name
GHJ - PWR 14
APPLICATION OF A VIEW
GHJ - PWR 15
DATABASE DESIGN FOR SUB - TYPES
GHJ - PWR 16
B. I. ALL IN ONE TABLE – RULES
• Design process must be carried out for each sub-type and for each
of their sub-types and so on. A table is created for the outer super-
type entity and optional views may be created for each of the sub-
types.
• Step 1
• Create a table for the outer super-type entity
• Step 2
• As before (the rules A. 1,2,3,4), add to the table columns for attributes
and many to one and one to one relationships of the outer super-type
entity
GHJ - PWR 17
B. I. ALL IN ONE TABLE – RULES CONT.
• Step 3
• Add to the table columns created for attributes and many to one,
and one to one relationships of the all the sub-types are made
null
• A mandatory attribute or relationship for one sub-type would
not be applicable for another
• The integrity condition enforced either by application software
or by a view with enforced ″check option”
• Step 4
• At least one extra not-null column must be added to this table to
indicate TYPE, and it becomes a part of the primary key
• Step 5
• A relational view may be created for each of the sub-types to
enable to access only the data they need, as specified at the
business level by business functions defining actions on entities
that are sub-types
GHJ - PWR 18
B.I. EXAMPLE 2. DATABASE DESIGN FOR SUB-TYPES - ALL
IN ONE TABLE
# * number
o description
ID is a combination of attribute
part of and relationship
made up of
JOB ORDER
# * number Simple entity
* date
GHJ - PWR 19
B.I. EXAMPLE 2. DATABASE DESIGN FOR SUB-TYPES - ALL IN ONE
TABLE RULES
PK number
All columns from sub-types are made null Simple database rules
date
GHJ - PWR 20
B.I. 5. POSSIBLE RELATIONAL VIEWS, AS DEFINED
IN SQL
# * number
o description
ID is a combination of attribute
part of and relationship
made up of
JOB ORDER
# * number
* date Simple entity
GHJ - PWR 23
B.II. EXAMPLE 3. TABLES FOR SUB – TYPES
JOB ITEMS
number not null PK Primary key is a combination „number” column
job_order_number not null PK, FK and foreign key for job order from identified
job_code not null FK relationship
cost not null
description null Foreign key to job
JOB ITEMS
OTHER ITEMS PK number
number not null PK PK,FK1 job_order number JOBS
PK number PK number
PK,FK1 job_order number
date
description
tax comment
GHJ - PWR 25
C. EXCLUSIVE RELATIONSHIP
belongs to
ACCOUNT PERSON
# code
has got
# number
* date owned by ORGANIZATION
owner of # code
GHJ - PWR 27
C.I. COMMON DOMAIN – EXAMPLE C.I
belongs to PERSON
ACCOUNT # code (char 13) Simple entity
has got
# number ORGANIZATION
* date # code (char 13)
owned by owner of
GHJ - PWR 28
C.I. COMMON DOMAIN – SOLUTION FOR
EXAMPLE C.I
PERSONS
ACCOUNTS PK code
PK number
Column name for
relationship identifier Simple data
date
account belongs to base rules
owner_code ORGANIZATIONS
Column name for
PK code
entity identifier
ACCOUNTS
number integer 11 not null PK
date date not null
account_belongs_to char 1 not null (value ″P″ or ″O″)
owner_code char 13 not null FK
Both columns added are not null for these mandatory relationships,
owner_code column would take values for either PERSON or ORGANIZATION
GHJ - PWR 29
C.II. EXPLICIT FOREIGN KEYS
GHJ - PWR 30
C.II. EXPLICIT FOREIGN KEYS – EXAMPLE C.II
belongs to PERSON
ACCOUNT # code (integer 8) Simple entity
has got
# number ORGANIZATION
* date # code (char 13)
owned by owner of
GHJ - PWR 31
C.II. EXPLICIT FOREIGN KEYS – SOLUTION FOR
THE EXAMPLE C.II
PERSONS
PK code
ACCOUNTS
Simple data
PK number
Foreign key: base rules
date
to person FK1 person_code
to organization FK2 organization_code ORGANIZATIONS
PK code
ACCOUNTS
number integer 11 not null PK
date date not null
person_code integer 8 null FK
organization_code char 13 null FK
Both forein key columns are null for these mandatory relationships,
only
GHJ one of them may be entered, never both
- PWR 32
LOGICAL STRUCTURE OF DATABASE TABLE
DEFINITIONE (1)
EMPLOYEE
# no
* name
* date of birth
* profession
o address
Key Reference
# Column name Not null/Null Data type
(PK/FK) (table name)
1 no Not null numerical PK
GHJ - PWR
LOGICAL STRUCTURE OF DATABASE TABLE
DEFINITIONE (2)
belongs to PERSON
ACCOUNT # code (integer 8)
has got
# number ORGANIZATION
* date # code (char 13)
owned by owner of
Key Reference
# Column name Not null/Null Data type
(PK/FK) (table name)
1 number Not null numerical PK
GHJ - PWR
CASE TOOLS
GHJ - PWR 35
QUESTIONS TO HELP YOU L5
STUDY
1. What role does a primary key in the database play?
2. What role does a foreign key in the database play?
3. What elements does a database table consist of?
4. What groups of rules have been distinguished for the
transformation of ERD into a database?
5. When you transform an entity type into a database table, how
many tables are created?
6. After the entity type transformation, what kinds of columns in a
database table can arise?
GHJ - PWR 36
QUESTIONS TO HELP YOU L5
STUDY CONT.
7. What becomes a unary relationship in a database table?
8. What becomes a binary relationship in a database table?
9. What becomes an identifying relationship in a database table?
10. What rules apply to an entity supertype and subtypes
transformation?
11. In the transformation of an entity supertype, what role the view
does play?
12. For the transformation of exclusive relationship, what are the
rules?
GHJ - PWR 37