Detailed MySQL column type


   In MySQL, the so-called building table is the process of declaring columns. The data is placed on the hard disk in the form of files, and some are placed in the memory. The space occupied by different columns is different. The principle of selecting columns is sufficient and not wasteful.

Numeric

Integer

   Integer types include 5 column types: tinyint, smallint, mediumint, int, bigint.

Occupy space and storage range
Types of byte Rank Unsigned Signed
tinyint 1 8 0~(2^8-1) -2^7~ +(2^7-1)
smallint 2 16 0~(2^16-1) -2^15~ +(2^15-1)
mediumint 3 24 0~(2^24-1) -2^23~ +(2^23-1)
int 4 32 0~(2^32-1) -2^31~ +(2^31-1)
bigint 8 64 0~(2^64-1) -2^63~ +(2^63-1)

   Taking tinyint as an example, it occupies 1 byte (8 bits) of space on the hard disk, and the unsigned storage range is 2 ^ 8-1. If it represents a negative number, the highest bit can be used to mark the sign bit. -2 ^ 7 ~ + (2 ^ 7-1) (only the last 7 digits represent the absolute value, the highest digit represents the sign bit) (complement)
   tinyint is signed by default, the storage is -2 ^ 7 ~ + ( 2 ^ 7-1)
   optional attribute of integer column tinyint (M) unsigned zerofill; M: width (only meaningful when filled with 0), unsigned: unsigned type (non-negative), zerofill: 0 filled (default no Symbol); if a column is defined as zerofill, the default is unsigned.

Floating point / fixed point

   Floating point type: float (M, D)
   Fixed point type: decimal (M, D)
   M: precision (total digits, excluding decimal point), D: scale (decimal digits), float (6,2) as an example, It can store numbers in the range of -9999.99 ~ 9999.99, and the range of values ​​that can be taken is different from shaping, which is special. float (4,2) value range: -99.99 ~ 99.99, float (4,2) unsigned value range: 0.00 ~ 99.99.

Character

Occupy space and storage range
Types of Explanation Typical declaration range
char Fixed-length string gender char(1) char (M), M ranges from 0 ~ 2 ^ 8-1
varchar Variable length string email varchar(20) varchar (M), M ranges from 0 ~ 2 ^ 16-1, about 2W ~ 6W characters are affected by the character set
text++ Text string content text About 2W ~ 6W characters are affected by the character set

    Char is fixed length, char (M), M stands for width, the number of characters that can be accommodated.
   varchar becomes longer, varchar (M), M stands for width, the number of characters that can be accommodated.
   Difference:
   Space utilization efficiency:
   char fixed length M characters, if stored less than M characters, actually occupy M characters, if there are not enough M characters filled with spaces inside, spaces are lost when reading; varchar: variable length, M Characters, less than M characters are stored, set to N, N <= M, actually occupy N characters.
   Operational speed: fixed length is faster.
   The choice of char and varchar considers both space utilization efficiency and operation speed.

Date and time type (quotation marks are required when inserting)

Occupy space and storage range
Types of range
year 1901 ~ 2155, if you input two digits, '00 ~ 69 'means 2000 ~ 2069, and '70 ~ 00' means 1970 ~ 1999
date (year-month-day) 1000-01-01 ~ 9999-12-31 (AD)
time -838:59:59~+838:59:59
datetime (year-month-day hour: minute: second) 1000-01-01 00: 00: 00 ~ 9999-12-31 23:59:59 (AD)

   Date and time types are rarely used to represent a column that needs to be accurate to the second, because although the date and time type can be accurate to the second and easy to view, it is inconvenient to calculate. You can use a timestamp to represent a column that is accurate to the second. The timestamp is stored in int, which is the number of seconds from 1970-01-01 00:00:00 to the current.
   For example, generally store the registration time of the product release time, etc., instead of using datetime storage, but the timestamp, because datetime is intuitive, but the calculation is inconvenient, and int is used to store the timestamp, which is convenient for calculation and formatting .

Published 59 original articles · won 20 · views 3623

Guess you like

Origin blog.csdn.net/qq_34896730/article/details/105169102