Relational Database Design Principles and ER Diagrams

1. Functional dependencies

Note A->B means that the A function determines B, and it can also be said that the B function depends on A.

If {A1, A2, ... , An} is a set of one or more attributes of a relation whose set function determines all other attributes of the relation and is the smallest, then the set is called a key .

For A->B, if a proper subset A' of A can be found such that A'->B, then A->B is a partial functional dependency, otherwise it is a complete functional dependency.

For A->B, B->C, then A->C is a transitive functional dependency.

2. Abnormal

The functional dependency of the following student-course relationship is {Sno, Cname} -> {Sname, Sdept, Mname, Grade}, and the key code is {Sno, Cname}. That is, once students and courses are determined, other information can be determined.

Relationships that do not conform to the paradigm will generate many exceptions, mainly including the following four exceptions:

  • Redundant data: eg student-2 appears twice.
  • Modification exception: The information in one record is modified, but the same information in another record is not modified.
  • Deletion exception: Deleting one piece of information will also lose other pieces of information. For example, deleting course-1 needs to delete the first and third rows, then the information of student-1 will be lost.
  • Insertion exception: For example, if you want to insert a student's information, if the student has not selected a course, you cannot insert it.

3. Paradigm

Paradigm theory is to solve the four abnormalities mentioned above.

High-level normal forms depend on low-level normal forms, and 1NF is the lowest-level normal form.

Author: CyC2018
Link: https://leetcode.cn/leetbook/read/tech-interview-cookbook/omkkg7/
Source: LeetCode
Copyright belongs to the author. For commercial reprint, please contact the author for authorization, for non-commercial reprint, please indicate the source.

3.1 First Normal Form (1NF)

Attributes are indivisible.

3.2 Second Normal Form (2NF)

Every non-primary attribute is completely functionally dependent on the keycode.

can be satisfied by decomposition .

Before decomposition

In the above student-course relationship, {Sno, Cname} is the key code, which has the following functional dependencies:

  • Sno -> Sname, Sdept
  • Sdept -> Mname
  • Sno, Cname-> Grade

The Grade complete function relies on keycodes, it does not have any redundant data, and each student has a specific grade for each course.

Sname, Sdept and Mname are partly dependent on key codes. When a student takes multiple courses, these data will appear multiple times, resulting in a large amount of redundant data.

after decomposition

relationship-1

Has the following functional dependencies:

  • Sno -> Sname, Sdept
  • Sdept -> Mname

relationship-2

Has the following functional dependencies:

  • Sno, Cname -> Grade

 

3.3 Third Normal Form (3NF)

Non-primary properties do not transitively depend on keycodes.

The following transitive functional dependencies exist in Relation-1 above:

  • Sno -> Sdept -> Mname

The following decomposition can be done:

relationship-11

relationship-12

 

4. ER图

Entity-Relationship has three components: entity, attribute, and connection .

Used for conceptual design of relational database systems.

There are three types of relationships between entities: one-to-one, one-to-many, and many-to-many.

  • If A to B is a one-to-many relationship, then draw a line segment with an arrow pointing to B;
  • If it is one-to-one, draw two line segments with arrows;
  • If it is many-to-many, draw two line segments without arrows.

The Course and Student in the figure below have a one-to-many relationship:

Represents a relationship that appears multiple times : an entity that appears several times in a relationship needs to be connected with several lines .

The figure below shows the prerequisite relationship of a course. There are two Course entities in the prerequisite relationship. The first is the prerequisite course, and the latter is the post-requisite course. Therefore, two lines are needed to represent this relationship:

Multidirectionality of connection : Although a teacher can open multiple courses and teach multiple students, for a specific student and course, there is only one teacher teaching, which constitutes a ternary connection:

Represent subclasses: Use a triangle and two lines to connect the class and subclasses. The attributes and connections related to the subclasses are connected to the subclasses, while those related to both the parent class and the subclasses are connected to the parent class:

 

References:

  • AbrahamSilberschatz, HenryF.Korth, S.Sudarshan, etc. Concept of Database System[M]. Mechanical Industry Press, 2006.
  • Schwartz. High Performance MYSQL (3rd Edition)[M]. Electronic Industry Press, 2013.
  • Shi Jiaquan. Introduction to Database System[M]. Tsinghua University Press Co., Ltd., 2006.

Guess you like

Origin blog.csdn.net/daydayup858/article/details/129413616