Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 405G Introduction to Database Systems Lecture 3: ER Model Instructor: Chen Qian.

Similar presentations


Presentation on theme: "CS 405G Introduction to Database Systems Lecture 3: ER Model Instructor: Chen Qian."— Presentation transcript:

1 CS 405G Introduction to Database Systems Lecture 3: ER Model Instructor: Chen Qian

2 6/11/20162 Review A database is a large collection of integrated data A miniworld is some aspect of the real word, described by facts (data) A data model is a group of concepts for describing data. Which one(s) is a data model ER model Relation model Objected oriented programming model Chen Qian Univ. of Kentucky

3 6/11/20163 Today’s Outline Database design ER Model ER Diagrams – Notation Database schema Case studies Chen Qian Univ. of Kentucky

4 6/11/20164 Database Design Requirement Analysis: Understand the mini-world being modeled Conceptual Database Design: Specify it using a database design model A few popular ones are: Entity/Relationship (E/R) model (in this course) UML (Unified Modeling Language) Intuitive and convenient Logical Design: Translate specification to the data model of DBMS Relational, XML, object-oriented, etc. Chen Qian Univ. of Kentucky

5 6/11/20165 Database Design Chen Qian Univ. of Kentucky

6 6/11/20166 An Database Design Example The company is organized into DEPARTMENTs. Each department has a name, number and an employee who manages the department. We keep track of the start date of the department manager. Each department controls a number of PROJECTs. Each project has a name, number and is located at a single location. We store each EMPLOYEE’s social security number, address, salary, sex, and birthdate. Each employee works for one department but may work on several projects. We keep track of the number of hours per week that an employee currently works on each project. We also keep track of the direct supervisor of each employee. Each employee may have a number of DEPENDENTs. For each dependent, we keep track of their name, sex, birthdate, and relationship to employee. Chen Qian Univ. of Kentucky

7 6/11/2016Luke Huan Univ. of Kansas7

8 6/11/20168 Entity-relationship (E/R) model Historically and still very popular Can think of as a “watered-down” object-oriented design model Primarily a design model—not directly implemented by DBMS Designs represented by E/R diagrams there are other styles Chen Qian Univ. of Kentucky

9 6/11/20169 Entities and Attributes Entity: A specific object or “thing” in the mini-world that is represented in the database. For example, the EMPLOYEE John Smith, the Research DEPARTMENT, the iPhone8 PROJECT. Attributes : properties used to describe an entity. For example, an EMPLOYEE entity may have a Name, SSN, Address, Sex, BirthDate A specific entity will have a value for each of its attributes. For example, a specific employee entity may have Name='John Smith', SSN='123456789', Address ='731 Fondren, Houston, TX', Sex='M', BirthDate='09-JAN-55' Chen Qian Univ. of Kentucky

10 6/11/201610 Types of Attributes Simple vs. Composite Attributes Simple: Each entity has a single atomic value for the attribute. For example, SSN or Sex. Composite: The attribute may be composed of several components. For example, Name (FirstName, MiddleName, LastName). Chen Qian Univ. of Kentucky

11 6/11/201611 Types of Attributes (cont.) Single-valued vs. Multi-valued. Single-valued: an entity may have at most one value for the attribute Multi-valued: An entity may have multiple values for that attribute. For example, PreviousDegrees of a STUDENT. {PreviousDegrees}. NULL values What if the student does not hold a previous degree? What if the student has a previous degree but the information is not provided? Apartment number in an address Chen Qian Univ. of Kentucky

12 6/11/201612 Types of Attributes (cont.) Stored vs. derived Number of credit hours a student took in a semester GPA of a student in a semester Chen Qian Univ. of Kentucky

13 6/11/201613 Key Attributes Entities with the same basic attributes are grouped or typed into an entity type (or entity set). For example, the EMPLOYEE entity type or the PROJECT entity type. An attribute of an entity type for which each entity must have a unique value is called a key attribute (or key, in short) of the entity type. For example, SSN of EMPLOYEE. A key attribute may be composite. An entity type may have more than one key. Chen Qian Univ. of Kentucky

