The third paradigm of database design

**第一范式:数据库表中的字段都是单一属性的,不可再分。
第二范式:非主键信息不是由整个主键函数来决定时,即违法第二范式
          信用卡号,学籍号 主键---->卡现金
第三范式:存在非主键属性  传递依赖 即违法第三范式
StudentNo CardNo  UserID  职位
021101    001     Operator 操作员**

First, the database design paradigm and its significance and inadequate
design paradigm database is a database designed to meet the required specifications, standardization and optimization of the structure of the database table to optimize the data is organized into a table way, so make the data clearer, more concise . In practice, a database is usually divided into two or more tables and the relationship between the tables is defined to achieve data isolation. Adding, deleting and modifying a field only needs to be done in one table, and then can be passed through the defined relationship To the remaining tables in the database (very similar to the meaning of the hierarchical idea). In this way, we can eliminate many opportunities for errors or junk data and reduce the workload necessary to update information.

At present, there are mainly six paradigms: the first paradigm, the second paradigm, the third paradigm, the BC paradigm, the fourth paradigm, and the fifth paradigm. The one that meets the minimum requirements is called First Normal Form, or 1NF for short. On the basis of the first paradigm, the second paradigm, or 2NF for short, can further satisfy some requirements. And so on

Things are often multi-faceted, and the design paradigm will also bring certain troubles: operation is difficult, because multiple tables need to be contacted to get the required data, and the higher the performance of the paradigm, the worse. Therefore, it is necessary to weigh the pros and cons to use the high paradigm. Generally, in the project, it is enough to use the third paradigm, which has good performance and convenient data management.

Second, let's take an example to introduce three database design paradigm
Description: Examples of a "school room fee system," the "Student Information Form", "up and down machine records the student table" part of the field

1. The first normal form 1NF
definition: the fields in the database table are all single attributes and cannot be divided.
Simply put, every attribute is an atomic item and indivisible.

1NF is the minimum condition that the relational model should have. If the database design cannot meet the first normal form, it is not called a relational database. In other words, as long as it is a relational database, it must meet the first normal form.

Let’s first look at a table 1-1 that does not meet 1NF.
CardNo StudentNo StudentName Sex Department CardCash UserID UserLevel Time
001 021101 Xiao Ming Nan Education College, Department of Psychology, Class 1, 100 Operator 2011/10/03, 09:00

The reason why this table does not conform to 1NF is that the Department and Time fields can be subdivided, so it should be changed to Table 1-2:
CardNo StudentNo StudentName Sex Academy Major class CardCash UserID UserLevel Date Time
001 021101 Department of Psychology, Xiao Ming Male Education College 1 100 Operator 2011/10/03 09:00

2. The second normal form 2NF
definition: there is no partial functional dependence of non-key fields on any candidate key field in the database table, which conforms to the second normal form.

"Note: What is a functional dependency, please refer to Baidu Encyclopedia ( http://baike.baidu.com/view/40008.htm ) for details .
If the value of a certain field A in a table is determined by the value of another field or a group of fields B, it is said that the A function depends on B.
2NF can reduce insert exception, delete exception and modify exception.

Simply put, on the one hand, the second normal form must satisfy the first normal form, otherwise there is no need to talk about the second normal form.
On the other hand, when the non-primary key information in a table is not determined by the entire primary key function, that is, when there is a part that depends on the table that is not the primary key or the part that depends on the primary key, it usually violates 2NF.

Let's look at the above table 1-2 that satisfies 1NF
CardNo StudentNo StudentName Sex Academy Major class CardCash UserID UserLevel Date Time
001 021101 Department of Psychology, Xiao Ming Nan Education College 1 100 Operator Operator 2011/10/03 09:00

We see that in this table, StudentName, Sex, Academy, Major, class, CardCash, UserID, Date, Time can be determined by CardNo and StudentNo. So you can use the combination of CardNo and StudentNo as the primary key.
However, we found that CardCash does not completely depend on CardNo and StudentNo. CardCash can be determined only by CardNo, because a card must have the amount in the card. This caused partial dependence. When this happens, the second normal form is not satisfied.
change into:

Let's look at another example, the student boarding and boarding record form, it will be more obvious. Table 2-1
CardNo StudentNo StudentName Sex Department Major class OnDate OnTime OffDate OffTime ConsumeTime ConsumeMoney
001 0211 Department of Psychology, Xiao Ming Nan Education College 1 2011/10/14 09:00 2011/10/14 10:00 1 2

We see that in this table, StudentName, Sex, Department, Major, and class are directly dependent on StudentNo, not dependent on other fields in the table, this design does not comply with the 2NF non-primary key information is not from the entire primary key function When deciding.

We can optimize 1-2 and 2-1 to:
3-1
StudentNo CardNo UserID UserLevel Date Time
021101 001 Operator 2011/10/03 09:00
3-2
CardNo CardCash
001 98
3-3
CardNo OnDate OnTime OffDate OffTime ConsumeTime ConsumeMoney
001 2011/10/14 09:00 2011/10/14 10:00 1 2
3-4
StudentNo StudentName Sex Academy Major class
021101 Department of Psychology, Xiao Ming Nan Education College 1

3. The third normal form 3NF
definition: On the basis of the second normal form, if there is no transfer function dependence of non-key fields on any candidate key field in the data table, it conforms to 3NF.

Let’s look at the optimized Table 3-1 in the above example
StudentNo CardNo UserID
UserLevel Date Time 021101 001 Operator Operator 2011/10/03 09:00

In the table, a UserID can determine a UserLevel. In this way, UserID depends on StudentNo and CardNo, and UserLevel depends on UserID, which leads to transitive dependence. 3NF is to eliminate this dependence.

We optimize 3-1 to get:
4-1
StudentNo CardNo UserID Date Time
021101 001 Operator 2011/10/03 09:00
4-2
UserID UserLevel Operator
Operator

We have seen that the third normal form rule searches to eliminate attributes that do not directly depend on the primary key of the table formed by the first normal form and the second normal form. We created a new table for all the information not associated with the primary key of the table. Each new table holds information from the source table and the primary key they depend on.

Third, the summary
database design standardization allows us to better adapt to change, so that you can change business rules, requirements and data without the need to reconfigure the entire system

Guess you like

Origin blog.csdn.net/ke_weiquan/article/details/53032769