SQL Optimization Lecture (6)-Be kind to the database from the beginning of building tables

SQL column

Summary of SQL basics

SQL advanced knowledge summary

To build a table, I believe that all of you will be able to build a table by clicking the right mouse button, enter the required columns and types, name the table and save it. But have you ever thought about irregular table creation, what kind of situation would it be for the database?

1


Why build specification table

Standardizing the table creation helps to clarify the relationship between the table and the table, the relationship between the column and the column, and the specific role of the current library can be well understood.

The above is just the most basic, the deeper reason is that it can effectively improve the efficiency of table-to-table association or separate query.

Generally, complex logic is due to the unreasonable table design, and many tables need to be associated when the table is associated. Each time the association is performed, the database needs to spend a certain amount of time to find matching items. The more connected, the longer it will take.

2

How to standardize table building

A. Divide the table by subject

For example, students related to the subject can be created in the following way when building a table

CREATE TABLE STU.Students

(......)

Among them, STU represents a topic

B. The naming convention of tables and columns

The name of the table needs to be well-known when it is created, separated by _, use camel case (initial capitalization), and the length should not exceed 30.

For example, to create a student payment table, you can use the following methods:

CREATE TABLE STU.Students_Fee

(......)

The table name Students_Fee here is composed of the words students (Students) and fees (Fee), connected with _, the first letter of the word is capitalized, and consists of 12 characters.

C. Unified definition of field types

  • For the string type, if the length exceeds 20 lengths, it is generally defined as a VARCHAR type. If the number is less than 20, you can consider using the CHAR type, which can effectively improve the query efficiency.

  • Chinese fields generally use VARCHAR type, and the length depends on the situation.

  • The DECIMAL(m,n) type is used for the amount involved and higher precision, where m and n are determined according to the actual situation. DOUBLE and FLOAT types are not recommended

  • The judgment type can be directly replaced by the INT type, such as 0 for no, 1 for yes

  • No length is added to the shaping definition, such as using INT instead of INT(4)

  • Try not to use IMAGE and TEXT types

  • The storage time (accurate to the second) is recommended to use the TIMESTAMP type, because TIMESTAMP uses 4 bytes, and DATETIME uses 8 bytes

  • It is recommended that the field be defined as NOT NULL. Columns with NULL may invalidate the index.

D. Try to meet the third normal form

In fact, the role of the third normal form is to make the fields in the table store as few columns as possible and reduce redundancy. However, in actual development and production, many people basically build a watch based on their mood. Some tables can be redundant with several columns and the results are not redundant, and some tables need to be split into several tables but put all the fields in one large table. This is a taboo for table building.

If you don't know how to build a better table, it is recommended that the number of columns should not exceed 20. It is best to draw an ER sketch first, sharpen the knife and not cut the wood by mistake. After the logic is clarified, the table building is actually fast and efficient.

Today’s lesson is introduced here. If you still don’t understand or want to know about the reading and construction form, you can leave a message below.

Guess you like

Origin blog.51cto.com/15057820/2656474