14 6/11/201614 SUMMARY OF ER-DIAGRAM NOTATION Meaning ENTITY TYPE ATTRIBUTE KEY ATTRIBUTE MULTIVALUED ATTRIBUTE COMPOSITE ATTRIBUTE DERIVED ATTRIBUTE Symbol Chen Qian Univ. of Kentucky

15 6/11/201615 Summary (cont.) Chen Qian Univ. of Kentucky

16 6/11/201616 Summary (cont.) Chen Qian Univ. of Kentucky John Smith is an entity. EMPLOYEE is an entity type. SSN is a key attribute. The SSN of John, 123-45-6789, is a value of the attribute

17 6/11/201617 Relationships A relationship relates two or more distinct entities with a specific meaning. For example, EMPLOYEE John Smith works on the iPhone8 PROJECT or EMPLOYEE Franklin Wong manages the Research DEPARTMENT. Relationships of the same type are grouped or typed into a relationship type (or relationship set). For example, the WORKS_ON relationship type in which EMPLOYEEs and PROJECTs participate, or the MANAGES relationship type in which EMPLOYEEs and DEPARTMENTs participate. Chen Qian Univ. of Kentucky

18 6/11/201618 Relationships The degree of a relationship type is the number of participating entity types. Both MANAGES and WORKS_ON are binary relationships. Name a 3-degree relationship? John works on the iPhone8 project since 2010. Chen Qian Univ. of Kentucky

19 6/11/201619 Instances of a relationship: a set of relationships e 1 e 2 e 3 e 4 e 5 e 6 e 7 EMPLOYEEWORKS_FOR d 1 d 2 d 3 DEPARTMENT Chen Qian Univ. of Kentucky r1r2r3r4r5r6r7r1r2r3r4r5r6r7

20 6/11/201620 Summary Chen Qian Univ. of Kentucky John Smith work on the iPhone8 project is a relationship. WORKS_ON is an relationship type. The set of WORKS_ON relationships of all current employees and departments is an instance of WORKS_ON

21 6/11/201621 Key constraints One-to-one (1:1) MANAGE One-to-many (1:N) or Many-to-one (N:1) DEPENDENT_OF Many-to-many WORKS_ON Chen Qian Univ. of Kentucky

22 6/11/201622 Many-to-one (N:1) RELATIONSHIP e 1 e 2 e 3 e 4 e 5 e 6 e 7 EMPLOYEE r1r2r3r4r5r6r7r1r2r3r4r5r6r7 WORKS_FOR d 1 d 2 d 3 DEPARTMENT Chen Qian Univ. of Kentucky

23 6/11/201623 Many-to-many (M:N) RELATIONSHIP e 1 e 2 e 3 e 4 e 5 e 6 e 7 r1r2r3r4r5r6r7r1r2r3r4r5r6r7 p 1 p 2 p 3 r8r8 r9r9 Chen Qian Univ. of Kentucky EMPLOYEEWORKS_ONPROJECT

24 6/11/201624 More Examples Each student may have exactly one account. Each faculty may teach many courses Each student may enroll many courses Students Enroll Courses TaughtBy InstructorsStudents Own UK Accounts Chen Qian Univ. of Kentucky

25 6/11/201625 The (min,max) notation relationship constraints (1,1) (0,1) (1,N) (1,1) Chen Qian Univ. of Kentucky

26 6/11/201626 Participation constraints total participation All entities of an entity type participate in the relationship type. Example: The participation of EMPLOYEE in WORKS_IN. partial participation A participation that is not total Example: The participation of EMPLOYEE in MANAGE. Chen Qian Univ. of Kentucky

27 6/11/201627 The (min,max) notation relationship constraints (1,1) (0,1) (1,N) (1,1) Chen Qian Univ. of Kentucky

28 6/11/201628 Recursive relationship We can also have a recursive relationship type. Both participations are same entity type in different roles. For example, SUPERVISION relationships between EMPLOYEE (in role of supervisor or boss) and (another) EMPLOYEE (in role of subordinate or worker). In ER diagram, need to display role names to distinguish participations. Chen Qian Univ. of Kentucky

29 6/11/201629 A RECURSIVE RELATIONSHIP SUPERVISION e 1 e 2 e 3 e 4 e 5 e 6 e 7 EMPLOYEE r1r2r3r4r5r6r1r2r3r4r5r6 SUPERVISION 2 1 1 2 2 1 1 1 2 1 2 2 Chen Qian Univ. of Kentucky

30 6/11/201630 Weak Entity Types A week entity is an entity that does not have a key attribute A weak entity must participate in an identifying relationship type with an owner Entities are identified by the combination of: A partial key of the weak entity type The particular owner they are related to in the owner type Chen Qian Univ. of Kentucky

31 6/11/2016Luke Huan Univ. of Kansas31

32 6/11/201632 Weak Entity Types Example: Suppose that a DEPENDENT entity is identified by the dependent’s first name and birthdate, and the specific EMPLOYEE that the dependent is related to. DEPENDENT is a weak entity type with EMPLOYEE as its owner via the identifying relationship type DEPENDENT_OF An dependent of John Smith, Mary, is identified by her first name Mary, birthdate 1/1/1980, and the entity John. Chen Qian Univ. of Kentucky

33 6/11/201633 SUMMARY OF ER-DIAGRAM NOTATION FOR ER SCHEMAS Meaning ENTITY TYPE WEAK ENTITY TYPE RELATIONSHIP TYPE IDENTIFYING RELATIONSHIP TYPE ATTRIBUTE KEY ATTRIBUTE MULTIVALUED ATTRIBUTE COMPOSITE ATTRIBUTE DERIVED ATTRIBUTE TOTAL PARTICIPATION OF E 2 IN R CARDINALITY RATIO 1:N FOR E 1 :E 2 IN R Key CONSTRAINT (min, max) ON PARTICIPATION OF E IN R Symbol E1E1 R E2E2 E1E1 R E2E2 R (min,max) E N Chen Qian Univ. of Kentucky

34 More ER Attribute of a relationship type: To describe some properties of the relationship. 6/11/201634Chen Qian Univ. of Kentucky name Employees ssn lot Works_In since dname budget did Departments

35 35 More ER dependentemployee IS_DEPEN An arrow indicates that given an entity, we can uniquely determine the other entity of the relationship. departmentemployee MANAGE (1,1) (0,N) (0,1)

36 36 ER-Design Principles Avoid redundancy. Limit the use of weak entity sets. Don’t use an entity set when an attribute will do. Limit the use of multivalued or composite attributes.

37 37 Avoiding Redundancy Redundancy occurs when we say the same thing in two different ways. Redundancy wastes space (more importantly) encourages inconsistency. The two instances of the same fact may become inconsistent if we change one and forget to change the other, related version.

38 Example of inconsistency Suppose in a DB, a student is stored as an entity with an attribute advisor, a professor is also stored as an entity with a multi-valued attribute advisees. So…. If a student changes her advisor, and we only make changes of the student entity. Inconsistency happens because the professor entity still includes her as an advisee. The right way is: 6/11/201638Chen Qian Univ. of Kentucky

39 39 Consistent ER studentprofessor advised name

40 More Example Assume we would like to set up a database for a wine distribution company. Wine Manufacture 40

41 41 Example BeersManfs ManfBy name manf addr

42 42 Example: redundancy BeersManfs ManfBy name This design states the manufacturer of a beer twice: as an attribute and as a related entity. name manf addr

43 43 Example Beers namemanfmanfAddr

44 44 Example: redundancy and losing information Beers name This design repeats the manufacturer’s address once for each beer; loses the address if there are temporarily no beers for a manufacturer. manfmanfAddr

