PRACTICAL DATA MODELING and DATABASE Data Modeling DESIGN USING
DATA MODELING AND DATABASE DESIGN Part 2
description
Transcript of DATA MODELING AND DATABASE DESIGN Part 2
DATA MODELING AND DATA MODELING AND DATABASE DESIGNDATABASE DESIGN
Part 2Part 2
Objectives
• Resolving ERD with LINK entities
• Recursive relationships
• Levels of normalization
• Integrity constraints
• Candidate keys, primary and foreign keys
Resolving Many to ManyRelationships
Most many to many relationships must be resolved before they can be mapped to a database design. We resolve that by creating new entity called LINK or INTERSECTION ENTITY.
CREATE an INTERSECTION ENTITY to control this relationship
EMPLOYEE_NEW_FORCE
ACTIVITYperformsan
requiresan
RESOLVED ERD
EMPLOYEE_NEW_FORCE
ACTIVITY
EMPLOYEE -ACTIVITY
* per_diem_rate
# emp_no* positiono job_description* salaryo commission
# activity_id* descriptiono start_dateo end_date* internal
requiresan
for
performsan
performed by
Recursive Relationships
• Define a relationship between an entity and itself as a recursive relationship.
• Represent that relationship with a “pig’s ear.”
EMPLOYEE#* id* last nameo first name* salary* start dateo manager_id
managedbymanagedby
the manager ofthe manager of
LevelLevel
First normal formFirst normal form
Second normal formSecond normal form
Third normal formThird normal form
CharacteristicsCharacteristics
All attributes must be single-valued.All attributes must be single-valued.
An attribute must depend upon its entity's An attribute must depend upon its entity's
entire UID (not only on part of UID)entire UID (not only on part of UID)
No non-UID attribute may be dependent No non-UID attribute may be dependent
upon another non-UID attribute.upon another non-UID attribute.
Levels of NormalizationBenefits
• Minimizes data redundancy
• Reduces integrity problems
• Identifies missing entities and relationships
Unnormalized Form
# emp_no* position* salaryo commissiono activity1o act1_start_dateo act1_end_dateo activity2o act2_start_dateo act2_end_dateo activity3o act3_start_dateo act3_end_date
EMPLOYEE
First Normal Form
EMPLOYEE# emp_no* position* salaryo commission
ACTIVITY# activity_id* descriptiono start_dateo end_date
Convert to third Normal Form
EMPLOYEE# id* last name* department location
DEPARTMENT# id* name
has
employed in
Third Normal Form
EMPLOYEE# id* last name
DEPARTMENT# id* name* location
has
employed in
Integrity Constraints• Ensure data consistency
• Should be enforced by the database server or the application software
• Correspond to keys– Primary key, unique key and foreign key
TypeType
EntityEntity
ReferentialReferential
ColumnColumn
User-DefinedUser-Defined
CharacteristicsCharacteristics
No part of a primary key can be NULL, and the value No part of a primary key can be NULL, and the value must be unique.must be unique.
Foreign key values must match a primary key or be Foreign key values must match a primary key or be NULL.NULL.
Values in the column must match the defined datatype.Values in the column must match the defined datatype.
Values must comply with business rules.Values must comply with business rules.
Defining a Primary Key• A primary key (PK) allows no duplicate
values and cannot be NULL.
• Each row is uniquely identified by a column or set of columns (composite primary key).
• There may be only one PK, but there may be many candidate keys.
Emp_noEmp_no activity_id activity_id per_diem_rate per_diem_rate
------------------------------------------------------------------------------------------
1212 10111011 500500
1515 10111011 400400
1616 910910 300300
0707 421421 150150
Composite Primary KeyComposite Primary Key
Table EMPLOYEE_ACTIVITYTable EMPLOYEE_ACTIVITY
Defining a Foreign Key
• A foreign key (FK) is a column in one table that refers to a PK in the same table or in another table.
• The value must either match the PK value in the ‘parent table’ or be NULL.
• If FK is part of a PK, then it cannot be NULL.
Defining a Foreign Key: Example
The DEPT_ID column is an FK in the EMP table, and refers to values in the ID column of the DEPT table.
IDID LAST_NAMELAST_NAME FIRST_NAMEFIRST_NAME ...... DEPT_IDDEPT_ID ......
11 MartinezMartinez AnaAna 101022 KimKim ToniToni 313133 TakeshiTakeshi TofiroTofiro 212144 McCoyMcCoy JamesJames 101055 GauletGaulet ChantalChantal 2020
IDID NAMENAME REGION_IDREGION_ID1010 CorporateCorporate 112020 HRHR 112121 SalesSales 113131 StockStock 11
Primary KeyPrimary Key
Foreign KeyForeign KeyEMP TableEMP Table
DEPT TableDEPT Table
Primary KeyPrimary Key