MySQL data type described in: integer type, float type, string type, date type, other types of data

A, MySQL data types

It includes the following five categories:

Integer types: BIT, BOOL, TINY INT, SMALL INT, MEDIUM INT, INT, BIG INT

Floating-point type: FLOAT, DOUBLE, DECIMAL

String type: CHAR, VARCHAR, TINY TEXT, TEXT, MEDIUM TEXT, LONGTEXT, TINY BLOB, BLOB, MEDIUM BLOB, LONG BLOB

Date Type: Date, DateTime, TimeStamp, Time, Year

Other data types: BINARY, VARBINARY, ENUM, SET, Geometry, Point, MultiPoint, LineString, MultiLineString, Polygon, GeometryCollection etc.

 

1, integer

MySQL Data Types Meaning (Signed)
tinyint(m) A byte range (-128 to 127)
smallint(m) 2 byte range (-32768 to 32767)
mediumint(m) 3 byte range (~ -8388608 8388607)
int(m) 4 byte range (-2147483648 to 2147483647)
bigint(m) Range of 8 bytes (18 + -9.22 * 10 th)

Unsigned range if the increase, the maximum value is doubled, as tinyint unsigned ranges (0 to 256).

 int (m) where m is a SELECT query result set display width does not affect the actual value range, did not affect the width of the display, I do not know what is the use of this m.

 

2, floating point (float and double)

MySQL Data Types meaning
float(m,d) Total single precision floating point precision of eight bits (4 bytes) m number, d decimal places
double(m,d) The total 16-bit double precision floating point precision (8 bytes) m number, d decimal places

Field is defined as a set float (6,3), if a number is inserted 123.45678, actually stored in the database is 123.457, but also the total number of the actual subject, i.e., six. The integer portion of the maximum is three, if the number of inserted 12.123456, 12.1234 is stored, if inserted 12.12, storage is 12.1200.

 

3, fixed-point

Float stored in the database are approximations, and the fixed-point type stored in the database the exact value. 

decimal (m, d) Parameters m <65 is the total number, d <30 and d <m is a decimal.

 

4, character string (char, varchar, _text)

MySQL Data Types meaning
char(n) Fixed length, up to 255 characters
varchar(n) Fixed length, up to 65,535 characters.
tinytext Variable length, up to 255 characters
text Variable length, up to 65,535 characters.
mediumtext Variable length, a maximum power of 24 characters -1 2
longtext Variable length, up to 32 characters power of 2 -1

 

char和varchar:

1.char (n) is stored if the number of characters is less than n, it places the space up, and then remove the spaces of the query. Char type is stored so the end of the string can not have spaces, varchar limited thereto. 

2.char (n) fixed length, char (4) either into a few characters, will be 4 bytes, varchar the actual number of characters is stored in bytes + 1 (n <= 255) or 2 bytes (n> 255),

So varchar (4), into three characters will occupy 4 bytes. 

String search speed faster than 3.char type varchar type.
varchar and text: 

1.varchar can specify n, text can not be specified, are stored in the internal storage varchar + 1 bytes actual number of characters (n <= 255) or two bytes (n> 255), text is the actual number of characters +2 words

Section. 

2.text type can not have default values. 

3.varchar can create an index directly, text before creating the index you want to specify how many characters. varchar queries faster than text, in the case creates an index, text index seems to have no effect.

 

5, binary data (_Blob)

1._BLOB and _text storage in different ways, _TEXT stored as text, English storage case sensitive, and _Blob stored in binary, case insensitive.

2._BLOB data stored can only be read as a whole. 

3._TEXT can specify the character set, _BLO do not specify the character set.

 

6. Date Time Type

MySQL Data Types meaning
date Date '2008-12-2'
time Time '12: 25: 36 '
datetime Date Time "2008-12-2 22:06:44 '
timestamp Automatically store records modified

If a field is defined as a timestamp, this time to field data with other fields modify the auto-refresh time, so this type of field data can be stored this record was last modified.

 

Data types of attributes

 

MySQL keywords meaning
NULL Data columns contains a NULL value
NOT NULL NULL values ​​are not allowed data column
DEFAULT Defaults
PRIMARY KEY Primary key
AUTO_INCREMENT Auto-increment for integer types
UNSIGNED Unsigned
CHARACTER SET name Specify a character set

 

Two, MYSQL data type and length range

Each data type and byte length list:

type of data Byte length Range or usage
Bit 1 Unsigned [0,255], signed [-128,127], Tianyuan blog Note: BIT and BOOL boolean occupies 1 byte
TinyInt 1 Integer [0,255]
SmallInt 2 Unsigned [0,65535], signed [-32768, 32767]
MediumInt 3 Unsigned [0,2 ^ 24-1], signed [-2 ^ 23, 2 ^ 23-1]]
Int 4 Unsigned [0,2 ^ 32-1], signed [31,2 ^ -2 ^ 31-1]
BigInt 8 Unsigned [0,2 ^ 64-1], signed [-2 ^ 63, 2 ^ 63 -1]
Float(M,D) 4 Single-precision floating-point number. Day alert margin blog where D is the precision, if D <= 24 compared with the default FLOAT, if D> 24 will automatically be converted DOUBLE type.
Double(M,D) 8  Double-precision floating point.
Decimal(M,D) M + 1 or M + 2 未打包的浮点数,用法类似于FLOAT和DOUBLE,天缘博客提醒您如果在ASP中使用到Decimal数据类型,直接从数据库读出来的Decimal可能需要先转换成Float或Double类型后再进行运算。
Date 3 以YYYY-MM-DD的格式显示,比如:2009-07-19
Date Time 8 以YYYY-MM-DD HH:MM:SS的格式显示,比如:2009-07-19 11:22:30
TimeStamp 4 以YYYY-MM-DD的格式显示,比如:2009-07-19
Time 3 以HH:MM:SS的格式显示。比如:11:22:30
Year 1 以YYYY的格式显示。比如:2009
Char(M) M 定长字符串。
VarChar(M) M 变长字符串,要求M<=255
Binary(M) M 类似Char的二进制存储,特点是插入定长不足补0
VarBinary(M) M 类似VarChar的变长二进制存储,特点是定长不补0
Tiny Text Max:255 大小写不敏感
Text Max:64K 大小写不敏感
Medium Text Max:16M 大小写不敏感
Long Text Max:4G 大小写不敏感
TinyBlob Max:255 大小写敏感
Blob Max:64K 大小写敏感
MediumBlob Max:16M 大小写敏感
LongBlob Max:4G 大小写敏感
Enum 1或2 最大可达65535个不同的枚举值
Set 可达8 最大可达64个不同的值
Geometry    
Point    
LineString    
Polygon    
MultiPoint    
MultiLineString    
MultiPolygon    
GeometryCollection    

三、使用建议

1、在指定数据类型的时候一般是采用从小原则,比如能用TINY INT的最好就不用INT,能用FLOAT类型的就不用DOUBLE类型,这样会对MYSQL在运行效率上提高很大,尤其是大数据量测试条件下。

2、不需要把数据表设计的太过复杂,功能模块上区分或许对于后期的维护更为方便,慎重出现大杂烩数据表

3、数据表和字段的起名字也是一门学问

4、设计数据表结构之前请先想象一下是你的房间,或许结果会更加合理、高效

5、数据库的最后设计结果一定是效率和可扩展性的折中,偏向任何一方都是欠妥的

Guess you like

Origin www.cnblogs.com/peijz/p/12349225.html