Chapter 3 Multi-table Operation 1 (Building Table Three Paradigm)

1 Why do you need to save data with multiple tables

(1) Different tables in the database generally correspond to different entities, and different entities have different attributes. If all tables are summarized into one table, all attributes need to be collected together, no matter which entity has or not , And then when assigning an entity without this attribute, the assignment is empty , which seriously wastes disk space.

(2) Each table in the database stores certain records. If you summarize all the records into one table, the speed will be very slow when you query. When you query the database, you give it the table name, which is based on the table. Find this table by name, and then query the records one by one, but if they are all concentrated in one table, the records in this table will be n times the original records in the separate table. How long does it take to query? ?

That’s why the standard of the table is produced, the three paradigm of table building

 

2 Three paradigms of table building

 

The first paradigm: eliminate duplication in the group, that is, whether the information in other columns is stored in the column (the field cannot be divided) 

   such as:

1 The designed table must have a primary key

 For example, Zhang said that there are several fields name address. If there are several records in the table that are consistent, suppose there are two Wang Wu and the addresses are the same, there will be data redundancy.

The 2 columns are inseparable, and repeated.

For example, there is a table user with only one field to save data for userinfo. Zhang San, 23, Beijing. The problem is that it is inconvenient to query and maintain. It is recommended to use three fields to represent it. But actual problems, actual analysis.

 

 

The second paradigm: Eliminate partially dependent columns, that is, whether there are columns that depend on a part of the primary key (non-primary key fields are completely dependent on the primary key field) 

such as:

  

In this way, the product name, unit, product price and other information in the table are not related to the primary key of the table, but only related to the product number

The reasonable design is as follows:

    

 

The third paradigm: eliminate non-dependent columns, whether there are columns that rely on non-primary keys (eliminate transitive dependencies) 

  such as:

 In this way, when querying the order information, the customer number can be used to refer to the records in the customer information table, and there is no need to enter the content of the customer information in the order information table multiple times, which reduces data redundancy.

 

  to sum up:

It boils down to three sentences: 1NF: Fields are indivisible; 2NF: There is a primary key, non-primary key fields depend on the primary key; 3NF: Non-primary key fields cannot depend on each other; 

 

 

 

 

Guess you like

Origin blog.csdn.net/yipianfeng_ye/article/details/89888153