lecture 4 (1).ppt
TRANSCRIPT
1
Database Design - Structure
Conceptual model
Logical model
Physical model
- General (not attached to any model)- Not normalized- e.g. ER diagram, still has M:N relationships, composite & multivalued attributes
- Specific to data model, e.g. relational data model- Normalized- e.g. ER diagram, only has 1:1 & 1:M relationships- normalized relational schema
- Specific to DBMS- e.g. Oracle, CREATE TABLE statement
2
Revisit the ERD
• Look for M:N relationships – change the diamond into rectangle symbol. Create a new relationship from entity1 to the newly created entity with 1:M connectivity & from the newly created entity to entity2 with M:1 connectivity.
• Look for multivalued attributes – create a new entity for the attribute. Create a key attribute for the new entity. Link the original entity with the newly created entity with 1:M connectivity if each value is unique to an instance, otherwise M:N
• Look for composite attributes – if each sub-attribute’s value is unique for each instance, then include each sub-attribute in the entity. Otherwise, create a new entity, and link the new entity to the original with connectivity M:1.
Mapping of ER Diagram to Relational Database
Schema
4
Lesson Outcomes
By the end of the lesson, the students should be able to:• Explain some of the terminologies used in relational
database• Map an ER diagram to relational database schema.
5
Relational Database is a collection of normalized relations with distinct relation names.
• A relation is a table with columns and rows.
• Normalized is defined as “conform to a norm or standard” – Merriam-Webster dict. In database, it means no unnecessary redundancies.
• An attribute is a named column/field of a relation.
• Domain is the set of allowable values for one or more attributes.
Relational Database Terminologies
6
Unnormalized vs Normalized Relations
StudentID Name ProgCode Program Year
16227 Ali ICT Info & Comm. Technology 1
15113 Chong BIS Business Information Systems 2
15661 Peter ICT Info & Comm. Technology 2
16227 Ali BIS Business Information Systems 1
14355 Elaine EE Electrical & Electronic Engineering 3
14111 Kumar BIS Business Information Systems 3
StudentID Name ProgCode Year16227 Ali ICT 115113 Chong BIS 215661 Peter ICT 216227 Ali BIS 114355 Elaine EE 314111 Kumar BIS 3
ProgCode Program
ICT Info & Comm. Technology
BIS Business Information Systems
EE Electrical & Electronic Engineering
Unnormalized relation
Normalized relations
7
• Tuple is a row/record of a relation.
• Degree is the number of attributes in a relation.
• Cardinality is the number of tuples in a relation.
© Pearson Education Limited 1995, 2005
Relational Database Terminologies
8
Relational Database
tuple
9
What is database schema?
• Database schema is the structure of the database when presented according to the database model chosen.
• For relational database, each entity in an ERD will become a relation.
• Each relation will have its relation schema• A relation schema shows the name of the relation along with
all of its attributes which include:– Relation name– Attribute names– Primary key– Foreign key – an attribute or set of attributes, within one relation that
matches primary key of some (possibly same) relation.
Properties of Relations
10
• Relation name is distinct from all other relation names in relational schema.
• Each cell of relation contains exactly one atomic (single) value.
• Each attribute has a distinct name.
• Values of an attribute are all from the same domain.
• Each tuple is distinct; there are no duplicate tuples.
• Order of attributes and tuples in a relation has no significance.
© Pearson Education Limited 1995, 2005
11
Steps to map ERD to relational database schema
• Begin with an entity which has 1 connectivity on its side.
• Write : Entity_name, (, attributes, ).
• Underline the primary key
• Continue with other entities (1 on their side)
ENT1 (attr1a, attr1b)
ENT1 (attr1a, attr1b)
ENT1
attr1a
rel11
attr1b
m
12
Cont’d
• Next, select an entity with M on its side (in M:1 relationship)
• Write: Entity_name, (, attributes, the key of the 1 side, )
• Underline the primary key
• Circle the foreign key
• Continue with similar entities
ENT1
attr1a
rel11
ENT2 (attr2a, attr2b, attr1a)
attr1b
ENT2
attr2a
attr2b
m
ENT2 (attr2a, attr2b, attr1a)
ENT2 (attr2a, attr2b, attr1a)
13
Cont’d
• Next, select the M:N relationship• Write: a relation name to represent the
relationship, (, p.k. of entity1, p.k. of entity2, attributes if any, )
• Underline the two keys, i.e., primary/composite key
• Circle each item of the composite key for foreign key
• Continue with similar relationships
ENT2
attr2a
rel2m
attr2b
ENT3
attr3a
attr3b
n
attr23aENT23 (attr2a, attr3a, attr23a)
ENT23 (attr2a, attr3a, attr23a)
ENT23 (attr2a, attr3a, attr23a)
ENT3 (attr3a, attr3b)
ENT2 (attr2a, attr2b)
14
Cont’d - extra• If any of the entity has multivalued
attribute, then:
– create a new relation, create a key attribute and put the multivalued attribute in the new relation.
– Option 1: if each tuple of the new relation is only for one tuple of the original relation (1:1 or M:1), then
• delete the multivalued attribute from the original relation
• add the key of the original relation into the new relation. Circle the attribute for foreign key.
ENT1
attr1a
rel11
attr1b
m ENT1b(attr1ba, attr1b)
ENT1(attr1a)
ENT1b(attr1ba, attr1b, attr1a)
15
Cont’d - extra
– Option 2: if each tuple of the new relation might be linked to many tuples of the original relation, vice-versa (M:N), then
• delete the multivalued attribute from the original relation.
• create another new relation. Fill it up with the keys from both the original relation and the created relation earlier.
ENT11b(attr1a, attr1ba)
ENT1(attr1a)
16
Cont’d - extra
• If any of the entity has composite attribute, then:
– create a new relation, create a key attribute and list out all sub-attributes in the new relation.
– Replace the composite attribute of the original relation with the key of the new relation. Circle the attribute for foreign key
ENT1
attr1a
rel11
attr1b
m
attr1b1 attr1b2
ENT1b(attr1ba, attr1b1, attr1b2)
ENT1(attr1a, attr1ba)
17
Lesson Summary
• Relational database schema represents the ERD in the relational data model
• Each entity will create a relation schema, so is the M:N relationship, multivalued attribute and composite attribute.