What is one-to-one, one-to-many, many-to-many? What is an ER diagram and how to draw an ER diagram? What is a relational schema and how to convert an ER diagram into a relational schema

1. What is one-to-one, one-to-many, many-to-many?

One-to-one, one-to-many, and many-to-many are the three types of relationships between database entities

One to one:

  • A person has only one ID number, and an ID number can only identify one person

  • A person has only one facial biometric information, and a facial biometric information can only belong to one person

One-to-many:

  • Zhang San only belongs to Class 1, Senior 3, but there are 30 students in Class 1, Senior 3

  • Nanjing City can only belong to Jiangsu Province, but Jiangsu Province has 13 cities

Many-to-many:

  • Xiao Ming can choose Chinese, Mathematics, English and other courses, and Chinese can also be chosen by Xiao Wang, Xiao Li, Xiao Zhao and other students

  • Lianhua Supermarket can sell Lay’s Potato Chips, Haojia Potato Chips, and other brands of potato chips. Lay’s Potato Chips can also be sold by RT-Mart and Carrefour and other supermarkets.


2. What is an ER diagram? How to draw an ER diagram

The ER diagram is the entity-relationship diagram, E stands for Entity, and R stands for Relationship, which represents entity types, attributes and connections. It is a conceptual model used to describe the real world, such as Zhang San, quantum mechanics, etc.

The ER diagram contains entities, attributes, and connections. Entities represent things that can be distinguished from each other. They can be people, objects, or abstract concepts; attributes represent certain characteristics of entities, such as Zhang San's name and the brand of potato chips. Etc.; associations represent associations between entities or within entities.

Usually, a rectangular frame is used to represent the entity. If it is a weak entity, add a rectangle outside the rectangle. The oval frame represents the attribute, and the diamond frame represents the connection. Use a straight line to connect the entity with the connection, the entity with the attribute, and the connection with the attribute. The relationship between entities is marked on the connection line (1:1, 1:N, M:N).

For example:

A company has multiple departments, and each department has a person in charge, and each person in charge is in charge of the employees of their respective departments.


3. What is a relational model? How to convert ER diagram into relational schema?

The relationship model is a description of the relationship, which can be formally expressed as: R (U, D, dom, F), generally abbreviated as: R (U), such as students (student number, name, gender), where students It is the relationship name, and the student number, name, and gender are the attributes.

  • R: relation name

  • U: collection of attribute names

  • D: The domain the attribute comes from

  • dom: a collection of mappings from attributes to domains

  • F: Dependency collection of data between attributes

The relational mode corresponds to the table structure of the database , for example:

Student (student number, name, gender)

course(course_id, course_name)

Elective (student number, course number, grades)

ER can be converted to a relational schema, for example:

ER diagram of 1:1 relationship:

Doctor (doctor number, department)

Patient (name, age, patient number)

Responsible for (doctor number, patient number)

The connection in this ER diagram is responsible , and the connection in the one-to-one relationship can be merged into any entity . For example, the responsible group can be merged into the patient :

Doctor (doctor number, department)

Patient (name, age, patient number, doctor number)

It is also possible to merge the responsible group into a doctor :

Doctor (doctor number, department, patient number)

Patient (name, age, patient number)

ER diagram of 1:n relationship:

Company (unified social credit code, business license, legal person)

department (department code, department name)

Settings (unified social credit code, department code)

The relationship in this ER diagram is set , and the relationship in the one-to-many relationship can be merged into many entities:

Company (unified social credit code, business license, legal person)

Department (department code, department name, unified social credit code)

ER diagram of m:n relationship:

Student (student number, name, age)

course(course_id, course_name)

select (student number, course number)

In many-to-many relationships, associations cannot be merged.

Guess you like

Origin blog.csdn.net/sxww_zyt/article/details/129587984