8
头图

图怪兽_9ef789cda64e9ae362106336d3fb4f69_18188.jpg

Preface

We often hear about paradigm design and anti-paradigm design, so when should we follow paradigm design? When should he violate his rules? With these two questions, let me take you to study now~

Paradigm

image.png
The paradigm is given by the predecessors through continuous verification. In order to establish a database with less redundancy and a reasonable structure, it is a certain rule that must be followed in the design of the database. In a relational database, this specification is called a paradigm. This article not only explains the design of the paradigm, but also Some examples will be given, and the data table design given by the analysis with you will belong to the first paradigm.

1NF First Normal Form

image.png
Rules to follow: each column of attributes is an indivisible attribute value to ensure the atomicity of each column

is no longer separable? For example, user name, gender, and age contact information can no longer be divided.
But for example, the city, Tongzhou District, Beijing, we can still subdivide a field into provinces: Beijing District: Tongzhou District

First, we see that in the user information table, the city where it is located can be subdivided.
codingusernameagegenderContact informationCity
1Zhang San18male1888888888Qingdao, Shandong
2Li Si17Female1999999999Wuhan, Hubei
3Wang Wu22male1777777777Qingdao, Shandong
In the following table, we divide the city into two fields, province and city, according to the above user information table. After this design, the data table meets the requirements of the first normal form.
User codeusernameagegenderContact informationProvinceCity
1Zhang San18male1888888888Shandong ProvinceQingdao City
2Li Si17Female1999999999Hubei ProvinceWuhan
3Wang Wu22male1777777777Shandong ProvinceQingdao City

Let's make an explanation with the product list

codingproduct namepricein stockManufacturing plant
1iPhone 13 white1999100Foxconn Taiwan
2iPhone 12 black129920Beijing Foxconn
3iPhone 13 red223930Shanghai Inventec

Still in accordance with the above rules, first we find the attributes that can no longer be divided

Now the indivisible attributes include code, price (only RMB), inventory, and the name of the product and the manufacturing plant must be known to everyone. This is an attribute that can be subdivided, then look at the data table that meets the first normal form below. design

codingbrandmodelcolourpricein stockManufacturing plant (location city)Manufacturing plant name
1iPhone13White1999100TaiwanFoxconn
2iPhone12black129920BeijingFoxconn
3iPhone13Red223930ShanghaiInventec

We split the product name from the manufacturing plant as follows to achieve the indivisible attribute

relationship mode:

Commodity name (brand, model, color)
Manufacturing plant (city where manufacturing plant is located, name of manufacturing plant)


Seeing this, you must have understood the design rules of the first paradigm, so let's continue the study of the second paradigm.

2NF Second Normal Form

image.png

Rules to follow: First, it must meet the requirements of the first normal form. Secondly, only one type of data can be stored in a table. Make sure that each column is related to the primary key. We use the course code as the primary key. Other information except the course name has nothing to do with the primary key, so We only need independent course codes and course names, and create a new table for the others.

Course CodeCourse TitleInstructorTeacher sexTeacher contact
10001C++ programmingZhang Sanmale1888888888
10002data structureLi SiFemale1999999999
10003operating systemWang Wumale1777777777

Course Schedule

Course CodeCourse Title
10001C++ programming
10002data structure
10003operating system

teacher table

Course CodeInstructorTeacher sexTeacher contact
10001Zhang Sanmale1888888888
10002Li SiFemale1999999999
10003Wang Wumale1777777777

After this split, we meet the requirements of the second normal form, and also meet the requirements of the first normal form, and there is no subdivision attribute.


Let's take the above-mentioned commodity table that conforms to the first normal form as an example

codingbrandmodelcolourpricein stockManufacturing plant (location city)Manufacturing plant name
1iPhone13White1999100TaiwanFoxconn
2iPhone12black129920BeijingFoxconn
3iPhone13Red223930ShanghaiInventec

Here, code and brand are used as the combined primary key, and a new table is created for attributes that are not related to the primary key.

Manufacturer table

codingManufacturing plant (location city)Manufacturing plant name
1TaiwanFoxconn
2BeijingFoxconn
3ShanghaiInventec

Commodity table

codingbrandmodelcolourpricein stockManufacturer code
1iPhone13White19991001
2iPhone12black1299202
3iPhone13Red2239303

This meets the requirements of the second normal form.

We already have a general understanding of the second paradigm, don’t stop, the third paradigm is waiting for you!

3NF Third Normal Form

Rules to follow: First meet the requirements of the second paradigm, and there can be no transitive dependencies among other columns except the primary key column.

