PostgreSQL - ER diagram

Created:2019-03-03  Last modified:2019-03-03


  1. ER diagram

    ER stands for Entity and Relationship.

    ER model defines entity, relationship, aggregation, ISA inheritance, constraints

    Entity (A noun)

    1. An entity is an object, e.g. a person, a building, a company.
    2. An entity has attributes, such as company name, address, #employees, revenue.
    3. An attributes has a domain of value, such as #employees > 0.
    4. A key is a minimal set of attributes whose values uniquely identify an entity in the entity set.
    5. Candidate key: an entity set may have multiple keys, every key is called a candidate key.
    6. Primary key: a designated candidate key.
    7. Superkey: a set of fields that contains a key. e.g. {id} is a key, then {id, name} is a superkey.
    8. Entity representation: rectangle [entity name], oval [attributes], domain along with attribute, underlining primary key's attributes

    Relationship (A verb)

    1. 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)
    2. A relationship set is a set of similar relationship e.g. {(Marc, Facebook), (Tianhao, Facebook), (Fangzhou, Amazon)}
    3. A relationship attribute is a attribute describes this relationship, e.g. (Tianhao, Facebook, since: 2018, role: SDE)
    4. Representation: diamond [relationship name], oval [atttributes]

    Aggregation (A meta-relationship)

    1. 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)

    1. 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.
    2. 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.
    3. 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.

  2. ER Design Considerations

    1. Entity vs. Attribute

      A person's address is XXXXXX, the address can be an attribute of the person, but can also be an entity and conntect to the person via a relationship.
  3. References