34
Logical Design Logical Design database database design design

Logical Design database design. Dr. Mohamed Osman Hegaz2 Conceptual Database Designing –Provides concepts that are close to the way many users perceive

Embed Size (px)

Citation preview

Page 1: Logical Design database design. Dr. Mohamed Osman Hegaz2 Conceptual Database Designing –Provides concepts that are close to the way many users perceive

Logical Design Logical Design database design database design

Page 2: Logical Design database design. Dr. Mohamed Osman Hegaz2 Conceptual Database Designing –Provides concepts that are close to the way many users perceive

Dr. Mohamed Osman Hegaz2

• Conceptual Database Designing– Provides concepts that are close to the way many users

perceive data. (modeling the collected information at a high-level of abstraction)

• Logical Database Designing– Provides concepts that arrange the data into a logical structure

which can then be mapped into the storage objects

• Physical Database Designing– Provide concepts that describe the details of how data is stored

in the computer.

Database DesigningDatabase Designing

Page 3: Logical Design database design. Dr. Mohamed Osman Hegaz2 Conceptual Database Designing –Provides concepts that are close to the way many users perceive

(A)- Entity- Relationship to (A)- Entity- Relationship to Relational MappingRelational Mapping

Logical Design Database Logical Design Database Design Design

Page 4: Logical Design database design. Dr. Mohamed Osman Hegaz2 Conceptual Database Designing –Provides concepts that are close to the way many users perceive

Motivation

Page 5: Logical Design database design. Dr. Mohamed Osman Hegaz2 Conceptual Database Designing –Provides concepts that are close to the way many users perceive

Contents

Entity- Relationship to Relational Mapping

• Steps for mapping a basic ER diagram to a relational schema

Page 6: Logical Design database design. Dr. Mohamed Osman Hegaz2 Conceptual Database Designing –Provides concepts that are close to the way many users perceive

Mapping Method

Method for mapping a conceptual schema developed using the ER model to a relational database schema comprises 7 steps:

1.Entity Mapping2.Weak Entity Mapping3.Binary 1: 1 Relationship Mapping4.Binary 1: N Relationship Mapping5.Binary M: N Relationship Mapping6.Multi- valued Attribute Mapping7.N- ary Relationship Mapping

Page 7: Logical Design database design. Dr. Mohamed Osman Hegaz2 Conceptual Database Designing –Provides concepts that are close to the way many users perceive

Step 1: Entity Mapping

For each regular (non- weak) entity type E, create a relation R that includes all simple attributes of E

– Include only simple component attributes of a composite attribute

– Choose one key attribute of E as primary key for R. If key of E is composite, the set of simple attributes together should form the key

– Add following attributes in subsequent steps: Foreign key, Relationship, Multi- valued

Page 8: Logical Design database design. Dr. Mohamed Osman Hegaz2 Conceptual Database Designing –Provides concepts that are close to the way many users perceive

Step 1: Example

Page 9: Logical Design database design. Dr. Mohamed Osman Hegaz2 Conceptual Database Designing –Provides concepts that are close to the way many users perceive

Step 1: Example

Entity Types in the Company Database:EMPLOYEE, DEPARTMENT, PROJECT

Page 10: Logical Design database design. Dr. Mohamed Osman Hegaz2 Conceptual Database Designing –Provides concepts that are close to the way many users perceive

Step 1: Example

Entity Types in the Company Database:EMPLOYEE, DEPARTMENT, PROJECT

Page 11: Logical Design database design. Dr. Mohamed Osman Hegaz2 Conceptual Database Designing –Provides concepts that are close to the way many users perceive

Schema (in progress)

• EMPLOYEE [Ssn, Fname, Mit, Lname, Dob, Address, Sex, Salary]

• DEPARTMENT [Dnumber, DName]

• PROJECT [Pno, PName, Plocation]

Page 12: Logical Design database design. Dr. Mohamed Osman Hegaz2 Conceptual Database Designing –Provides concepts that are close to the way many users perceive

Step 2: Weak Entity Mapping

For each weak entity type W with owner entity type E create a relation R that includes all simple attributes of W

– Include as foreign key attributes in R the primary key attributes of the relation( s) that correspond to the owner entity types. (This maps the identifying relationship type of W)

– The primary key of R is the combination of the primary key( s) of the owner( s) and the partial key of the weak entity type W (if any)

Page 13: Logical Design database design. Dr. Mohamed Osman Hegaz2 Conceptual Database Designing –Provides concepts that are close to the way many users perceive

Step 2: Example

Weak Entity Types in the Company Database: DEPENDENT

Page 14: Logical Design database design. Dr. Mohamed Osman Hegaz2 Conceptual Database Designing –Provides concepts that are close to the way many users perceive

Schema (in progress)

• EMPLOYEE [Ssn, Fname, Mit, Lname, Dob,Address, Sex, Salary]

• DEPARTMENT [Dnumber, DName]

