This article mainly shares with you how to choose mysql field type. Hope it helps everyone.
Occupy as little storage space as possible
Integer type
##Time type (choose the accurate type)
Type | Meaning |
##time()
Hours, minutes and seconds |
|
datetime()
Year, Month, Day Hours, Minutes and Seconds |
| ##year()
Year |
| date()
年月日 |
| timestamp()
Time stamp (1970-1-1 to now) Seconds) |
|
It is best to integrate data with a fixed length
char (number of characters)
Fixed length , runs fast and takes up more storage
-
Length: 255 character limit
-
varchar (number of bytes)
The length is not fixed. The content is relatively small and needs to be operated. This type should reserve 1-2 bytes to save the length of the current data.
-
Length: 65535 words Section limit
-
To store Chinese characters, for example, the character set utf8 (each Chinese character occupies 3 bytes), can store up to 65535/3-2 bytes
-
Information is best stored as integer
时间信息可以存储为整型的(时间戳)
Copy after login
set集合类型 多选:set(‘篮球’,’足球’,’棒球’,’乒乓球’);enum枚举类型 单选: enum(‘男’,’女’,’保密’);
推荐使用set和enum类型,内部会通过整型信息参数具体计算、运行。
Copy after login
ip地址也可以变为整型信息进行存储(mysql内部有算法,把ip变为数字):
mysql: inet_aton(ip) inet_ntoa(数字)php: ip2long(ip) long2ip(数字)
Copy after login
Summary:
Principles for field type selection: small space occupied, fixed data length, and minimum data content Good for integer type
Related recommendations:
More detailed description of MySQL field types
The above is the detailed content of How to choose mysql field type. For more information, please follow other related articles on the PHP Chinese website!