bordoloi cmis 450: database design dr. bijoy bordoloi transforming e/r diagrams to relations
DESCRIPTION
Bordoloi Represent Entities QuestionQuestion –What happens if you allow (non-identifying) multi-valued attributes in an entity-type? Can the identifier of an entity-type then also be the PK of the corresponding relation?TRANSCRIPT
BordoloiBordoloi
CMIS 450: Database DesignCMIS 450: Database DesignDr. Bijoy BordoloiDr. Bijoy Bordoloi
Transforming E/R Diagrams to Transforming E/R Diagrams to RelationsRelations
BordoloiBordoloi
Represent EntitiesRepresent Entities
• Each entity-type in E/R Diagram becomes a Each entity-type in E/R Diagram becomes a Relation (Table)Relation (Table)
• The identifier of the entity-type becomes the The identifier of the entity-type becomes the PK of the corresponding relation (provided PK of the corresponding relation (provided the PK meets all the necessary criteria the PK meets all the necessary criteria discussed earlier)discussed earlier)
• Each non-key attribute of the entity-type Each non-key attribute of the entity-type becomes a non-key attribute of the relationbecomes a non-key attribute of the relation
BordoloiBordoloi
Represent EntitiesRepresent Entities
• QuestionQuestion– What happens if you allow (non-identifying) What happens if you allow (non-identifying)
multi-valued attributes in an entity-type? Can multi-valued attributes in an entity-type? Can the identifier of an entity-type then also be the the identifier of an entity-type then also be the PK of the corresponding relation? PK of the corresponding relation?
BordoloiBordoloi
Attributes CardinalityAttributes Cardinality
• Describes association between attribute and Describes association between attribute and owner entityowner entity
• Single-valued attribute has at most one value Single-valued attribute has at most one value for each entity instancefor each entity instance
• Multi-valued attribute has many values for Multi-valued attribute has many values for some entity instancessome entity instances
BordoloiBordoloi
Attributes CardinalityAttributes Cardinality
EMPLOYEE
EMP-ID SS-NUM SEX EXTENSION**
BordoloiBordoloi
Multi-valued AttributesMulti-valued Attributes
Either redefine the Primary Key* or make a Either redefine the Primary Key* or make a separate relation with a foreign key taken separate relation with a foreign key taken from the superior entityfrom the superior entity
* Refer to class notes on Relational Model – * Refer to class notes on Relational Model – Primary KeyPrimary Key
BordoloiBordoloi
Multi-valued AttributesMulti-valued Attributes
• Multi-valued attributes become new Multi-valued attributes become new characteristic tablescharacteristic tables
EMPLOYEE
EMP#
E-NAME DATETITLE
M M
N
N
EMPLOYEE HISTORY
EMP# E-NAME
EMP#TITLE DATE
1
11
NN
N
BordoloiBordoloi
Multi-valued AttributesMulti-valued Attributes
• Characteristic table is necessary because relational model is normalizedCharacteristic table is necessary because relational model is normalized
EMP# E-NAME TITLE
HISTORY
DATE EMP# E-NAME
EMPLOYEENOT IN 2NF
MA TABLE
EMP # DATE TITLE
E1 3-15-85 JANITORE1 3-16-85 TECHNICIANE1 3-17-85 MANAGERE2 2-23-87 EMPLOYERE2 4-19-88 DOGCATCHER
BordoloiBordoloi
Transforming RelationshipsTransforming Relationships
Mapping Binary RelationshipsMapping Binary Relationships– One-to-Many – and One-to-One relationships One-to-Many – and One-to-One relationships
become become foreign keys. foreign keys. – Many-to-Many - Create a Many-to-Many - Create a new relationnew relation with the with the
primary keys of the two entities as its primary primary keys of the two entities as its primary keykey
BordoloiBordoloi
Cont’dCont’d
• In N-1 relationship, always place foreign In N-1 relationship, always place foreign key at ‘N’ sidekey at ‘N’ side
• In 1-1 relationship, usually place foreign In 1-1 relationship, usually place foreign key in smaller table (results in fewer null key in smaller table (results in fewer null values)values)
BordoloiBordoloi
Representing a (1:N) RelationshipRepresenting a (1:N) Relationship
CUSTOMER
ORDER
Places
CUSTOMER NO.
NAME CITY STATE ZIP
ADDRESS
DISCOUNT
ORDER DATE
PROMISED DATE
ORDER NO.
a) E-R Diagram
BordoloiBordoloi
Representing a (1:N) RelationshipRepresenting a (1:N) Relationship
CUSTOMER NO. NAME ADDRESS CITY STATE ZIP DISCOUNT1273 Contemporaries 123 Oak St. Austin, TX 38405 5%6390 Casual Corner 18 Hoosier Dr. Bloomington, IN 45821 3%…
CUSTOMER
ORDER NO. ORDER DATE PROMISED DATE CUSTOMER NO.57194 3/15/9X 3/28/9X 639063725 3/17/9X 4/01/9X 127380149 3/14/9X 3/24/9X 6390
b) Relations
BordoloiBordoloi
Mapping a binary 1:1 relationship
(a) Binary 1:1 relationship
BordoloiBordoloi
Figure (b) Resulting relations
BordoloiBordoloi
NULL Values in Foreign KeysNULL Values in Foreign Keys• Whether or not a Foreign Key can have Whether or not a Foreign Key can have
NULL values depends on the minimum NULL values depends on the minimum cardinality of the concerned relationshipcardinality of the concerned relationship
• Minimum cardinality of 0 represented as Minimum cardinality of 0 represented as NULL allowed for foreign key columnsNULL allowed for foreign key columns
• Minimum cardinality of 1 represented as Minimum cardinality of 1 represented as NULL disallowed for foreign key columnsNULL disallowed for foreign key columns
BordoloiBordoloi
Sub-tablesSub-tables• Sub-entity Sub-entity - a - a subsetsubset of another entity, called of another entity, called
the super-entitythe super-entity– Has 1-1 relationship to the super-entity (IS-A)Has 1-1 relationship to the super-entity (IS-A)– Are existence-dependent on the super-entityAre existence-dependent on the super-entity
EMPLOYEE
AGENT
CLERK
PROGRAMMER
BordoloiBordoloi
• Sub-entities become sub-tables in baseline logical design, but may merge with super-table in final design
• Any kind of table may have sub-table
Sub-tablesSub-tables
EMPLOYEE
PROGRAMMER
SYSTEM
ASSIGNMENT
PERMANENT
TEMPORARY
BordoloiBordoloi
Design of sub-tableDesign of sub-table• Foreign key identifies super-tableForeign key identifies super-table
• Primary key-foreign key with no qualifying columnsPrimary key-foreign key with no qualifying columns• Foreign key rules reflect existenceForeign key rules reflect existence
– Nulls not allowedNulls not allowed– Delete cascadesDelete cascades– Update cascadesUpdate cascades
EMPLOYEE
PROGRAMMER
BordoloiBordoloi
Design of sub-tableDesign of sub-tableCREATE TABLECREATE TABLE PROGRAMMER (EMP#, LANGUAGE, LEVEL)PROGRAMMER (EMP#, LANGUAGE, LEVEL)PRIMARY KEYPRIMARY KEY (EMP#)(EMP#)FOREIGN KEYFOREIGN KEY (EMP# IDENTIFIES EMPLOYEE NULLS NOT (EMP# IDENTIFIES EMPLOYEE NULLS NOT
ALLOWED DELETE OF EMPLOYEE ALLOWED DELETE OF EMPLOYEE RESTRICTED RESTRICTED UPDATE OF EMPLOYEE CASCADES)UPDATE OF EMPLOYEE CASCADES)
• Optional class column in super-table improves Optional class column in super-table improves performance, but is redundant to data in sub-tablesperformance, but is redundant to data in sub-tables
BordoloiBordoloi
Representing IS-A RelationshipsRepresenting IS-A Relationships
PROPERTY
MOUNTAIN PROPERTY
BEACH PROPERTY
CITY STATE ZIP
STREET ADDRESS
TYPICAL RENT
NO. ROOMS
STREET ADDRESS
STREET ADDRESS CITY
STATE ZIPCITY
STATE ZIP
SKIING
BLOCKS TO BEACH
(a) E-R diagram
IS-A IS-A
BordoloiBordoloi
Representing IS-A RelationshipsRepresenting IS-A Relationships
STREET ADDRESS CITY STATE ZIP NO. ROOMS TYPICAL UNIT120 Surf Dr. Honolulu, HI 99987 3 500
100 Mogul Dr. Jackson, WY 89204 3 250
PROPERTY
STREET ADDRESS CITY STATE ZIP BLOCKS TO BEACH120 Surf Dr. Honolulu, HI 99987 2
BEACH
STREET ADDRESS CITY STATE ZIP SKIING100 Mogul Dr. Jackson, WY 89204 N
MOUNTAIN
(b) Relations
BordoloiBordoloi
Associative TablesAssociative Tables
• M-N relationshipM-N relationship becomes associative table becomes associative table with two foreign keyswith two foreign keys
BordoloiBordoloi
Associative TablesAssociative Tables
• Associative tables also known as association or Associative tables also known as association or intersection tableintersection table
EMPLOYEE PROJECT
EMP# E-NAME P-NAMEP#
1 NEMPLOYEE PROJECT
EMP# E-NAME P-NAMEP#
ASSIGNMENT
EMP# P#
M N
N 1
EMP# E-NAME EMP# P# P# P-NAME
BordoloiBordoloi
Transforming RelationshipsTransforming Relationships
Mapping Unary RelationshipsMapping Unary Relationships What do you do when the relationship is unary?What do you do when the relationship is unary? Rules are the same irrespective of whether the Rules are the same irrespective of whether the
relationship is unary or binary. 1:N and 1:1 relationship is unary or binary. 1:N and 1:1 relationships be come foreign keys in the relationships be come foreign keys in the samesame table. M:N relationship becomes a separate table. M:N relationship becomes a separate (associative table).(associative table).
BordoloiBordoloi
In-Class Exercise: Transform the In-Class Exercise: Transform the following ERD to a relational structurefollowing ERD to a relational structure
EMPLOYEE
DIVISION
DEPARTMENT
SSN
EMP#
FNAME LNAME SALARY
JOBCODE
MARRIED-TO
BLDGDIVNAME
DIVNAME
DEPT#
DEPTNAME
DIRECT
WORK-IN MANAGE
BELONG-TO
BordoloiBordoloi
Reverse EngineeringReverse Engineering
Modify the previous ERD to Modify the previous ERD to reflect the existence of the reflect the existence of the following five tablesfollowing five tables
BordoloiBordoloi
More Example TablesMore Example Tables
EMP LANGUAGE LEVELE1 PL/1 BEGE6 FORTRAN INTE7 COBOL ADVE8 FORTRAN BEG
PROGRAMMER
EMP TERRITORY COMMISSIONE2 TEXAS 33000E3 NULL NULL
E11 FLORIDA 23050E12 TEXAS 1905
AGENT
PROJ# DESCRIPTION BUDGETP1 XYZ DATABASE 19000P2 GIZMO TEST 48000P3 WIDGET SALES 4000
PROJECT
BordoloiBordoloi
More Example TablesMore Example Tables
EMP# PROJ# HOURSE1 P1 80E1 P3 230E3 P2 NULLE5 P3 1200
ASSIGNMENT
PROJ# TASKNAME START STATUSP1 REQUIREMENTS 10/29/85 COMPLETEP1 LOGICAL DESIGN 3/24/87 ACTIVEP1 PHYSICAL DESIGN 3/29/87 PROPOSEP3 SALES REP NULL PROPOSE
TASK