当前位置:网站首页>Chapter 4 of getting started with MySQL: data types stored in data tables
Chapter 4 of getting started with MySQL: data types stored in data tables
2022-07-03 00:03:00 【Brother tortoise】
MySQL Support multiple data types , It can be roughly divided into three categories , They are numeric types 、 Date and time type 、 character string ( character ) type .
1.1 value type
MySQL Support all standards SQL Numerical data type . These types include strict numerical data types (INTEGER,
SMALLINT、TINYINT、MEDIUMINT and BIGINT), Approximate numerical data type (FLOAT、REAL and DOUBLE), And fixed-point number type (DECIMAL).
Be careful : keyword INT yes INTEGER A synonym for , keyword DEC yes DECIMAL
A synonym for .
MySQL Provide multiple integer types , Different data types provide different value ranges , The larger the range of values that can be stored , The more storage space it needs , Therefore, we should choose the appropriate data type according to the actual needs .
The following figure shows the storage requirements and value range of each integer type .
Type the name | explain | Storage demand byte | The value range of signed number | The value range of the unsigned number |
---|---|---|---|---|
TINYINT | Very small integers | 1 | 128~127 | 0~255 |
SMALLINT | Small integers | 2 | 32768~32767 | 0~65535 |
MEDIUMINT | An integer of medium size | 3 | -8388608~8388607 | 0~16777215 |
INT (INTEGER) | An integer of normal size | 4 | -2147483648~2147483647 | 0~4294967295 |
BIGINT | Large integer | 8 | 9223372036854775808~9223372036854775807 | 0~18446744073709551615 |
MSQL It supports the selection of the display width of integer values specified in parentheses behind keywords of this type ( for example INT(4)).INT(M) Medium M Indicates the maximum display width , The maximum effective display width is 4, It should be noted that , The display width is independent of the range of values contained in the storage size or type .
1.2 Floating point type
In real life, many situations need to store numerical values with decimal parts , This requires a floating point number type , Such as FLOAT and DOUBLE. among ,FLOAT It is a single precision floating-point number type ;DOUBLE Double precision floating-point number type . Floating point number type can be used (M,D) To express , among M It's called precision , The total number of digits ;D Is called a scale , Represents the number of decimal places . The following table shows the storage requirements and value range of each floating-point number type .
Type the name | Storage requirements / byte | Signed value range | The range of unsigned values |
---|---|---|---|
FLOAT | 4 | -3.402823466E+38~-1.175494351E-38 | 0 and 1.175494351E-38~3.402823466E+38 |
DOUBLE | 8 | -1.7976931348623157E+308~ -2.2250738585072014E-308 | 0 and 2.2250738585072014E-308~ 1.7976931348623157E+308 |
Be careful :M and D stay FLOAT and DOUBLE Is optional ,FLOAT and DOUBLE The type will be saved to the maximum precision supported by the hardware .
1.3 Fixed point number type
MySQL in , Except that floating-point numbers are used to represent decimals , You can also use fixed-point numbers to represent decimals , There is only one type of fixed-point number :DECIMAL. Fixed point number type can also be used (M,D) To express , among M It's called precision , The total number of digits ;D Is called a scale , Represents the number of decimal places .DECIMAL Default D The value is 0,M The value is 10. The following table shows the storage requirements and value range of fixed-point number types .
DECIMAL Different types of dry FLOAT and DECIMAL, DECIMAL It is actually stored as a string .DECIMAL The valid value range of is determined by M and D Value determination of . If you change M And fixed D, Then its value range will follow M Become bigger and change
1.4 Date and time type
MySQL in , The date and time type that represent the time value are DATETIME、DATA、TIMESTAMP,TIME and YEAR. for example , Just record the year information , It can be used only YEAR type . Each type has a legal value range .
Type the name | Date format | Date range | Storage requirements / byte |
---|---|---|---|
YEAR | YYYY | 1901~2155 | 1 |
TIME | HH:MM:SS | -838:59:59-838:59:59 | 3 |
DATE | YYYY-MM-DD | 1000-01-019999-12-31 | 3 |
DATETIME | YYYY-MM-DD HH:MM:SS | 1000-01-01 00:00:009999-12-3123:59:59 | 8 |
TIMESTAMP | YYYY-MM-DD HH:MM:SS | 1970-01-01 00:00:001~2038-01-19 03:14:07 | 4 |
1.5 String type
String type is used to store string data ,MySQL Support two types of string data : Text strings and binary strings . Text strings can be used for case sensitive or case insensitive string comparison , You can also search for pattern matching .MySQL The string type in refers to CHAR、VARCHAR、TINYTEXT、TEXT、MEDIUMTEXT、LONGTEXT,ENUM and SET. The following table lists them MySQL String data type in .
Type the name | explain | Storage requirements |
---|---|---|
CHAR M) | Fixed length non binary string | M byte ,1<=M<=255 |
VARCHAR(M) | Variable length non binary strings | L+1 byte , Here it is L<=M and 1<=M<=255 |
TINYTEXT | Very small non binary strings | L+1 byte , Here it is L<28 |
TEXT | Small non binary strings | L+2 byte , Here it is L<216 |
MEDIUMTEXT | Medium sized non binary strings | L+3 byte , Here it is L<224 |
LONGTEXT | Large non binary strings | L+4 byte , Here it is L<232 |
ENUM | Enumeration type , There can only be one enumeration string value | 1 or 2 byte , Depends on the number of enumeration values ( Maximum 65535) |
SET | A collection , String objects can have zero or more SET member | 1,2,3,4, or 8 byte , Depends on the number of members of the collection ( most 64 Members ) |
1.6 Data type selection
MySQL Provides a large number of data types , To optimize storage , Improve database performance , In any case, the most precise type should be used , 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 hold data ; If necessary, the decimal part , Floating point number type . For floating point data columns , The stored values will round the decimal places defined in the column . for example , If the range of values of the column is 1~99999, If you use integers , be MEDIUMINT UNSIGNED It's the best type ; If you need to store decimals , Then use FLOAT type .
Floating point types include FLOAT and DOUBLE type .DOUBLE The type accuracy is better than FLOAT Type high , therefore , If high storage accuracy is required , Choose DOUBLE type .Floating point and fixed point numbers
Floating point numbers FLOAT and DOUBLE Relative to the fixed number DECIMAL The advantage is : In the case of a certain length , Floating point numbers can represent a larger range of data . But because floating point numbers are prone to errors , Therefore, when the accuracy requirement is relatively high , It is recommended to use DECIMAL To store .DECIMAL stay MySQL Is stored as a string , Used to define currency and other data requiring high accuracy . In data migration ,FLOAT(M,D) Right or wrong SQL Definition , There may be problems with database migration , It's best not to use . in addition , It is also easy to have problems when subtracting and comparing two floating-point numbers , So in the calculation , Be careful . If you do a numerical comparison , Best use DECIMAL type .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 record the time , Just use TIME type . If you need to record the date and time at the same time , You 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.TIMESTAMP There is also a DATETIME Attributes that you don't have . By default , When a record is inserted but not specified TIMESTAMP When this column value ,MySQL Will be able to TIMESTAMP The column is set to the current time . Therefore, when it is necessary to insert the current time while inserting the record , Use TIMESTAMP It's convenient , in addition TIMESTAMP In space than DATETIME More effective .CHAR And VARCHAR Between the characteristics and choices CHAR and VARCHAR The difference is as follows :
(1)CHAR It's a fixed length character ,VARCHAR It's a variable length character ;
(2)CHAR The trailing space of the inserted data will be deleted automatically ,VARCHAR Does not remove trailing spaces ;
(3)CHAR It's a fixed length , So it's faster than VARCHAR It's faster , But its disadvantage is a waste of storage space . So it's not a big memory , But those with speed requirements can be used CHAR type ;
conversely , have access to VARCHAR Type to implement
边栏推荐
- I've been interviewed. The starting salary is 16K
- 返回二叉树中最大的二叉搜索子树的大小
- ArrayList分析2 :Itr、ListIterator以及SubList中的坑
- Integration of revolution and batch normalization
- All things work together, and I will review oceanbase's practice in government and enterprise industry
- 130 pages of PPT from the brick boss introduces the new features of Apache spark 3.2 & 3.3 in depth
- Intranet penetration | teach you how to conduct intranet penetration hand in hand
- Dishes launcher small green program and directory management (efficiency tool)
- How to maintain the brand influence of clothing enterprises
- Digital collection trading website domestic digital collection trading platform
猜你喜欢
How to apply for company email when registering in company email format?
Speech recognition Series 1: speech recognition overview
What are the projects of metauniverse and what are the companies of metauniverse
Remote connection of raspberry pie by VNC viewer
Additional: token; (don't read until you finish writing...)
Highly available cluster (HAC)
The privatization deployment of SaaS services is the most efficient | cloud efficiency engineer points north
Explain in detail the process of realizing Chinese text classification by CNN
JDBC practice cases
How much do you know about synchronized?
随机推荐
Open Source | Wenxin Big Model Ernie Tiny Lightweight Technology, Accurate and Fast, full Open Effect
yolov5train. py
The privatization deployment of SaaS services is the most efficient | cloud efficiency engineer points north
[ml] Li Hongyi III: gradient descent & Classification (Gaussian distribution)
Mapper agent development
接口自动化覆盖率统计——Jacoco使用
Fusion de la conversion et de la normalisation des lots
ArrayList analysis 2: pits in ITR, listiterator, and sublist
容器运行时分析
Analyze ad654: Marketing Analytics
Returns the root node of the largest binary search subtree in a binary tree
Fudian bank completes the digital upgrade | oceanbase database helps to layout the distributed architecture of the middle office
[Verilog tutorial]
130 pages of PPT from the brick boss introduces the new features of Apache spark 3.2 & 3.3 in depth
【ML】李宏毅三:梯度下降&分类(高斯分布)
How much do you know about synchronized?
Request and response
开发知识点
[live broadcast appointment] database obcp certification comprehensive upgrade open class
Many to one, one to many processing