O slideshow foi denunciado.
Utilizamos seu perfil e dados de atividades no LinkedIn para personalizar e exibir anúncios mais relevantes. Altere suas preferências de anúncios quando desejar.


3.350 visualizações

Publicada em

Publicada em: Negócios, Tecnologia
  • Seja o primeiro a comentar

  • Seja a primeira pessoa a gostar disto


  1. 1. 2. Entity Relationship(ER) Model DATABASE MANAGEMENT SYSTEMS
  2. 2. THE RELATİON MODEL <ul><li>Relational databases are based on the relational model of data. Code defined the term ‘data model’ to consist of there element: </li></ul><ul><li>Definitions of the structure of the data </li></ul><ul><li>Definition of the types of operations allowed on the data </li></ul><ul><li>Integrity constraints used to ensure the data is as accurate as possible. For example, constraints can be specified to ensure that data fails within a set of values. </li></ul>
  3. 3. BASİC CONCEPT <ul><li>A database can be modeled as </li></ul><ul><ul><li>a collection of entities </li></ul></ul><ul><ul><li>relationship among entities </li></ul></ul><ul><li>An entity is an object that exists independently and is distinguishable from other objects. </li></ul><ul><ul><li>an employee, a company, a car, etc. </li></ul></ul><ul><ul><li>color, age, etc. are not entities </li></ul></ul><ul><ul><li>A relation is a table or flat file with columns and rows which has certain properties </li></ul></ul><ul><ul><li>A tuple is a row of a relation and represents an instance of a relation </li></ul></ul>
  4. 4. <ul><ul><li>An attribute is a named column of a relation </li></ul></ul><ul><ul><li>A domain is the set of allowable values for one or more attributes </li></ul></ul><ul><ul><li>The degree of a relation is the number of attributes it contains </li></ul></ul><ul><ul><li>The cardinality of relation is the number of tuples it contains </li></ul></ul>BASİC CONCEPT
  5. 5. AN ENTİTY SET İS A SET OF ENTİTİES OF THE SAME TYPE <ul><li>E.g., a set of employees, a set of departments  also called entity types </li></ul>Employee Entity Type : e 1 e 2 e 3 Entity set: … The actual employees A general specification
  6. 6. ATTRİBUTES <ul><li>Properties of an entity or a relationship </li></ul><ul><ul><li>name, address, weight, height are properties of a Person entity. </li></ul></ul><ul><li>date of marriage is a property of the relationship Marriage . </li></ul><ul><li>ER models are usually represented graphically. The language we are going to use represents entity types as rectangles . </li></ul>
  7. 7. TYPES OF ATTRİBUTES <ul><li>Simple attribute : contains a single value. </li></ul>Employee EmpNo Name Address
  8. 8. CONPOSİTE ATTRİBUTE:CONSİST OF SEVERAL COMPONENTS Country Employee Address Street City EmpNo Name
  11. 11. KEY ATTRİBUTES <ul><li>A set of attributes that can uniquely identify an entity </li></ul>ERD tabular Employee EmpNo Name EmpNo Name . . . 123456 John Wong . . . 456789 . . . 146777 . . . John Wong Mary Cheung
  12. 12. KEY ATTRİBUTES <ul><li>Composite key : Name or Address alone cannot uniquely identify an employee, but together they can! </li></ul>Employee Name Address
  13. 13. KEY ATTRİBUTES <ul><li>An entity may have more than one key </li></ul><ul><ul><li>e.g., EmpNo , ( Name, Address ) </li></ul></ul><ul><ul><li>only one is selected as the key. (sometimes called the Primary key ) </li></ul></ul>In many cases, a key is artificially introduced (e.g., EmpNo) to make applications more efficient. Employee EmpNo Name Address
  14. 14. RELATİONSHİP <ul><li>A relationship is an association between one or more entities. </li></ul><ul><ul><li>Given a customer and an account , the relationship depositor between them indicates that the customer deposits money into the account. </li></ul></ul><ul><ul><li>The language we are going to use represents entity types relationships as diamonds. </li></ul></ul>
  15. 15. <ul><li>A relationship may have attributes </li></ul><ul><li>A relationship type or relationship set identifies relationships of the same properties </li></ul>Question: Could Amount be an attribute of Customer? Or an attribute of Account? What does Amount mean? How many values you want to keep? Customer depositor Account Amount CusNo Name AccNo Name
  16. 16. REPRESENTATİON OF RELATİONSHİP <ul><li>Tabular: </li></ul>Depositor Note: this is NOT an ERD The amount in each deposit. CustomerNo A123456 B456789 B456789 AccountNo A-101 A-201 A-302 Amount 500 900 700 A123456 B456789 A-101 A-201 A-202 500 900 700 <ul><li>Graph : </li></ul>
  17. 17. TRY AN ALTERNATİVE <ul><li>Represent Amount as an attribute of Account </li></ul>AccountNo A-101 A-201 A-302 Name Current Saving Current Amount 500 900 700 <ul><li>Consider Amount as the balance of an account (I.e., one value per account) or as the last deposit amount. </li></ul><ul><li>“ Multivalue” attribute, though allowed in ER model, is difficult to implement </li></ul>
  18. 18. CARDİNALİTY OF RELATİONSHİPS <ul><li>Number of entities that can be associated together in a relationship set. </li></ul><ul><li>1 : 1 One entity of type X can be associated with, at most, one entity of type Y. One entity of type Y can be associated with, at most, one entity of type X. </li></ul>Customer Loan Borrows <ul><li>A customer can borrow 1 loan and vice versa </li></ul>
  19. 19. 1 : N Customer Loan One entity of type X can be associated with many entities of type Y . One entity of type Y can be associated with, at most, one entity of type X . Customer Borrow Loan <ul><li>A Customer can borrow more than 1 loan, </li></ul><ul><li>whereas a loan has only one borrower. </li></ul>
  20. 20. N : M One entity of type X can be associated with many entities of type Y . One entity of type Y can be associated with many entities of type X . Customer Borrow loan <ul><li>A customer can borrow more than one loan </li></ul><ul><li>A loan can have more than one borrower. </li></ul>
  21. 21. NOTES <ul><li>Cardinality specifies the maximum condition. </li></ul><ul><li>1 : 1 N : M </li></ul><ul><li>1 : N </li></ul><ul><li>The minimum is specified by existence constraints (explained later) </li></ul><ul><li>Conditions must be satisfied at all times </li></ul>
  22. 22. DEGREES OF A RELATİONSHİPS SET <ul><li>Number of entity sets participating in a relationship set. </li></ul><ul><li>Relationships with degree >3 is very rare. </li></ul><ul><li>Hint: translate a ternary relationship into one sentence. </li></ul><ul><li>Can you break it up into two or more sentences? </li></ul><ul><li>A customer borrows a loan from a branch. </li></ul><ul><li>A customer borrows a loan. A loan is made at a branch. </li></ul>Customer Loan Borrow - Binary Customer Loan Borrow Branch - Ternary
  23. 23. RECURSİVE RELATİONSHİPS <ul><li>A relationship relating entitles of the same type </li></ul><ul><li>Employees play different roles: manager or worker </li></ul><ul><ul><li>Without role names, you can’t tell whether 1 employee manages n other employees or n employees manages 1 employee </li></ul></ul><ul><li>You can “unfold” a recursive relationship to understand it: </li></ul>manager worker Employee work-for Employee manager worker Employee work-for
  24. 24. TABULAR REPRESENTATİON OF RECURSİVE RELATİONSHİPS <ul><li>Without Role Names </li></ul><ul><li>With Role Names </li></ul><ul><li>Where ManagerNo and WorkerNo are Valid EmployeeNo </li></ul>ManagerNo WorkerNo A1234 A6543 A1234 A8734 EmployeeNo EmployeeNo A1234 A8734 A1234 A6543
  25. 25. OPTİONAL VERSUS MANDATORY <ul><li>Assume there is an entity type called person , and entity subtypes called customer and employee . When a person is created, the designer of the database has two options: </li></ul><ul><li>mandatory </li></ul><ul><li>He/she can demand that the person be classified as one of the subtypes. </li></ul><ul><li>optional </li></ul><ul><li>He/she can allow a person to be created without classifying the person as any subtype. </li></ul>
  26. 26. OPTİONAL VERSUS MANDATORY Neither one is preferable to the other. The proper one to choose depends on the business situation. Mandatory sub-typing is represented by creating a double line from the super-type ( person in the following ER diagram) to the circle. Optional sub-typing is represented by leaving a single line from the super-type to the circle.
  27. 27. AGGREGATİON OF ENTİTY TYPES <ul><li>Subtypes are generally thought of in terms of X is a Y (which is why these are commonly referred to as is-a relationships). Another type of relationship that needs to be represented in a database is the part of relationship, more formally called aggregation . When an entity is made up of several different types of other entities, an aggregation relationship may be called for. </li></ul>
  28. 28. AGGREGATİON OF ENTİTY TYPES Consider the relationship between a car and its engine and body. The engine and body are both part of the car. The relationship is represented as follows in an ER diagram.
  29. 29. PARALLEL RELATİONSHİPS <ul><li>Two entities can have more than one type of relationship. This is not surprising; further, it is not difficult to represent in a database or in an ER diagram. Consider the entity types person and insurance policy and the relationships between them of pays for and is insured under. </li></ul>
  30. 30. PARALLEL RELATİONSHİPS A person pays for zero or more insurance policies. An insurance policy is paid for by exactly one person. A person is insured by zero or more insurance policies. An insurance policy insures one or more persons.
  31. 31. EXİSTENCE DEPENDENCE <ul><li>The existence of an entity depends on the existence of another entity </li></ul>Customer Loan loan borrow CusNo LoanNo A loan cannot exist if there is no borrower.
  32. 32. WEAK ENTİTİES <ul><li>A weak entity cannot be identified with its own attributes  no key </li></ul><ul><li>A weak entity implies existence dependency but NOT vice versa </li></ul>
  33. 33. LoanNo Amount Date_pay PaymentNo Payment <ul><li>A loan may have 240 payments, each identified by a payment no 1 - 240. </li></ul><ul><ul><li>The PaymentNo is unique given a particular loan but not unique globally </li></ul></ul><ul><li>PaymentNo is called partial key </li></ul><ul><li>The primary key of Payment is the combination of LoanNo and PaymentNo . </li></ul>Loan payment Question: Why not combine loan and payment into one entity type? Amount Loan
  34. 34. WEAK ENTİTY VS EXİSTENCE CONSTRAİNTS <ul><li>In the existence constraint example, LoanNo can uniquely identify a Loan in the database so it is not a weak entity. </li></ul><ul><li>The existence constraint means that you cannot create a Loan record without first knowing who borrowed the loan. </li></ul>
  35. 35. ANOTHER EXAMPLE OF WEAK ENTİTY TYPE <ul><li>A child may not be old enough to have a HKID number </li></ul><ul><li>Even if he/she has a HKID number, the company may not be interested in keeping it in the database. </li></ul>EmpNo Name Dependent Emp_Dep Age Employee
  36. 36. TERNARY RELATİONSHİPS Customer Loan Borrow Branch A customer borrows a loan from a branch. Customer Loan Borrow Branch Issue A customer borrows a loan. A loan is issued from a branch.
  37. 37. WHAT ARE THE DİFFERENCES? A customer borrows a loan from a branch. A customer borrows a loan. A loan is issued from a branch.
  38. 38. <ul><li>Imagine a bank allows borrowers of the same loan to go to difference branches for signing documents, deposit payments, etc. </li></ul><ul><li>The two schemes are not the same. The binary relationships capture less information. </li></ul><ul><li>Adding a third relationship won’t help . </li></ul>Customer Loan Borrow Branch Issue Cus_Br
  39. 39. WHY? <ul><li>Customer, Loan and Branch have a N:M:P relationship </li></ul><ul><li>N:N:1 relationship can be decomposed (A loan is issued by ONE BRANCH ONLY) </li></ul><ul><li>John borrows a loan which is issued from Wanchai branch </li></ul>A12345 B56789 A12345 A12345 L-001
  40. 40. BİNARY RELATİONSHİPS TO TERNARY? <ul><li>Binary relationships may have different meanings so that they can’t be combined into ternary relationships . </li></ul><ul><li>You may have a ternary relationship Customer-Loan-Branch and other binary relationships between Customer, Loan and Branch </li></ul>Customer Loan Borrow Branch Issue Buy_stock
  41. 41. A CASE STUDY A primary school student writes a composition about a picnic: Today is Sep 9, the weather is fine. My classmates, John, Mary and I go to a picnic in Sai Kung. Our teacher is Ms Wong Initial Design: Picnic weather destination date Students Name Teacher Name
  42. 42. QUESTİONS ? <ul><li>Why “John”, “Mary”, “Miss Wong” are not in the ER diagram ? </li></ul><ul><li>What do these names tell us ? </li></ul><ul><li>What are the keys of Student, Picnic & Teacher ? </li></ul><ul><li>What are the cardinalities of the relationships ? </li></ul>
  43. 43. <ul><li>Every student has an ID number, it is better to keep it in the database and use it as a key </li></ul><ul><li>I bet that there won’t be teachers with the same name; otherwise, I’ll add employee number and use it as a key </li></ul><ul><li>goes is N:M, why ? A picnic has more than one student participating; also, a student can go to more than 1 picnic. However, this N:M relationship allows a student to go to more than one picnic on the same date </li></ul><ul><li>leading is N:1 , why? Depends on your assumptions </li></ul><ul><ul><li>I assume a teacher can only lead 1 picnic on a certain date, so given the teacher name and the date, I can identify a picnic </li></ul></ul><ul><li>Picnic is made a weak entity. I could have added a PicnicNo, but it would be very awkward. </li></ul>My solution Question: How to record number of students in a picnic? Student StudentNo Name weather date destination Picnic goes leading Teacher Name
  44. 44. E-R DESİGN DECİSİONS <ul><li>The use of an attribute or entity set to represent an object. </li></ul><ul><ul><li>Should an address be an attribute or entity? </li></ul></ul><ul><li>Whether a real-world concept is best expressed by an entity set or a relation set. </li></ul><ul><ul><li>Should marriage be an entity or relationship? </li></ul></ul><ul><ul><li>Should picnic be an entity or relationship? </li></ul></ul><ul><li>The use of a ternary relationship versus a pair of binary relationships. </li></ul><ul><ul><li>See the borrow-loan-branch example </li></ul></ul><ul><li>The use of a strong or weak entity set. </li></ul><ul><ul><li>See the employee-dependent example </li></ul></ul>
  45. 45. E-R DİAGRAM FOR COMPANY DATABASE EMPLOYEE WORKS_FOR MANAGES CONTROLS Startdate DEPARTMENT WORKS_ON PROJECT Hours DEPENDENTS_OF DEPENDENT SUPERVISION supervisee supervisor Fname Minit Lname Name Sex Address Salary Ssn Bdate Number Of Employees Locations Can you translate it back into English? Name Number Name Number Location Relationship Birthdate Sex Name
  46. 46. LİMİTATİONS OF ER MODEL <ul><li>Consider representing Part-time and Full-time employees in the company database: </li></ul><ul><ul><li>Either you have two entity types will lots of similarity </li></ul></ul><ul><ul><li>Or you have a single entity type with redundancy for most of the entities within it </li></ul></ul><ul><li>ER model is extended to support other features such as generalization </li></ul>
  47. 47. REDUCTİON OF AN E-R SCHEMA TO TABLES <ul><li>Primary keys allow entity sets and relationship sets to be expressed uniformly as tables which represent the contents of the database. </li></ul><ul><li>A database which conforms to an E-R diagram can be represented by a collection of tables. Always! </li></ul><ul><li>Converting an E-R diagram to a table format is the basis for deriving a relational database design from an E-R diagram. </li></ul>
  48. 48. REPRESENTİNG ENTİTY SETS AS TABLES <ul><li>A weak entity set becomes a table that includes a column for the primary key of the identifying strong entity set </li></ul><ul><li>A strong/regular entity set reduces to a table with the same attributes. </li></ul>Composite key payment loan-no payment-date payment-no payment-amount L-17 L-15 L-23 5 22 11 10 May 1999 23 May 1999 17 May 1999 50 300 75 customer customer-name customer-street customer-id Jones Hayes Smith 321-12-3123 677-89-9011 019-28-3746 Main Main North customer-city Harrison Rye Harrison
  49. 49. REPRESENTİNG RELATİONSHİP SETS AS TABLES depositor <ul><li>A many-to-many relationship set is represented as table with columns for the primary keys of the two participating entity sets, and any descriptive attributes of the relationship set. </li></ul>customer borrow loan loan-no cust-no share% date cust-name
  50. 50. <ul><li>For 1:N and 1:1 relationships, you can create a table for each relationship </li></ul><ul><li>But it is more concise to merge the relationship-table with the entity-table on the “N” side </li></ul>customer cust-no cust-name A12345 B56789 Peter Wong Mary Cheung loan loan-no L-001 L-002 date Sep 2000 Aug 2001 customer borrow loan cust-no cust-name date loan-no cust-no A12345 B56789 indicates who borrowed the loan
  51. 51. WEAK ENTİTİES <ul><li>Since a weak entity has to include the primary key of the identifying entity, the 1:N relationship is already captured. E.g., The payment table already contains information about the Loan (I.e., loan_no) </li></ul>Already indicates the 1:N relationship between loan-no and payment-no payment loan-no payment-date payment-no payment-amount L-17 L-17 L-17 5 7 6 10 May 1999 23 May 1999 17 May 1999 50 300 75