ER diagram
ER stands for Entity and Relationship.
ER model defines entity, relationship, aggregation, ISA inheritance, constraints
Entity (A noun)
- An entity is an object, e.g. a person, a building, a company.
- An entity has attributes, such as company name, address, #employees, revenue.
- An attributes has a domain of value, such as #employees > 0.
- A key is a minimal set of attributes whose values uniquely identify an entity in the entity set.
- Candidate key: an entity set may have multiple keys, every key is called a candidate key.
- Primary key: a designated candidate key.
- Superkey: a set of fields that contains a key. e.g. {id} is a key, then {id, name} is a superkey.
- Entity representation: rectangle [entity name], oval [attributes], domain along with attribute, underlining primary key's attributes
Relationship (A verb)
- A relationship is an association among two or more entities, such as employee and company, denoted by a tuple (e1, e2, .. en). e.g. (Marc, Facebook)
- A relationship set is a set of similar relationship e.g. {(Marc, Facebook), (Tianhao, Facebook), (Fangzhou, Amazon)}
- A relationship attribute is a attribute describes this relationship, e.g. (Tianhao, Facebook, since: 2018, role: SDE)
- Representation: diamond [relationship name], oval [atttributes]
Aggregation (A meta-relationship)
- An aggregation describes a relationship between another relationship and an entity. e.g. [Student] --- <study with> ---[Professor]. <study with> --- <monitored by> --- [Director]
Weak entity and identifying relationship
An entity may not have a unique key, then to identify an entity in a entity set requires another entity.
e.g. An employee entity and an dependent entity. When an employee leaves a company, his/her dependent is meaningless, so the dependent should only exist when the employee exist.
To identify an dependent, we need the employee id and the dependent's name.
The weak entity has to be an total participation, key constraint is not necessary; the relationship is called identifying relationship
How an entity join a relationship (constraints)
- key constraints: an entity join a relationship at most once
in the relationship set, this entity appear at more once.
e.g. A company at most has one CEO. - total participation: an entity join a relationship at least once
in the relationship set, this entity appear at least once.
e.g. A company at least has one employee. - exact one participation: key constraint + total participation constraint
ISA
ISA inheritance Hourly_Employee and Contract_Employee are derived from Employee.
1). overlap constraints: if hold, whether two subclasses are allowed to contain the same entity.
2). covering constraints: if hold, every Employee must belong to one of the subclass.