Upload
martha-williams
View
214
Download
0
Tags:
Embed Size (px)
Citation preview
2
Relational Database
Remember that a relational database is a collection of time-varying, independent relations
Each relation models some entity type in the problem space (outside the computer)
There is a close correspondence between entity types and relations
3
Data Modeling
• We can say that the relational database models the real-world problem
• So construction of a relational database becomes the selection of the entity types to put into the data model
• Sometimes entity type selection is simple; other times entity types are not apparent and selection can be difficult
4
Notation for Data Models
• Chen developed the entity-relation data model, which considered entity types and their relationships
• Originally he expected that a new type of database system would evolve, directly implementing the E-R data model
• Instead, the E-R notation was found to be an excellent tool for relational database design and the relational approach has taken over
• Since then, popular automated tools have tended to change the notation for easier computer printing
5
Chen’s Notation
• We will use Chen’s notation today for two reasons:
1. It separates the notion of a relationship from the notion of an entity type
2. Conversion of the design into relational tables occurs at the end of the design process, so the difference between the data model and physical design is clear
• In your own work, once you are familiar with the concepts, you will be able to use the popular “crow’s foot” notation used by DBDesigner4
6
Entities
• Strong Entity—has independent existence
• Weak Entity—exists only when the entity it depends on exists
Employee
Dependent
7
Examples of Entity Types
EMPLOYEECUSTOMERORGANIZATIONPARTINGREDIENTPURCHASE ORDERCUSTOMER ORDERPRODUCT
8
Examples of Entity Instances
An instance of an entity is a specific occurrence of an entity type:
• Bill Gates is an Employee of Microsoft• Spam is a Product• Greenpeace is an Organization• Flour is an ingredient
9
Attributes
• Attributes are indicated inside ovals; identifier attributes are underlined. For complex diagrams, attributes are often omitted
Employee
EMPNO
JOB
SALARY
10
Examples of Attributes
An attribute is a characteristic of an entity type:EmployeeIDSocial Security NumberFirst NameLast NameStreet AddressCityStateZipCodeDate Hired Health Benefits Plan
12
Relationship
A relationship is an association between two entity types, for example:– A CUSTOMER places a CUSTOMER ORDER
– An EMPLOYEE takes a CUSTOMER ORDER– A STUDENT enrolls in a COURSE– A COURSE is taught by a FACULTY
MEMBER• Some say that entity types should be nouns and
relationships should be verbs
14
Categorizing Relationships
• Number of entity types participating: – Unary: one– Binary: two– Ternary: three
• Existence of related instances(a.k.a. optionality):– Mandatory– Optional
• Cardinality of the relationship:– One-many– Many-many– One-one
15
One-to-One Relationships
• Usually the only time a one-to-one relationship is used is for a dependent entity. Otherwise, usually if there is a one-to-one relationship, careful consideration will show that there is just one entity type.
EMPLOYEE
SPOUSE
HAS
1
1
16
Relationship And Cardinality
• A relationship is an association between two or more entity types, drawn as a diamond. Relationships may be one-to-many, many-to-many or one-to-one
DEPARTMENT
EMPLOYEE
HAS
STUDENT
COURSE
GRADE
EMPLOYEE
COMPANY CAR
HAS
1
N N
N 1
1
17
Recursive Relationship
• Recursive relationship is how a repeated hierarchy is represented
Employee WORKS FOR
Question: How else can this hierarchy be represented?
18
Relationship of Higher Degree
• A ternary relationship, also said to have degree 3.
STUDENT
COURSE
REG
N
N
TEACHERN
COURSENO
GRADE
19
Optionality of Participation
EMPLOYEE
HAS
STUDENT
COURSE
GRADE
EMPLOYEE
COMPANY CAR
HAS
1
N N
N 1
1
DEPARTMENT
20
Subtypes
• It can be useful to consider a supertype that includes several entity types as subtypes
• Supertypes can be– Complete: every instance of the supertype is
one of the subtypes– Distinct: no single entity type can be a
member of two subtypes
21
Notation
• Chen’s notation for supertypes and subtypes is cumbersome, so let’s use “crow’s-foot” notation
• A supertype is shown as a box surrounding the boxes for all the subtypes
• Relationship lines end on the supertype or the subtype boundaries as appropriate
22
Subtypes
• Subtypes can be either mutually exclusive (disjoint) or overlapping (inclusive). – For a mutually exclusive category, an entity
instance may be in only one subtype. – For an overlapping category, an entity
instance may be in two or more subtypes. • The completeness constraint: all
instances of a subtype must be represented in the supertype.
24
Why Use Subtypes and Supertypes Like This?
• Relationships are simplified– The relationship can take place at the highest
possible level in the hierarchy– Fewer relationship tables are generally
required• Programming can be simplified
– All subtypes can be processed in the same way
– For example, write a check to an organization or a check to a supplier with the same code
25
Converting an ERD to Relational
• Recall that when we did an ERD we were not designing tables, we were defining the problem in terms of entity types and relationships
• Now the design must be translated into relational tables
Question: What are the symbols on the ERD that will become tables in the relational database?
26
Converting to Relational
• Every entity type becomes a table• For one-many relationships, put the
identifier of the one with each of the many• For one-one relationships, put the
identifier of each with the other• For many-many relationships, identify an
entity type that connects them, and make it a table, using the primary key of both partners as a composite primary key
Conversion
• We see that every rectangle will become a table in the relational database
• Some of the diamonds will become tables and some will not
27
Students and Courses
28
Course
NumberLocation
Credits
Registration
Grade
StudentIDName
Student
Grad YR