CSCI 242 Relational Data Modeling Copyright 2011, David C. Roberts, all rights reserved.
CSCI 6442 Entity-Relation Data Modeling Copyright 2012, David C. Roberts, all rights reserved.
-
Upload
bridget-watts -
Category
Documents
-
view
220 -
download
5
Transcript of CSCI 6442 Entity-Relation Data Modeling Copyright 2012, David C. Roberts, all rights reserved.
![Page 1: CSCI 6442 Entity-Relation Data Modeling Copyright 2012, David C. Roberts, all rights reserved.](https://reader035.fdocuments.net/reader035/viewer/2022062515/56649cd95503460f949a2d78/html5/thumbnails/1.jpg)
CSCI 6442Entity-Relation Data Modeling
Copyright 2012, David C. Roberts, all rights reserved
![Page 2: CSCI 6442 Entity-Relation Data Modeling Copyright 2012, David C. Roberts, all rights reserved.](https://reader035.fdocuments.net/reader035/viewer/2022062515/56649cd95503460f949a2d78/html5/thumbnails/2.jpg)
Agenda
•Data Modeling•Natural and Generated Keys
2
![Page 3: CSCI 6442 Entity-Relation Data Modeling Copyright 2012, David C. Roberts, all rights reserved.](https://reader035.fdocuments.net/reader035/viewer/2022062515/56649cd95503460f949a2d78/html5/thumbnails/3.jpg)
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
![Page 4: CSCI 6442 Entity-Relation Data Modeling Copyright 2012, David C. Roberts, all rights reserved.](https://reader035.fdocuments.net/reader035/viewer/2022062515/56649cd95503460f949a2d78/html5/thumbnails/4.jpg)
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
![Page 5: CSCI 6442 Entity-Relation Data Modeling Copyright 2012, David C. Roberts, all rights reserved.](https://reader035.fdocuments.net/reader035/viewer/2022062515/56649cd95503460f949a2d78/html5/thumbnails/5.jpg)
Notation for Data Models• Because a relational database is comprised of
independent tables, how do you deal with relationships during design?
• Chen developed the entity-relation data model, which considered entity types and their relationships
• 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
![Page 6: CSCI 6442 Entity-Relation Data Modeling Copyright 2012, David C. Roberts, all rights reserved.](https://reader035.fdocuments.net/reader035/viewer/2022062515/56649cd95503460f949a2d78/html5/thumbnails/6.jpg)
Chen’s Notation• We will use Chen’s notation here for two
reasons:1. It separates the notion of a relationship from
the notion of an entity type2. 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
![Page 7: CSCI 6442 Entity-Relation Data Modeling Copyright 2012, David C. Roberts, all rights reserved.](https://reader035.fdocuments.net/reader035/viewer/2022062515/56649cd95503460f949a2d78/html5/thumbnails/7.jpg)
Entities
•Strong Entity—has independent existence
•Weak Entity—exists only when the entity it depends on exists
7
Employee
Dependent
Question: What are someExamples of strong entitytypes? Weak entity types?
![Page 8: CSCI 6442 Entity-Relation Data Modeling Copyright 2012, David C. Roberts, all rights reserved.](https://reader035.fdocuments.net/reader035/viewer/2022062515/56649cd95503460f949a2d78/html5/thumbnails/8.jpg)
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
8
![Page 9: CSCI 6442 Entity-Relation Data Modeling Copyright 2012, David C. Roberts, all rights reserved.](https://reader035.fdocuments.net/reader035/viewer/2022062515/56649cd95503460f949a2d78/html5/thumbnails/9.jpg)
Attributes
•Attributes are indicated inside ovals; identifier attributes are underlined. For complex diagrams, attributes are often omitted
9
Employee
EMPNO
JOB
SALARYQuestion: Which attributes in this diagram should be underlined?
![Page 10: CSCI 6442 Entity-Relation Data Modeling Copyright 2012, David C. Roberts, all rights reserved.](https://reader035.fdocuments.net/reader035/viewer/2022062515/56649cd95503460f949a2d78/html5/thumbnails/10.jpg)
Examples of AttributesAn attribute is a characteristic of an entity type:EmployeeIDSocial Security NumberFirst NameLast NameStreet AddressCityStateZipCodeDate Hired Health Benefits Plan
10
![Page 11: CSCI 6442 Entity-Relation Data Modeling Copyright 2012, David C. Roberts, all rights reserved.](https://reader035.fdocuments.net/reader035/viewer/2022062515/56649cd95503460f949a2d78/html5/thumbnails/11.jpg)
Multi-Valued Attribute
•A multi-valued attribute is shown as a double line
11
Employee
EMPNO
JOB
SALARY
TELEPHONE
Question: Which attribute should have a double underline?
![Page 12: CSCI 6442 Entity-Relation Data Modeling Copyright 2012, David C. Roberts, all rights reserved.](https://reader035.fdocuments.net/reader035/viewer/2022062515/56649cd95503460f949a2d78/html5/thumbnails/12.jpg)
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
12
![Page 13: CSCI 6442 Entity-Relation Data Modeling Copyright 2012, David C. Roberts, all rights reserved.](https://reader035.fdocuments.net/reader035/viewer/2022062515/56649cd95503460f949a2d78/html5/thumbnails/13.jpg)
Relationship
13
EMPLOYEE
DEPARTMENT
WORKS FOR
![Page 14: CSCI 6442 Entity-Relation Data Modeling Copyright 2012, David C. Roberts, all rights reserved.](https://reader035.fdocuments.net/reader035/viewer/2022062515/56649cd95503460f949a2d78/html5/thumbnails/14.jpg)
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
14
![Page 15: CSCI 6442 Entity-Relation Data Modeling Copyright 2012, David C. Roberts, all rights reserved.](https://reader035.fdocuments.net/reader035/viewer/2022062515/56649cd95503460f949a2d78/html5/thumbnails/15.jpg)
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.
15
EMPLOYEE
SPOUSE
HAS
1
1
![Page 16: CSCI 6442 Entity-Relation Data Modeling Copyright 2012, David C. Roberts, all rights reserved.](https://reader035.fdocuments.net/reader035/viewer/2022062515/56649cd95503460f949a2d78/html5/thumbnails/16.jpg)
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
16
DEPARTMENT
EMPLOYEE
HAS
STUDENT
COURSE
GRADE
EMPLOYEE
COMPANY CAR
HAS
1
N N
N 1
1
![Page 17: CSCI 6442 Entity-Relation Data Modeling Copyright 2012, David C. Roberts, all rights reserved.](https://reader035.fdocuments.net/reader035/viewer/2022062515/56649cd95503460f949a2d78/html5/thumbnails/17.jpg)
Recursive Relationship
•Recursive relationship is how a repeated hierarchy is represented
17
Employee WORKS FOR
Question: How else can this hierarchy be represented?
![Page 18: CSCI 6442 Entity-Relation Data Modeling Copyright 2012, David C. Roberts, all rights reserved.](https://reader035.fdocuments.net/reader035/viewer/2022062515/56649cd95503460f949a2d78/html5/thumbnails/18.jpg)
Relationship of Higher Degree• A ternary relationship, also said to have degree
3.
18
STUDENT
COURSE
REG
N
N
TEACHERN
COURSENO
GRADE
![Page 19: CSCI 6442 Entity-Relation Data Modeling Copyright 2012, David C. Roberts, all rights reserved.](https://reader035.fdocuments.net/reader035/viewer/2022062515/56649cd95503460f949a2d78/html5/thumbnails/19.jpg)
Optionality of Participation
19
EMPLOYEE
HAS
STUDENT
COURSE
GRADE
EMPLOYEE
COMPANY CAR
HAS
1
N N
N 1
1
DEPARTMENT
![Page 20: CSCI 6442 Entity-Relation Data Modeling Copyright 2012, David C. Roberts, all rights reserved.](https://reader035.fdocuments.net/reader035/viewer/2022062515/56649cd95503460f949a2d78/html5/thumbnails/20.jpg)
Supertypes
•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
20
![Page 21: CSCI 6442 Entity-Relation Data Modeling Copyright 2012, David C. Roberts, all rights reserved.](https://reader035.fdocuments.net/reader035/viewer/2022062515/56649cd95503460f949a2d78/html5/thumbnails/21.jpg)
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
21
![Page 22: CSCI 6442 Entity-Relation Data Modeling Copyright 2012, David C. Roberts, all rights reserved.](https://reader035.fdocuments.net/reader035/viewer/2022062515/56649cd95503460f949a2d78/html5/thumbnails/22.jpg)
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.
22
![Page 23: CSCI 6442 Entity-Relation Data Modeling Copyright 2012, David C. Roberts, all rights reserved.](https://reader035.fdocuments.net/reader035/viewer/2022062515/56649cd95503460f949a2d78/html5/thumbnails/23.jpg)
Example Subtypes
23
PARTY
EMPLOYEE
APPLICANT
PERSON
ORGANIZATION
SUPPLIER
CUSTOMER
Question: How would this be implemented as database tables?PARTY(PARTYID)
PERSON(PARTYID,FNAME,MI,LNAME,DOB,POB)EMP(PARTYID,EMPID,DHIRED,SAL,JOB,DEPTNO)APP(PARTYID,DAPPLIED,STATUS)ORG(PARTYID,ORGNAME,ORGCITY,ORGSTATE,ORGZIP)CUST(PARTYID,CUSTID,DOFO,DOLO,LYVOL)SUPP(PARTYID,SUPPID,DOFO,DOLO,LYVOL)
![Page 24: CSCI 6442 Entity-Relation Data Modeling Copyright 2012, David C. Roberts, all rights reserved.](https://reader035.fdocuments.net/reader035/viewer/2022062515/56649cd95503460f949a2d78/html5/thumbnails/24.jpg)
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
24
![Page 25: CSCI 6442 Entity-Relation Data Modeling Copyright 2012, David C. Roberts, all rights reserved.](https://reader035.fdocuments.net/reader035/viewer/2022062515/56649cd95503460f949a2d78/html5/thumbnails/25.jpg)
Relationships
25
DEPT WORKS IN
BELONGS TO
![Page 26: CSCI 6442 Entity-Relation Data Modeling Copyright 2012, David C. Roberts, all rights reserved.](https://reader035.fdocuments.net/reader035/viewer/2022062515/56649cd95503460f949a2d78/html5/thumbnails/26.jpg)
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
26
Question: What are the symbols on the ERD that will become tables in the relational database?
![Page 27: CSCI 6442 Entity-Relation Data Modeling Copyright 2012, David C. Roberts, all rights reserved.](https://reader035.fdocuments.net/reader035/viewer/2022062515/56649cd95503460f949a2d78/html5/thumbnails/27.jpg)
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
27
![Page 28: CSCI 6442 Entity-Relation Data Modeling Copyright 2012, David C. Roberts, all rights reserved.](https://reader035.fdocuments.net/reader035/viewer/2022062515/56649cd95503460f949a2d78/html5/thumbnails/28.jpg)
Conversion
•We see that every rectangle in the ERD will become a table in the relational database
•Some of the diamonds will become tables and some will not
28
![Page 29: CSCI 6442 Entity-Relation Data Modeling Copyright 2012, David C. Roberts, all rights reserved.](https://reader035.fdocuments.net/reader035/viewer/2022062515/56649cd95503460f949a2d78/html5/thumbnails/29.jpg)
Students and Courses
29
Course
NumberLocation
Credits
Registration
Grade
StudentIDName
Student
Grad YR
![Page 30: CSCI 6442 Entity-Relation Data Modeling Copyright 2012, David C. Roberts, all rights reserved.](https://reader035.fdocuments.net/reader035/viewer/2022062515/56649cd95503460f949a2d78/html5/thumbnails/30.jpg)
What About Subtypes?
•Each subtype and supertype is a separate table
•Common attributes are stored with the highest level entity that shares them
•The root level supertype may have many attributes, or it may have only a key
30
![Page 31: CSCI 6442 Entity-Relation Data Modeling Copyright 2012, David C. Roberts, all rights reserved.](https://reader035.fdocuments.net/reader035/viewer/2022062515/56649cd95503460f949a2d78/html5/thumbnails/31.jpg)
OK, How Do I Do It?You will have your own ideas about how to proceed, but here’s an idea:1. Enumerate things in the problem space to keep
track of (these are entity types)2. Enumerate what to keep track of about each
thing (these are attributes)3. Enumerate relationships to keep track of4. Draw a draft ERD and review it for difficulty of
doing the operations that are needed5. Review it for possible simplifications and
alternative approaches
31
![Page 32: CSCI 6442 Entity-Relation Data Modeling Copyright 2012, David C. Roberts, all rights reserved.](https://reader035.fdocuments.net/reader035/viewer/2022062515/56649cd95503460f949a2d78/html5/thumbnails/32.jpg)
Here’s Another Approach• List all the facts you want to keep track of and their
subjects• Group your list by subject• Consider each subject as an entity type, the facts
about it as attributes• Be sure to use separate entity types for repeated
groups of facts• Now draw your draft ERD and review it for
difficulty of programming the operations that are needed
• Review it for simplifications and alternate approaches
32
![Page 33: CSCI 6442 Entity-Relation Data Modeling Copyright 2012, David C. Roberts, all rights reserved.](https://reader035.fdocuments.net/reader035/viewer/2022062515/56649cd95503460f949a2d78/html5/thumbnails/33.jpg)
Natural and Generated Keys
![Page 34: CSCI 6442 Entity-Relation Data Modeling Copyright 2012, David C. Roberts, all rights reserved.](https://reader035.fdocuments.net/reader035/viewer/2022062515/56649cd95503460f949a2d78/html5/thumbnails/34.jpg)
Definitions
Natural key—a key that occurs in the data, that uniquely identifies rows. AKA candidate key.
Generated key—a key that is generated at the time data is inserted into the database, that did not occur naturally
34
![Page 35: CSCI 6442 Entity-Relation Data Modeling Copyright 2012, David C. Roberts, all rights reserved.](https://reader035.fdocuments.net/reader035/viewer/2022062515/56649cd95503460f949a2d78/html5/thumbnails/35.jpg)
Postulate:In a normalized data model, every row has a natural
key
Proof: 3NF requires that the value of every attribute be ffd by the primary key. By construction, primary keys uniquely identify instances and are unique. Therefore, each row has a different value for the primary key and is hence unique.
Result: There is no reason to add a primary key to normalized data in order to make rows unique. They already are.
35
Question: does 3NF require that there be a single-attribute primary key?
![Page 36: CSCI 6442 Entity-Relation Data Modeling Copyright 2012, David C. Roberts, all rights reserved.](https://reader035.fdocuments.net/reader035/viewer/2022062515/56649cd95503460f949a2d78/html5/thumbnails/36.jpg)
Generated KeysIn some organizations, programmers have the idea that a
generated key should be included in every relation, a misunderstanding of the relational approach. Often they will say “it’s to make rows unique” or “to provide a unique identifier.” This is terribly wrong!
That’s a fine idea for an Excel spreadsheet but it shows a misunderstanding of what a relation is and what a tuple is.
Tuples are naturally unique because each one corresponds to a distinct instance of an entity type in the real world. You don’t have to add anything to make them unique.
If your database design is correct, you have unique rows even if you haven’t added a key to each row.
36
![Page 37: CSCI 6442 Entity-Relation Data Modeling Copyright 2012, David C. Roberts, all rights reserved.](https://reader035.fdocuments.net/reader035/viewer/2022062515/56649cd95503460f949a2d78/html5/thumbnails/37.jpg)
Making the Point Again, Again
•Each row corresponds to an occurrence of a entity instance
•Each entity instance is unique; that’s why it is tracked as an instance
•Therefore, every row is different from every other row without adding anything to it
37
Question: what if entity instances are not unique? How do we track them in a database?
![Page 38: CSCI 6442 Entity-Relation Data Modeling Copyright 2012, David C. Roberts, all rights reserved.](https://reader035.fdocuments.net/reader035/viewer/2022062515/56649cd95503460f949a2d78/html5/thumbnails/38.jpg)
Claim That’s Made for Generated Keys
• Some think that generated keys allow more flexibility• For example, in our EMP table, can we allow a
person’s name to change?• If we use a generated key, then the name can change
and all other associated data will still be associated with the correct person
• What do you think of this statement?• Can you use telephone number as the primary key?
Why or why not?• Can primary keys ever change in value?• And what if the primary key is a foreign key in
another table?
38
![Page 39: CSCI 6442 Entity-Relation Data Modeling Copyright 2012, David C. Roberts, all rights reserved.](https://reader035.fdocuments.net/reader035/viewer/2022062515/56649cd95503460f949a2d78/html5/thumbnails/39.jpg)
SQL and Primary Keys
•SQL allows primary keys to change; they are just a value that changes
•What about foreign keys? Can they change?
•In the CREATE TABLE statement, you state ON UPDATE or ON DELETE CASCADE, RESTRICT, NO ACTION, SET NULL or SET DEFAULT. For ON DELETE, DELETE can also be an action
39
![Page 40: CSCI 6442 Entity-Relation Data Modeling Copyright 2012, David C. Roberts, all rights reserved.](https://reader035.fdocuments.net/reader035/viewer/2022062515/56649cd95503460f949a2d78/html5/thumbnails/40.jpg)
Limitation of SQL Database Systems
•In general, the primary key is not allowed to have null value—which is fine
•But a composite primary key is not allowed to have any part that is null
•The composite primary key limitation does not correspond to reality, can force us to use a generated key
40
![Page 41: CSCI 6442 Entity-Relation Data Modeling Copyright 2012, David C. Roberts, all rights reserved.](https://reader035.fdocuments.net/reader035/viewer/2022062515/56649cd95503460f949a2d78/html5/thumbnails/41.jpg)
Benefits of Natural Keys:
•Assures proper attention to the data model, because unique values must be identified
•Tables have fewer columns•Avoid storage of meaningless data•The key value itself may have value to an
application, hence will avoid retrieval of the row itself
41
![Page 42: CSCI 6442 Entity-Relation Data Modeling Copyright 2012, David C. Roberts, all rights reserved.](https://reader035.fdocuments.net/reader035/viewer/2022062515/56649cd95503460f949a2d78/html5/thumbnails/42.jpg)
When to Use Generated Keys• Sometimes you have a compound key that is very
long• Lots of queries will be very complicated and tables
with foreign keys will copy most of another table• As a guideline, the only time to use a generated
key is when the natural key has at least three columns
• A special case of this rule is the case where the whole row would have to be a composite key without a generated key
• Example: internal person
42
![Page 43: CSCI 6442 Entity-Relation Data Modeling Copyright 2012, David C. Roberts, all rights reserved.](https://reader035.fdocuments.net/reader035/viewer/2022062515/56649cd95503460f949a2d78/html5/thumbnails/43.jpg)
Another Case for Generated Keys•Suppose we don’t have any kind of single
or composite identifier that is always present
•Database systems generally don’t allow for part of a designated composite key to be null
•Even though it’s legitimate, because of database system limitations, you may have to use generated keys if you don’t have a common identifier without any null parts, ever
43
![Page 44: CSCI 6442 Entity-Relation Data Modeling Copyright 2012, David C. Roberts, all rights reserved.](https://reader035.fdocuments.net/reader035/viewer/2022062515/56649cd95503460f949a2d78/html5/thumbnails/44.jpg)
Example
•ID Card for entry to GWU buildings•Students may not have SSN but all have
student number•Contractors may not have GWU employee
# but all have SSN
44
![Page 45: CSCI 6442 Entity-Relation Data Modeling Copyright 2012, David C. Roberts, all rights reserved.](https://reader035.fdocuments.net/reader035/viewer/2022062515/56649cd95503460f949a2d78/html5/thumbnails/45.jpg)
The Bottom Line
•Don’t use generated keys. Ever.•Unless you must!
45