MySQL database _ simple design of the database

MySQL database

Simple design of the database

  • The relational database is based on the ER model. We need to extract the model and relationship according to the design plan of the product manager, and formulate the table structure. This is the first step in the project.
  • In the development, there are many softwares for designing databases, such as power designer, db desinger, etc., which can intuitively see entities and the relationships between entities.
  • Design the database, which may be completed by a dedicated database designer, or it may be completed by members of the development team, usually the project manager leads the team members to complete
  • Developers do not need to complete the database design independently, but they should pay attention to accumulate some experience in this area

Three paradigm

  • After researching and summarizing the problems in use, some specifications are proposed for the design of the database. These specifications are called Normal Forms.
  • There are currently 8 paradigms that can be found. Generally, 3 paradigms need to be followed.

First Normal Form (1NF)

  • The emphasis is on the principle of the column, that is, the column cannot be divided into other columns.
考虑这样一个表:【联系人】(姓名,性别,电话) 如果在实际场景中,一个联系人有家庭电话和公司电
话,那么这种表结构设计就没有达到 1NF。要符合 1NF 我们只需把列(电话)拆分,即:【联系人】(姓
名,性别,家庭电话,公司电话)。1NF 很好辨别,但是 2NF 和 3NF 就容易搞混淆。

第一范式示例

sheji01

Second Normal Form (2NF)

  • The first is 1NF, and there are two other parts. One is that the table must have a primary key; the other is that the columns that are not included in the primary key must be completely dependent on the primary key, not just a part of the primary key.
考虑一个订单明细表:【OrderDetail】(OrderID,ProductID,UnitPrice,Discount,
Quantity,ProductName)。 因为我们知道在一个订单中可以订购多种产品,所以单单一个 OrderID 
是不足以成为主键的,主键应该是(OrderID,ProductID)。显而易见 Discount(折扣),
Quantity(数量)完全依赖(取决)于主键(OderID,ProductID),而 UnitPrice,ProductName 
只依赖于 ProductID。所以 OrderDetail 表不符合 2NF。不符合 2NF 的设计容易产生冗余数据。


可以把【OrderDetail】表拆分为【OrderDetail】(OrderID,ProductID,Discount,
Quantity)和【Product】(ProductID,UnitPrice,ProductName)来消除原订单表中
UnitPrice,ProductName多次重复的情况。

第二范式示例

sheji02

Third Normal Form (3NF)

  • The first is 2NF. In addition, non-primary key columns must directly depend on the primary key, and there can be no transitive dependencies. That is, it cannot exist: the non-primary key column A depends on the non-primary key column B, and the non-primary key column B depends on the primary key.
考虑一个订单表【Order】(OrderID,OrderDate,CustomerID,CustomerName,CustomerAddr,
CustomerCity)主键是(OrderID)。 其中 OrderDate,CustomerID,CustomerName,
CustomerAddr,CustomerCity 等非主键列都完全依赖于主键(OrderID),所以符合 2NF。不过问题
是 CustomerName,CustomerAddr,CustomerCity 直接依赖的是 CustomerID(非主键列),而不
是直接依赖于主键,它是通过传递才依赖于主键,所以不符合 3NF。 通过拆分【Order】为【Order】
(OrderID,OrderDate,CustomerID)和【Customer】(CustomerID,CustomerName,
CustomerAddr,CustomerCity)从而达到 3NF。 *第二范式(2NF)和第三范式(3NF)的概念很容易
混淆,区分它们的关键点在于,2NF:非主键列是否完全依赖于主键,还是依赖于主键的一部分;3NF:非主
键列是直接依赖于主键,还是直接依赖于非主键列。

第三范式示例

sheji03

Final table

sheji04

ER model

  • E means entry, entity, designing an entity is like defining a class, specifying from which aspects the object is described, and an entity is converted into a table in the database
  • R represents relationship, relationship, relationship describes the corresponding rules between two entities, the types of relationships include one-to-one, one-to-many, and many-to-many
  • The relationship is also a kind of data, which needs to be stored in the table through a field
  • Entity A to entity B is 1 to 1, then create a field in table A or table B to store the primary key value of another table

er02

  • Entity A is one-to-many to entity B: Create a field in table B to store the primary key value of table A

er02

  • Entity A is many-to-many for entity B: Create a new table C, this table has only two fields, one is used to store the primary key value of A, and the other is used to store the primary key value of B.

er03

Tombstone

  • For important data, I don’t want to delete it physically. Once deleted, the data cannot be retrieved.
  • Delete scheme: set the column of isDelete, the type is bit, which means logical deletion, the default value is 0
  • For non-important data, it can be physically deleted
  • The importance of data should be decided based on actual development

Extended reading

Guess you like

Origin blog.csdn.net/weixin_42250835/article/details/90454674