45 45 Example BeersManfs ManfBy name addr

46 46 Example BeersManfs ManfBy name This design gives the address of each manufacturer exactly once. nameaddr

47 47 Don’t Overuse Weak Entity Sets Beginning database designers often doubt that anything could be a key by itself. They make all entity sets weak, supported by all other entity sets to which they are linked. In reality, we usually create unique ID’s for entity sets. Examples include social-security numbers, automobile VIN’s etc.

48 48 When Do We Need Weak Entity Sets? The usual reason is that there is no global authority capable of creating unique ID’s. Example it is unlikely that there could be an agreement to assign unique player numbers across all football teams in the world.

49 Limit the use of multivalued or composite attributes 6/11/2016Chen Qian Univ. of Kentucky49 Because they can be converted to entities, which are easier to manage Manfs nameaddr beer

50 Limit the use of multivalued or composite attributes 6/11/2016Chen Qian Univ. of Kentucky50 Because they can be converted to entities, which are easier to manage BeersManfs ManfBy name addr

51 Decision making 6/11/2016Chen Qian Univ. of Kentucky51 Entity vs. Attribute

52 52 Entity Sets Versus Attributes An entity set should satisfy at least one of the following conditions: It is more than the name of something; it has at least one non-key attribute. or It is the “many” in a many-one or many-many relationship.

53 53 Example: Bad BeersManfs ManfBy name

54 54 Example: Bad BeersManfs ManfBy name Since the manufacturer is nothing but a name, and is not at the “many” end of any relationship, it should not be an entity set. name

55 55 Example: Good Beers name There is no need to make the manufacturer an entity set, because we record nothing about manufacturers besides their name. manf

56 56 Example: Good BeersManfs ManfBy name Manfs deserves to be an entity set because of the non-key attribute addr. Beers deserves to be an entity set because it is the “many” of the many-one relationship ManfBy. nameaddr

57 57 ER Case Study I Works_In does not allow an employee to work in a department for two or more periods. We want to record several values of the descriptive attributes for each instance of this relationship. name Employees ssn lot Works_In from to dname budget did Departments dname budget did name Departments ssn lot Employees Works_In Duration from to 57 6/11/2016

58 58 ER Case Study I We want to record several values of the descriptive attributes for each instance of this relationship. Why not dname budget did name Departments ssn lot Employees Works_In Duration from to 586/11/2016 dname budget did name Departments ssn lot Employees Works_In to from

59 6/11/201659 ER Case study II Design a database representing cities, counties, and states For states, record name and capital (city) For counties, record name, area, and location (state) For cities, record name, population, and location (county and state) Assume the following: Names of states are unique Names of counties are only unique within a state Names of cities are only unique within a county A city is always located in a single county A county is always located in a single state

60 6/11/201660 Case study : first design County area is repeated for every city in the county  Redundancy is bad.  What else? State capital should really be a city  Should “reference” entities through explicit relationships I didn’t say a city name is unique within a state! Cities In States name capital name population county_area county_name 606/11/2016

61 61 Case study : second design Technically, nothing in this design could prevent a city in state X from being the capital of another state Y, but oh well… Cities IsCapitalOf name population Counties name area name In States 616/11/2016

62 Summary of Conceptual Design Conceptual design follows requirements analysis, Yields a high-level description of data to be stored ER model popular for conceptual design Constructs are expressive, close to the way people think about their applications. Basic constructs: entities, relationships, and attributes (of entities and relationships). Note: There are many variations on ER model.

63 Summary of ER (Contd.) ER design is subjective. There are often many ways to model a given scenario! Analyzing alternatives can be tricky, especially for a large enterprise. Common choices include: Entity vs. attribute, entity vs. relationship, binary or n-ary relationship.

64 Homework Quiz (close book) Beginning of next class 10 mins Project Find your collaborator 6/11/201664


Download ppt "CS 405G Introduction to Database Systems Lecture 3: ER Model Instructor: Chen Qian."

Similar presentations


Ads by Google