Let me first explain what is transitive dependency. Take the following order form as an example. We can find the product name through the order code, but we can also find the shipping warehouse and contact information through the store code. Both order code -> shipping warehouse, store code -> shipping warehouse, this will generate transitive dependency. The popular point is that if there are no non-primary keys related to attributes (shop code|sales code) except the primary key (order code) Warehouse), we can only find all the information through the primary key, to achieve the exclusive situation, which means that the next SQL statement can not make it valid, then the requirements of the third normal form have been met.

SELECT 发货仓,店铺电话 FROM 订单表 WHERE 商铺编码 = S1
Order codeproduct nameStore codeShipping warehouseShop phone
NS001Xiaomi mobile phoneS1A shipping warehouse in North China1888888888
NS002Samsung handphoneS2A cat's delivery warehouse in East China1999999999
NS003Huawei cell phoneS3A certain Huaxi shipping warehouse1777777777

Meet the requirements of the third normal form according to the following split

Order Form

Order codeproduct nameStore code
NS001Xiaomi mobile phoneS1
NS002Samsung handphoneS2
NS003Huawei cell phoneS3

shop table

Store codeShipping warehouseShop phone
S1A shipping warehouse in North China1888888888
S2A cat's delivery warehouse in East China1999999999
S3A certain Huaxi shipping warehouse1777777777

Just mentioned that the third normal form must meet the second normal form, and the first normal form must meet the first normal form, then let’s take a look at the order form that has just been dismantled.

  1. There are no more detachable attributes, which conforms to the first paradigm.
  2. There is no attribute irrelevant to the primary key, which conforms to the second paradigm.
  3. There is no transitive dependency and conforms to the third normal form.

Therefore, when you meet the third paradigm, you actually meet the first and second paradigms. Then we will end the three paradigms of relational databases. There may be some ambiguities in the data. Please understand the principles and don’t entangle them. thanks.

The purpose of anti-paradigm design

Of course, the paradigm is only the proposed specification, but in some special cases, we have to consider more points, such as

  1. Concurrent reading.
  2. Reduce the number of table associations.
  3. Redundant design of data sheet.
  4. Hot data and cold data.
  5. The non-real-time nature of the data.

Here is a comparison with the three major paradigms (personal opinion)

  • Reduce the number of table associations = violates the second normal form
  • Redundant design of data table = violation of third normal form

Of course, it is not a generalization. Here we take the order form as the guide to explain why we have to violate the rules of the paradigm.


image.png
Make a question here

After the user places an order, if the basic information of the product is associated with the table, what will happen if the product code of the order table is associated with the code of the product table?

Probably there will be such a problem:

image.png

After the user bought an HP printer for 1999 yuan, the merchant modified the current product and changed it to an ASUS notebook. Then through the table association (set to conform to the paradigm rule), will the user go crazy?

image.png

So how to solve this problem?

This uses the redundant design of the data I proposed above, and stores the basic information of the product directly in the order form. This is based on common sense. I will go to the mall and buy a watermelon for 20 yuan. The next day this Regardless of whether the price of watermelon increases or decreases, the mall will not ask you to make up the difference, and you cannot ask the mall to refund the difference (of course, this has nothing to do with the purchase of an expensive package, don’t compete).

image.png

So in the age of the Internet, at the moment you place an order, the information about the products you buy is already fixed, whether the merchant modifies the product, deletes the product, or the merchant shuts down and closes the store. The goods you buy will always be seen in your order. Of course, if the goods expire, after you click to enter, a treasure may give you a snapshot, which is still the data saved when you place the order.

You should be able to understand why anti-paradigm is needed. There are many similar examples. I have put forward similar requirements many times in my e-commerce design-related articles. So far, I will not list them one by one.

Of course, under normal circumstances, you still need to design your paradigm based on three main business, which is the industry norm, only a standardized uniform, you will not too "pit" You pick pot class people.


So what are the advantages of anti-paradigm design?

  1. Improve query speed.
  2. Avoid multi-table queries.
  3. Reduce the pressure on the database.

Problems that should be paid attention to in anti-paradigm

image.png

  1. Too much redundant data leads to excessive storage.
  2. If necessary, it needs to be implemented with a memory database.
  3. Violation of the paradigm rules, insert exceptions, query exceptions, etc. will occur.

Thanks

Thank you for seeing here, I hope this article can help you, thank you.
图怪兽_aca660f7aa40e1eb9fb2ce7d8b804a59_89437.jpg


CrazyCodes
16.9k 声望14.8k 粉丝

I am CrazyCodes,生命不息,编码不止。