Database system principle ------ ER diagram into relational mode

ER diagram conversion

The ER diagram is composed of three elements: entities, attributes of entities, and connections between entities. Converting ER diagrams to relational models is actually to convert entities, attributes of entities, and connections between entities into relational models.

Entity Set to Relational Schema Conversion

General conversion principles

Conversion rules for entity sets: an entity type is converted to a relational schema. The attribute of the entity is the attribute of the relationship, and the code of the entity is the code.

Example: Student entity can be transformed into the following relationship schema:

Student (student number, name, gender, date of birth, department, grade)

Transformation of a relationship set between two entity types to a relational schema

  • Conversion method of 1:1 relationship: 1:1 relationship can be converted into an independent relationship model, and can also be merged with the relationship model corresponding to either end.

    • If it is converted into an independent relational schema, the primary key of each entity connected to the relation and the attributes of the relation itself are transformed into relational attributes, and the primary key of each entity is a candidate key of the relation.

    • If it is merged with the relational schema corresponding to a certain end, it is necessary to add the primary key of another relational schema and the attributes of the connection itself to the attributes of the relational schema.

    • example

Conversion method of 1:n relationship: 1:n relationship can be converted into an independent relationship mode, or merged with the relationship mode corresponding to the n-terminal.

  • If it is converted into an independent relational schema, the primary key of each entity connected to the relation and the attributes of the relation itself are transformed into the attributes of the relation, and the primary key of the relation is the primary key of the n-terminal entity.

  • If it is merged with the n-terminal relationship model, a new attribute is added to the n-terminal entity set, and the new attribute is composed of the primary key of the 1-terminal entity set corresponding to the relationship and the attribute of the relationship itself, while the primary key of the relationship model remains unchanged.

  • example

  • Option 1: 1: The relationship formed by n connections exists independently.
    Warehouse (warehouse number, location, area)
    product (product number, product name, price)
    warehouse (product number, warehouse number, quantity)
  • Solution 2: The relationship formed by the connection is merged with the n-terminal object
    Warehouse (warehouse number, location, area)
    product (product number, product name, price,
    warehouse number, quantity)

The conversion method of the m:n relationship: the codes of the entities connected to the relationship and the attributes of the relationship itself are converted into the attributes of the relationship. The code of the relationship is the combination of the codes of each entity.

  • example

Conversion of relationship sets between the same entity type to relational schema

​ vThe connection between entities of the same entity type is self-connection, which can also be handled separately according to the above three cases of 1:1, 1: n and m:n .

  • 1:n

  • m:n

The conversion of the relationship between multiple entity types to the relational model

Generally, there are three situations of 1:1, 1:n and m:n between more than two entity types, and the conversion method generally follows the following principles:

  • 1:n multi-relationship: Modify the relationship corresponding to the entity set at the end 1, that is, add the codes of other entity sets related to the relationship and the attributes of the link itself as new attributes to the entity set at the end 1.
  • Multi-element relationship of m:n: Create an independent relationship, and the codes of the entities connected by the multi-element relationship and the attributes of the relationship itself are converted into the attributes of the relationship. The code of the relationship is the combination of the codes of each entity.

Conversion of Weak Entity Set to Relational Mode

  • The concepts of strong entity set and weak entity set are closely related to existence dependence. Members of strong entity set must be dominant entities, while members of weak entity set are subordinate entities.
  • Since a weak entity cannot exist independently, it must be attached to an owner entity. Therefore, when converting a weak entity set into a relational schema, the relationship corresponding to the weak entity must contain the primary key of the owner entity .

Applications

  • ** After converting the ****ER graph to relational schema according to the above rules, the relational schemas with the same primary key can be merged. The main purpose is to reduce the number of relations in the system, and facilitate maintenance and maintain data consistency.

  • The merging method is to add all the attributes of a relational schema to another relational schema in relational schemas with the same primary key, then remove the synonymous attributes (maybe the same name or different names), and adjust the order of the attributes appropriately .

Example: The ER diagram of a certain project is shown in Figure 3-19. The attributes of each entity are as follows:

Guess you like

Origin blog.csdn.net/baidu_41774120/article/details/114449377