• PROJECT [Pno, PName, Plocation]

• DEPENDENT [ESSN, DepName, Sex, DOB, Relationship]

Page 15: Logical Design database design. Dr. Mohamed Osman Hegaz2 Conceptual Database Designing –Provides concepts that are close to the way many users perceive

Step 3: Binary 1: 1 Relationship

For each binary 1: 1 relationship type RT, identify relations S & T that correspond to the entity types participating in RT– Choose one relation (say S) and include as foreign key in S the primary key of T– It is better to choose as S, the entity type with total participation in RT– Include all the simple attributes (or simple components of composite attributes) of the 1: 1 relationship type RT as attributes of S

Example paintings, artists

Page 16: Logical Design database design. Dr. Mohamed Osman Hegaz2 Conceptual Database Designing –Provides concepts that are close to the way many users perceive

Step 3: ExampleBinary 1: 1 relationship type in the CompanyDatabase: MANAGES

DEPARTMENT serves in the role of “S” because its participation in theMANAGES relationship type is total (every department has a manager)• Include the primary key of the EMPLOYEE relation as a foreign key in the DEPARTMENT relation (renamed MGRSSN)• Include the simple attribute StartDate of the MANAGES relation (renamed MGRSTART)

Page 17: Logical Design database design. Dr. Mohamed Osman Hegaz2 Conceptual Database Designing –Provides concepts that are close to the way many users perceive

Schema (in progress)

EMPLOYEE [Ssn, Fname, Mit, Lname, Dob, Address, Sex, Salary]

• DEPARTMENT [Dnumber, Dname, MGRSSN, MgrStart]

• PROJECT [Pno, PName, Plocation]

• DEPENDENT [ESSN, DepName, Sex, DOB, Relationship]

Page 18: Logical Design database design. Dr. Mohamed Osman Hegaz2 Conceptual Database Designing –Provides concepts that are close to the way many users perceive

Step 4: Binary 1: N Relationship

For each (non- weak) binary 1: N relationship type RT, identify relation S that represents the participating entity type at the N- side of the relationship type

– Include as foreign key of S the primary key of relation T that represents the other entity type participating in RT

– Include any simple attributes (or simple components of composite attributes) of the 1: N relationship type as attributes of S

Page 19: Logical Design database design. Dr. Mohamed Osman Hegaz2 Conceptual Database Designing –Provides concepts that are close to the way many users perceive

Binary I: N relationship types in the Company Database: WORKS_ FOR, CONTROLS and SUPERVISES

Where primary key of the DEPARTMENT relation isincluded as a foreign key in the EMPLOYEE relation(renamed Dno)

Step 4: Example

Page 20: Logical Design database design. Dr. Mohamed Osman Hegaz2 Conceptual Database Designing –Provides concepts that are close to the way many users perceive

Step 4: Example

Binary I: N relationship types in the Company Database: WORKS_ FOR, CONTROLS and SUPERVISES

PROJECT [Pno, PName, Plocation, Dnum]

Where primary key of the DEPARTMENT relation is included as a foreign key in the PROJECT relation (renamed Dnum)

Page 21: Logical Design database design. Dr. Mohamed Osman Hegaz2 Conceptual Database Designing –Provides concepts that are close to the way many users perceive

Step 4: Example

Binary I: N relationship types in the Company Database: WORKS_ FOR, CONTROLS and SUPERVISES

EMPLOYEE [Ssn, Fname, Mit, Lname,Dob, Address, Sex, Salary, Dno,SuperSsn]Where primary key of the EMPLOYEErelation is included as a foreign key within the EMPLOYEE relation (called SuperSsn)Note the recursive relationship!

Page 22: Logical Design database design. Dr. Mohamed Osman Hegaz2 Conceptual Database Designing –Provides concepts that are close to the way many users perceive

Schema (in progress)

EMPLOYEE [Ssn, Fname, Mit, Lname,Dob, Address, Sex, Salary, Dno, SuperSSN]

• DEPARTMENT [Dnumber, Dname,MGRSSN, MgrStart]

• PROJECT [Pno, PName, Plocation,DNum]

• DEPENDENT [ESSN, DepName, Sex, DOB,Relationship]

Page 23: Logical Design database design. Dr. Mohamed Osman Hegaz2 Conceptual Database Designing –Provides concepts that are close to the way many users perceive

Step 5: Binary M: N Relationship

For each binary M: N relationship type RT, create a new relation S to represent RT– Include as foreign key of S the primary keys of the relations that represent the participating entity types in RT– The combination of foreign keys will form theprimary key of S (Note: cannot represent the M: N using asingle foreign key in one relation because of the M: N cardinality ratio)– Include any simple attributes (or simple components of composite attributes) of the M: N relationship type as attributes of S.

Page 24: Logical Design database design. Dr. Mohamed Osman Hegaz2 Conceptual Database Designing –Provides concepts that are close to the way many users perceive

