Module 4 - Relational Database
Module 4 - Relational Database
Relational Model
3
Components of relational model
Data structure
◦ Tables (relations), rows, columns
Data manipulation
◦ Powerful SQL operations for retrieving and modifying data
Data integrity
◦ Mechanisms for implementing business rules that
maintain integrity of manipulated data
4
Relation
• A relation is a named, two-dimensional table of data.
• A table consists of rows (records) and columns (attribute or field).
• Requirements for a table to qualify as a relation:
➢ It must have a unique name.
➢ Every attribute value must be atomic (not multivalued, not composite).
➢ Every row must be unique (can’t have two rows with exactly the same values
for all their fields).
➢ Attributes (columns) in tables must have unique names.
➢ The order of the columns must be irrelevant.
➢ The order of the rows must be irrelevant.
NOTE: All relations are in 1st Normal form.
5
Correspondence with E-R Model
▪ Relations (tables) correspond with entity types and with many-to-many
relationship types.
▪ Rows correspond with entity instances and with many-to-many relationship
instances.
▪ Columns correspond with attributes.
6
Key Fields
▪ Keys are special fields that serve two main purposes:
▪ Primary keys are unique identifiers of the relation. Examples include employee
numbers, social security numbers, etc. This guarantees that all rows are unique.
▪ Foreign keys are identifiers that enable a dependent relation (on the many side of a
relationship) to refer to its parent relation (on the one side of the relationship).
7
Figure 4-3 Schema for four relations (Pine Valley Furniture Company)
Primary Key
Foreign Key
(implements 1:N relationship
between customer and order)
▪ Entity Integrity
▪ No primary key attribute may be null. All primary key fields MUST have data.
9
Domain definitions enforce domain integrity constraints.
11
Figure 4-5 - Referential integrity constraints (Pine Valley Furniture)
Referential
integrity
constraints are
drawn via arrows
from dependent to
parent table
Referential integrity
constraints are
implemented with
foreign key to primary
key references.
14
Figure 4-8 Mapping a regular entity
(a) CUSTOMER
entity type with
simple
attributes
(a)
(b)
17
Transforming EER Diagrams into Relations (cont.)
Mapping Weak Entities
▪Becomes a separate relation with a foreign
key taken from the superior entity
▪Primary key composed of:
✓Partial identifier of weak entity
✓Primary key of identifying relation (strong
entity)
18
Figure 4-11 Example of mapping a weak entity
a) Weak entity DEPENDENT
21
Figure 4-12 Example of mapping a 1:M relationship
a) Relationship between customers and orders
Foreign key
new
Foreign key
intersection
relation
27
Figure 4-15 Example of mapping an associative entity
a) An associative entity
32
Figure 4-17 Mapping a unary 1:N relationship
(a) EMPLOYEE
entity with unary
relationship
(b)
EMPLOYEE
relation with
recursive
foreign key
(a) Bill-of-materials
relationships (M:N)
34
Transforming EER Diagrams into Relations (cont.)
Mapping Ternary (and n-ary) Relationships
➢ One relation for each entity and one for the
associative entity
➢ Associative entity has foreign keys to each
entity in the relationship
35
Figure 4-19 Mapping a ternary relationship
a) PATIENT TREATMENT Ternary relationship with associative entity
38
Figure 4-20 Supertype/subtype relationships