Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Sections 11
Database relationship, Integrity, keys,
mapping conceptual model to
logical/physical model
Marge Hohly
Marge Hohly
Marge Hohly
Marge Hohly
Primary Key
Primary Key (PK)
Column or set of columns that uniquely identifies each
row in a table
Employee ID in Employee table (single unique)
Bank ID & Account ID in Accounts table (composite)
Marge Hohly
Foreign Key
Foreign Key (FK)
Marge Hohly
Key questions
what makes emp_no and payroll_id
good candidates for the primary key?
why is having alternate or unique
keys useful?
Marge Hohly
Column integrity
Contain values consistent with data
format of column
Column Name
Data Type
Optionality
BANK_NO
Number (5)
Not null
ACCT_NO
Number (8)
Not null
BALANCE
Number (12,2)
Not null
DATE_OPENED
Date
Marge Hohly
10
Marge Hohly
11
Data-Integrity Summary
Marge Hohly
12
Data-Integrity Summary
Entity integrity- no part of PK can be NULL
Referential integrity FK must match an
existing PK value (or else be NULL)
Column integrity column must contain
only values consistent with defined data
format
User-defined integrity data stored in
database must comply with the rules of the
business
Marge Hohly
13
Referential Integrity
Use Foreign Key to map relationships
A foreign key (FK) is a column or
combination of columns in one table
that refers to a primary key in the
same table or another table.
(next slide)
Marge Hohly
14
Example
Marge Hohly
15
Composite key
Made up of two or more values
Together unique
ENROLL Table/Entity
student_no & ticket_no
ACCOUNTS
bank_no & acct_no
Marge Hohly
16
JOBS Table
Marge Hohly
17
Transformation
Conceptual model, focus on the
business and its rules.
Data modeling pays attention to the
business requirements, regardless of
implementation.
Conceptual model
Logical model
Marge Hohly
18
Review 12.2.3
Marge Hohly
19
Marge Hohly
20
Terminology Mapping
Marge Hohly
21
22
23
Marge Hohly
24
Relationship mapping
Relationships are mapped to foreign
keys (at the many end)
Foreign keys enable users to access
related information from other tables.
Mapping relationships to relational
database structures is part of creating
the first-cut database design.
Marge Hohly
25
Relationship mapping
1:M mapping
Foreign key goes in
table at crows foot
from parent
FK1 Dept_id
mandatory is required
FK2 might be better
mgn_id and is optional
Does the president of
the company have a
manager?
Marge Hohly
26
Relationship mapping
FK is mandatory from this diagram
Marge Hohly
27
Enforcing Optionality
Optional or
Mandatory
determined by
crows foot end of
relationship
Marge Hohly
28
NonTransferable Relationship
Transferablility is a procedural model
Must be implemented by a program
Need to document this
constraint/business rule
Marge Hohly
29
Barred Relationship
Barred relationship is mapped to a
foreign-key column on the many side,
just like any other M:1 relationship.
Bar means it becomes part of the
composite primary key of the child
ACCOUNT table has both acct_id and
bank_id as the composite primary
key
Marge Hohly
30
TEAM
within
made up of
DEPARTMENT
within
made up of
DIVISION
within
made up of
COMPANY
31
Marge Hohly
32
33
Review
FK
1:M
*
o
rename one
M:M first resolve with an intersection
entity
Marge Hohly
34
Review cont.
Will be part of PK a composite key
FK on mandatory side
FK on either side
Marge Hohly
35
Arc mapping
Foreign key from the parent (single)
side are placed in the child (many)
side
The Foreign key is ALWAYS Optional
in the child
Only one of the Arc can be valid and
all others must be NULL
Mandatory relationship is enforced
with a check constraint
Marge Hohly
36
Arc constraint
You need a constraint to make sure
only one is NOT NULL at a time
Example: FK1, FK2, FK3, ....
ALTER EVENT constraint (FK1 is not
null and FK2 is null and FK3 is
null ....) OR (FK1 is null and FK2 is
not null and FK3 is null ....) OR (FK1
is null and FK2 is null and FK3 is not
null ....)
Marge Hohly
37
ARC mapping
If mandatory then one MUST be NOT
NULL
If optional then all may be NOT NULL
You will always need a check
constraint defined
Marge Hohly
38
Subtype Review
Marge Hohly
39
Subtype mapping
Mapping supertypes and subtypes
makes sure that the right information
gets stored with each type.
Marge Hohly
40
Subtype modeling
Marge Hohly
41
42
CHECK (epe_type =
FTE and salary is not
null and hourly_rate is
null and agy_id is null)
OR (epe_type = PTE
and salary is null and
hourly_rate is not null
and agy_id is not null)
Marge Hohly
43
Marge Hohly
44
Two-Table implementation
Marge Hohly
45
2-table cont.
A separate table
would be created
for SHIRTS and
SHOES.
Marge Hohly
46
Subtype Considerations
Subtype implementation may be
appropriate when:
Subtypes have very little in common. There are
few attributes at the supertype level and several
at the subtype level.
Most of the relationships are at the subtype
level.
Business rules and functionality are quite
different between subtypes.
How tables are used is different -- for example,
one table is being queried while the other is
being updated.
Marge Hohly
47