Step 5: ExampleBinary M: N relationship type in the Company Database: WORKS_ ON

WORKS_ ON [ESSN, PNo, Hours]

Where WORKS_ ON includes the primary keys of the PROJECT and EMPLOYEE relations as foreign keysThe primary key of WORKS_ ON is the combination of the foreign key attributes (renamed to PNO and ESSN respectively)

HOURS in WORKS_ ON represents the attribute of the relationship type

Page 25: Logical Design database design. Dr. Mohamed Osman Hegaz2 Conceptual Database Designing –Provides concepts that are close to the way many users perceive

Schema (in progress)

• EMPLOYEE [Ssn, Fname, Mit, Lname, Dob, Address, Sex, Salary, Dno, SuperSSN]

• DEPARTMENT [Dnumber, Dname, MGRSSN, MgrStart]

• PROJECT [Pno, PName, Plocation, DNum]

• DEPENDENT [ESSN, DepName, Sex, DOB, Relationship]

• WORKS_ ON [ESSN, PNo, Hours]

Page 26: Logical Design database design. Dr. Mohamed Osman Hegaz2 Conceptual Database Designing –Provides concepts that are close to the way many users perceive

Step 6: Multivalued Attributes

For each multi- valued attribute A, create a new relation R that includes an attribute corresponding to A plus the primary key K (as a foreign key of R) of the relation thatrepresents the entity type or relationship type that has A as an attribute

– The primary key of R is the combination of attributes A & K– If the multi- valued attribute is composite, include its simple components

Page 27: Logical Design database design. Dr. Mohamed Osman Hegaz2 Conceptual Database Designing –Provides concepts that are close to the way many users perceive

Step 6: ExampleMulti- valued attributes in the Company Database: Locations

DEPT_ LOCS [DNumber, DLocation]Where primary key of DEPT_ LOCS isthe combination of {DNumber,DLocation}• Attribute DLocation will represent theattributes Locations of DEPARTMENT multi-valued• Attribute DNumber (as foreign key) represents the primary key of the DEPARTMENT relation

Page 28: Logical Design database design. Dr. Mohamed Osman Hegaz2 Conceptual Database Designing –Provides concepts that are close to the way many users perceive

Final Schema

EMPLOYEE [Ssn, Fname, Mit, Lname, Dob, Address, Sex, Salary, Dno, SuperSSN]• DEPARTMENT [Dnumber, Dname, MGRSSN, MgrStart]

• PROJECT [Pno, PName, Plocation, DNum]

• DEPENDENT [ESSN, DepName, Sex, DOB, Relationship]

• WORKS_ ON [ESSN, PNo, Hours]

• DEPT_ LOCS[ DNumber, DLocation]

Page 29: Logical Design database design. Dr. Mohamed Osman Hegaz2 Conceptual Database Designing –Provides concepts that are close to the way many users perceive

Step 7: N- ary Relationship Type

For each “n- ary” relationship type RT, create a new relation S to represent RT.– Include as foreign key attributes of S the primary keys of the relations that represent the participating entity types in RT– Include any simple attributes of the n- ary relationship type– The combination of foreign keys referencing the relations representing the participating entity types is used to form primary key of S

Page 30: Logical Design database design. Dr. Mohamed Osman Hegaz2 Conceptual Database Designing –Provides concepts that are close to the way many users perceive

Ternary relationship example

SUPPLIER [sname,…,]PROJECT [ProjName, ... ]PART [PartNo, ... ]SUPPLY [SName, ProjName , PartNo, Quantity]

Page 31: Logical Design database design. Dr. Mohamed Osman Hegaz2 Conceptual Database Designing –Provides concepts that are close to the way many users perceive

Step 8: Super & Sub- classes

Option 8A– We create a relational table for the superclass andcreate a relational table for each subclass.– The primary key of each of the subclass is the primary key of the superclass.

Page 32: Logical Design database design. Dr. Mohamed Osman Hegaz2 Conceptual Database Designing –Provides concepts that are close to the way many users perceive

Step 8 (cont)Option 8B– We create a relational table for each subclass.The attributes of the superclass are merged into each of the subclasses.– The primary key of the subclass table is the primary key of the superclass.

Page 33: Logical Design database design. Dr. Mohamed Osman Hegaz2 Conceptual Database Designing –Provides concepts that are close to the way many users perceive

Step 8 (cont)Option 8C– We create a single relational table for all subclasses and the superclass.– The attributes of the table is the union of all attributes plus the attribute T to indicate the subclass to which each tuple belongs. T is NULL in tuples that do not belong to anysubclass (for partial constraints)

Page 34: Logical Design database design. Dr. Mohamed Osman Hegaz2 Conceptual Database Designing –Provides concepts that are close to the way many users perceive

Step 8 (cont)

Option 8D– We create a single relational table for all subclasses and the superclass.– The attributes of the table is the union of all attributes plus m extra boolean attributes for each subclass to indicate whether or not the tuple belongs to this subclass.