当前位置:网站首页>How to choose the appropriate data type for fields in MySQL?
How to choose the appropriate data type for fields in MySQL?
2022-07-24 15:43:00 【51CTO】
In the previous two articles, we introduced many data types , Do you have eyelashes , We are
mysql When creating a data table in , Which data type should I choose ? This time, let's simply sort it out .
Rules
No matter how we create the data table , First of all, we need a rule , What criteria ?
That is to say , We want to ensure that the most accurate type should be used in any case , That is, in all types that can represent the value of the column , This type uses the least storage .

Integers and floating point numbers
If you don't need a fraction , Use integers to store data ; If necessary, the decimal part , Just use the floating point number type .
For floating-point data columns , The stored values will round the decimal places defined in the column .
What do you mean ? Come on , for instance :
Suppose the range of values of a column is 1~99999, If you use integers , be MEDIUMINT UNSIGNED It's the best type ; If you need to store decimals , Then we should use float Type .
Floating point types include float and DOUBLE type .DOUBLE The type accuracy is better than FLOAT Type high , So , When high storage accuracy is required, you should choose DOUBLE type .
Floating point and fixed point numbers
Floating point numbers FLOAT、DOUBLE Relative to the fixed number DECIMAL What are the advantages ?
The advantage is :
In the case of a certain length , Floating point numbers can represent a larger range of data .
Because floating point numbers are prone to error , Therefore, when the accuracy requirements are high , Fixed point number is recommended DECIMAL To store .
DECIMAL stay mysql Is stored as a string , It is used to define data with high accuracy requirements such as currency .
Date and time type
mysql There are many data types for different kinds of dates and times , such as year and time. If you only need to record the year , Then use year The type is enough ; If you only need to record the time , Then use time The type is enough .
If you need to record the date and time at the same time , Then we can use timestamp perhaps datetime type . because timestamp The value range of column is less than datetime Value range of . Therefore, it is best to use the date with a large storage range datetime type .
CHAR and VARCHAR Between the characteristics and choices
char and varchar The difference is as follows :
-
char It's a fixed length character ;varchar It's a variable length character -
char The trailing space of the inserted data will be deleted automatically ,carchar Does not remove trailing spaces
char It's a fixed length , So his processing speed is faster than varchar It's faster , But the opposite also has disadvantages , That is a waste of storage space , So more storage is not enough , But it can be used if there are requirements on speed char type , Instead, you can use varchar Type to implement .
ENUM and SET
ENUM Only single value can be taken , His data list is an enumerated collection . His list of legal values allows at most 65535 Members .
therefore , When you need to select one from multiple values , have access to ENUM.
such as , The gender field is suitable to be defined as ENUM type , It's only from “ male ” perhaps “ Woman ” Take a value from ;
SET You can take multiple values . His legal value can be at most 64 Members .
An empty string is also a legal SET value . When you need to take multiple values , Suitable for use set type , For example, if you want to store one's hobbies, you can use SET type , For example, one can enjoy playing basketball , You can also like reading books .
BLOB and TEXT
BLOB It's a binary string ,TEXT It's a non binary string , Both can store large amounts of information .BLOB It mainly stores pictures 、 Audio information, etc , and TEXT Only text files can be saved .
In this case , For example, it is a multimedia platform , We're going to use BLOB type , Otherwise, use TEXT The type is enough .
thus , In this paper, the end .
For more information, go to VX official account “ Operation and maintenance home ” , Get the latest article .
------ “ Operation and maintenance home ” ------
------ “ Operation and maintenance home ” ------
------ “ Operation and maintenance home ” ------
linux Interrupt exception ,linuxuml modeling ,linux Compile multiple source files ,linux How to update the time ,linux Paging video tutorial in ,
linux How to decompress gzip,linux Unzip a directory , How to close linux gateway ,linux Document structure diagram ,linux Improve the permission to execute ,
linux What mark is used in the document , Network security linux strengthening ,linux Check email in ,linux Change the background color ,linux Build a website under the environment .
边栏推荐
- YOLO5Face:为什么要重新发明人脸检测器
- Intuitive understanding of various normalization
- 【AdaptiveAvgPool3d】pytorch教程
- [adaptiveavgpool3d] pytorch tutorial
- Lsyncd搭建同步镜像-用Lsyncd实现本地和远程服务器之间实时同步
- 华为无线设备配置WAPI-证书安全策略
- 4279. Cartesian tree
- Is it safe for Huatai Securities to open a mobile account and will it be leaked
- Multus of kubernetes multi network card scheme_ CNI deployment and basic use
- 报错【项目报错】
猜你喜欢

遭受DDoS时,高防IP和高防CDN的选择

2022 robocom world robot developer competition - undergraduate group (provincial competition) -- fifth question tree and bipartite diagram (completed)

AttributeError: module ‘seaborn‘ has no attribute ‘histplot‘

微调LayoutLM v3进行票据数据的处理和内容识别

降噪蓝牙耳机哪个好?性价比最高的降噪蓝牙耳机排行

Istio1.12:安装和快速入门

华为无线设备配置WAPI-证书安全策略
![[machine learning basics] - another perspective to explain SVM](/img/da/3c379d225f9866ed1d3ca853920bd5.png)
[machine learning basics] - another perspective to explain SVM

Analysis of some difficulties in VAE (variational self encoder)

torch_ How to use scatter. Scatter() in detail
随机推荐
每天20分钟之feign
AttributeError: module ‘seaborn‘ has no attribute ‘histplot‘
Which is a good noise reduction Bluetooth headset? Ranking of the most cost-effective noise reduction Bluetooth headsets
Exomeiser annotates and prioritizes exome variants
Multus of kubernetes multi network card scheme_ CNI deployment and basic use
Introduction to single chip microcomputer: LED bidirectional water lamp
2022 RoboCom 世界机器人开发者大赛-本科组(省赛)---第一题 不要浪费金币 (已完结)
Introduction to single chip microcomputer: LED lights cycle to the left and turn on
yolov3 训练自己的数据集
YOLO5Face:为什么要重新发明人脸检测器
Still using listview? Use animatedlist to make list elements move
[machine learning basics] - another perspective to explain SVM
2022 robocom world robot developer competition - undergraduate group (provincial competition) rc-u4 strategy team (completed)
Dynamics 365: explain virtual entity from 0 to 1
4279. 笛卡尔树
自适应设计和响应式设计
2022 RoboCom 世界机器人开发者大赛-本科组(省赛) CAIP 完整版题解
Read the paper with me - multi model text recognition network
Nine key measures to maintain server security in Hong Kong
Is it safe for Huatai Securities to open a mobile account and will it be leaked