540 likes | 721 Vues
Entity Relationship Model. Overview. Database design is essentially to decide the set of tables needed to store all the data in the underlying applications; capture the relationships among data. Design steps: Requirement analysis Conceptual database design Logical database design
E N D
Overview • Database design is essentially to decide the set of tables needed to • store all the data in the underlying applications; • capture the relationships among data. • Design steps: • Requirement analysis • Conceptual database design • Logical database design • Physical database design • …
Data modeling • Identify and understand thebusiness rules that govern data • A student may register for COMP3220(Database System Implementation) only if the student has passed COMP1160 • One lab session has at most 45 students • The percentage of ‘A’ grades must be between 10%-20% • Need a tool to represent these rules in a concise but accurate manner, so that they can be unambiguously understood by information systemdevelopers and users • Entity-Relationship Diagram (ER Diagram) is such a tool • Represent rules in terms of entities and their relationships • Proposed by Peter Chenin 1976
Entity and entity set • An entity is a real-world object distinguishable from other objects. • A person, a dog, a CD, a … • An entity has attributes. • A person has a nameand an address … • A dog has a name… • A CD has an author… • An entity set is a set of entities that share the same attributes. • A “person entity set”. • A “dog entity set”. • A “CD entity set”.
Entity set examples Dog Person
address name HKID ID name To represent an entity set Person Dog
address HKID name Key • Each entity set has a key minimal set of attributes whose value uniquely identifies an entity • There could be more than one candidate keys; the one designated is the primary key • Entity set: students Person age email student id name
Attribute types • Simple vs. composite • Single-valued vs. multi-valued • Name vs. phone-numbers • Derived vs. non-derived • Date of birth: Non-derived • Age: Derived
middle last-name date-of-birth first-name city zip-code street state street-number apartment-number street-name HKID name address Person phone-number age multi-valued attribute derived attribute
In Class Exercise I • Create an entity data model for the following list, and specify the attributes Stock
In Class Exercise II • You are asked to develop a small database for our department • Record the information of available COMP classes in one specific semester • Record the information of professors who will teach those classes • Record the information of all the students who are going to take those classes What are the entity sets in this database?
Relationship and relationship set • A relationship describes the connection between entities. • A person owns a dog. • A person bought a CD. • A man marries a woman. • Tsui works in Computer Science department. • A relationship set captures relationships of the same “type”. • See next.
Relationship set (cont.) • A relationship set is defined between two entity sets: • Each link is a relationship, and the set of all links constitutes a relationship set. owns
address name HKID ID name To represent a relationship owns Person Dog
Relationship with attribute:Owns 1/1/01 Sam Alice Lady Bob 1/1/01 Candy Bear 12/9/03 Person Owns Dog
address name HKID ID name since To represent a relationship owns Person Dog
A Note on Relation vs. Relationship • Relation • Also known as table in relational model • Set of tuples • Relationship • ER Model • Describes relationship between entities
Constraints on relationship sets • Each relationship set is associated with two constraints. • Cardinality constraint • Participation constraint • These constraints offer a powerful way to model real-world connections.
Cardinality constraint • One to one • One to many • Many to one • Many to many • We will see examples in the next few slides. 1-to-1 1-to-Many Many-to-1 Many-to-Many
Entity set examples customer-id customer- customer- customer- loan- amount name street city number
Cardinality constraint One to one (e.g., husband-wife) One to many (e.g., father-child)
One-to-one representation • A customer can borrow at most one loan. • A loan can be borrowed by at most one customer. one
One-to-many representation • A customer can borrow many loans. • A loan can be borrowed by at most one customer. many one
Cardinality constraint Many to one (e.g., child-father) Many to many (e.g., friendship)
Many-to-one representation • A customer can borrow at most one loan. • A loan can be shared by many customers. many one
Many-to-many representation • A customer can borrow many loans. • A loan can be shared by many customers.
In Class Exercise • Draw the ER diagram for our CS database (Slide 11) with the following additional information: • For each student, record the grade of every class he/she takes • A class is taught by a professor (i.e., no joint teaching) • A professor can teach many classes
Constraints of relationship sets • Each relationship set is associated with two constraints. • Cardinality constraint • Participation constraint
Participation constraint • Total participation: Every entity in the entity set must participate in at least one relationship. • Partial participation: Some entities may not participate. partial total • Some customers may not borrow any loan. • Every loan must be borrowed by at least one customer.
Participation constraint (cont.) • What can be implied from the participation constraints in this relationship? partial total • Every customer must borrow at least a loan. • Some loan may not be borrowed by any customer.
More on relationships constraints • Cardinality constraints. • 1-1, 1-m, m-1, m-m • Participation constraints. • Partial, total • The two types of constraints are independent. • Each cardinality constraint can be used with any participation constraint. • See some examples next.
More on constraints (cont.) • What can you imply in the following relationships?
More on constraints (cont.) • What can you imply in the following relationships?
More on constraints (cont.) • What constraints should be applied if: • Every customer should borrow exactly a loan. • Every loan should be borrowed by exactly one customer.
More on constraints (cont.) • Every customer should borrow exactly a loan. • Every loan should be borrowed by exactly one customer. • Answer:
In Class Exercise • Complete the ER diagram for our CS database with participant constraints: • A student registers at least one class • A class is registered by at least one student • A class must be taught by some professor
“Self” relationships • The entity sets participating in a relationship can be the same.
Keys • Primary keys of entities are underlined in an ER diagram.
Keys for relationship sets • Attributes that can uniquely identify a relationship in the relationship set. • The union of the primary keys of the participating entity sets contains the primary key of a relationship set. • {Customer-id, loan-number} can uniquely decide each borrower relationship. • But it is not a primary key –– it is not minimal.
Keys for 1-to-1 relationship sets • The primary key can be {customer-id} or {loan-number}.
Keys for 1-m and m-m relationship sets Primary key is {loan_number} Primary key is {customer-id, loan_number}
In Class Exercise • What are the keys for our CS database?
Ternary relationship set Two example works-on relationships: (John, HKBU branch, manager) (John, Central branch, officer) They indicate that John takes different roles in the two branches.
Strong vs. weak entities • Strong entity • exists independently of other types of entities • has its own unique identifier • Weak entity • dependent on a strong entity…cannot exist on its own • does not have a unique identifier
name cost pname age ssn lot Policy Dependents Employees Weak entity sets • A weak entity set cannot exist by itself. • It depends on an identifying entityset(strong entity set) via an identifying relationship set. • An identifying relationship is depicted using dark diamond. • A weak entity set is depicted using a dark rectangle. Partial key Identifying owner
Weak entity sets (cont.) • The partial key of a weak entity contains a set of attributes that distinguishes it, among the weak entities belonging to the same strong entity. • A partial key is underlined with a dotted line. • The primary key of a weak entity set is the union of • its partial key, and • the primary key of its host strong entity set. • Primary key for policy: (ssn, pname)
Class (ISA) Hierarchies • We declare A ISA B (e.g., every A entity is also considered to be a B entity). • A child entity has all the attributes of a parent entity; but not the vice versa. name ssn lot Employees hourly_wages hours_worked ISA contractid Contract_Emps Hourly_Emps
Constraints on class hierarchy • Disjoint: An entity can belong to only one lower-level entity set. • An employee cannot be both an hourly_employee and a contract_employee • Overlapping: The opposite. • Covering: Each entity in the parent entity set must be specialized. • Every employee has to belong to one of hourly_employee and contract_employee • Non-covering: The opposite.