Home  >  Article  >  Database  >  How to choose mysql field type

How to choose mysql field type

小云云
小云云Original
2018-03-15 10:40:551580browse

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

How to choose mysql field type

##Time type (choose the accurate type)

TypeMeaning##time()datetime()##year()Yeardate()年月日timestamp()Time stamp (1970-1-1 to now) Seconds)It is best to integrate data with a fixed length
Hours, minutes and seconds
Year, Month, Day Hours, Minutes and Seconds
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
  •  时间信息可以存储为整型的(时间戳)
    set集合类型 多选:set(‘篮球’,’足球’,’棒球’,’乒乓球’);enum枚举类型 单选: enum(‘男’,’女’,’保密’);
    推荐使用set和enum类型,内部会通过整型信息参数具体计算、运行。
    ip地址也可以变为整型信息进行存储(mysql内部有算法,把ip变为数字):
    mysql: inet_aton(ip)   inet_ntoa(数字)php:      ip2long(ip)       long2ip(数字)
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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn