当前位置:网站首页>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
边栏推荐
- Using tensorflow to realize voiceprint recognition
- Slf4j + Logback日志框架
- 来自数砖大佬的 130页 PPT 深入介绍 Apache Spark 3.2 & 3.3 新功能
- Detailed explanation of 'viewpager' in compose | developer said · dtalk
- Wechat applet basic learning (wxss)
- Program analysis and Optimization - 9 appendix XLA buffer assignment
- I've been interviewed. The starting salary is 16K
- Digital twin smart factory develops digital twin factory solutions
- Flexible combination of applications is a false proposition that has existed for 40 years
- Yolox enhanced feature extraction network panet analysis
猜你喜欢

Difference between NVIDIA n card and amda card

C MVC creates a view to get rid of the influence of layout

Flexible combination of applications is a false proposition that has existed for 40 years

2022 latest and complete interview questions for software testing
![Third party payment function test point [Hangzhou multi tester _ Wang Sir] [Hangzhou multi tester]](/img/d8/d22cbbaccb1594ee46aca098c41002.png)
Third party payment function test point [Hangzhou multi tester _ Wang Sir] [Hangzhou multi tester]
![[live broadcast appointment] database obcp certification comprehensive upgrade open class](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[live broadcast appointment] database obcp certification comprehensive upgrade open class

Maybe you read a fake Tianlong eight

Bean加载控制

来自数砖大佬的 130页 PPT 深入介绍 Apache Spark 3.2 & 3.3 新功能

Mapper代理开发
随机推荐
RuntimeError: no valid convolution algorithms available in CuDNN
Program analysis and Optimization - 9 appendix XLA buffer assignment
Why can't the start method be called repeatedly? But the run method can?
Judge whether the binary tree is full binary tree
Realization of mask recognition based on OpenCV
Maybe you read a fake Tianlong eight
接口差异测试——Diffy工具
How to maintain the brand influence of clothing enterprises
Interpretation of new plug-ins | how to enhance authentication capability with forward auth
Top Devops tool chain inventory
Agnosticism and practice makes perfect
ArrayList分析2 :Itr、ListIterator以及SubList中的坑
[OJ] intersection of two arrays (set, hash mapping...)
JSON数据传递参数
Fudian bank completes the digital upgrade | oceanbase database helps to layout the distributed architecture of the middle office
leetcode 650. 2 keys keyboard with only two keys (medium)
yolov5detect. Py comment
PR FAQ, what about PR preview video card?
cocospods 的使用
富滇银行完成数字化升级|OceanBase数据库助力布局分布式架构中台