Database Design -- Field Selection

Database Design -- Field Selection

 

 

Why pay attention to field selection?

 

       In the database design process, we must adhere to the principle of sufficient use. If we blindly set the data field range to the maximum or default value, it will lead to a lot of waste of storage space. In the case of a particularly large amount of data, such a design principle will cause a serious waste of database space, and will also have a great impact on the execution efficiency of the database. So we have to be cautious and careful when doing database design.

 

 

 

 

Common data types

 

       Eight commonly used types such as int, float, double, decimal, varchar, char, date, and datetime.

 

 

 

 

string

 

  • When the data is stored in characters, and the length is a fixed interval value, you can consider using char for storage.
  • When the character length is unknown and the length changes significantly, it is best to use varchar for storage.

       However, no matter which field is used for storage, the initial length of the field should not be set to the maximum value, and the most appropriate length field should be stored according to business requirements.

 

 

 

 

Integer

 

    You can select the following data types according to your needs:

 

  • bigint : Integer data (all numbers) from -2^63 (-9223372036854775808) to 2^63-1 (9223372036854775807). The storage size is 8 bytes. 
  • int : Integer data (all numbers) from -2^31 (-2,147,483,648) to 2^31 – 1 (2,147,483,647). The storage size is 4 bytes.
  • smallint : Integer data from -2^15 (-32,768) to 2^15 – 1 (32,767). The storage size is 2 bytes. 
  • tinyint : Integer data from 0 to 255. The storage size is 1 byte. 

       In the database design process, try to use int as the field type, because int is the best in terms of storage space and execution speed among all data types. For example: if all data is stored in the business and the length is not particularly long, you can consider using int for storage, or you need to use a certain identification weight when sorting the data in the business, or you can use int for storage. But don't consciously design all fields as ints because int is efficient, and finally design the corresponding fields according to the specific needs of the business.

 

    bit

 

       An array that stores only one bit, usually used to store state, active\inactive, is_deleted

 

 

 

 

floating point

 

  • float(N): When the data you give is an integer, then it will be processed for you as an integer.
  • double(N): always deal with decimals, the default value is: 0.00 and the actual storage is 0, also my deposit and withdrawal currency is 12.00, the actual storage is 12.
  • decimal(N, M): N is the total length, M is the number of digits after the decimal point, truncate if more, or fill with 0 if less

       When it comes to the amount of money, if the precision requirement is not high, float can be used first, followed by double for storage. If the precision requirements are relatively high, it is better to use decimal to store, but its efficiency is not as efficient as float and double. Which one to use depends on the specific needs of the business.

 

 

 

 

time type

 

       Regarding the usage of date and datetime, it depends on the precise value of the time you want to store. If you only want to be accurate to the day, you can use date. If you want to be accurate to the second level, you need to use datetime. Sometimes when you use datetime but only store it in the date unit of days, 00:00:00 will be automatically added to the back of the data as second-level data.

 

 

 

 

question

 

What is the difference between int(5) and int(10)?

 

       In fact, 5 and 10 are actually only the difference in display length, that is, no matter what the value of int(x)x is, the value range of the stored number is the value range of the data type of int itself, and x is only the length of the data display. Of course, if the stored data exceeds this length, an error will still be reported.

 

 

What is the difference between varchar(10) and char(10)?

 

       After MYSQL5, 5, varchar(x)x is actually the length range of the storage field, that is, a character represents a length, whether the character is a Chinese character or a character is a length unit. Although varchar sets the length value, because varchar is a variable length type, that is, when the stored length is less than x, the actual storage space is not x but the actual stored character length + some marked space. Of course, if it exceeds the length of x, it will still report an error. The value of y in char(y) is the actual storage length of the storage space, and an error will be reported if this length is exceeded.

 

 

 

 

Required fields

 

       id,created_time,created_by,updated_time,updated_by,is_deleted

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326221300&siteId=291194637