当前位置:网站首页>Yyds dry inventory Chapter 4 of getting started with MySQL: data types that can be stored in the data table
Yyds dry inventory Chapter 4 of getting started with MySQL: data types that can be stored in the data table
2022-07-03 21:27:00 【getupos】
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
边栏推荐
- 2022-02-15 Daily: 2022 AAAI fellow release
- MySQL——SQL注入问题
- Software testing skills, JMeter stress testing tutorial, obtaining post request data in x-www-form-urlencoded format (24)
- Yiwen teaches you how to choose your own NFT trading market
- Global and Chinese market of recycled yarn 2022-2028: Research Report on technology, participants, trends, market size and share
- Global and Chinese market of gallic acid 2022-2028: Research Report on technology, participants, trends, market size and share
- Transformation between yaml, Jason and Dict
- For in, foreach, for of
- Monkey/ auto traverse test, integrate screen recording requirements
- @Scenario of transactional annotation invalidation
猜你喜欢
运维各常用命令总结
Talk about daily newspaper design - how to write a daily newspaper and what is the use of a daily newspaper?
Getting started with postman -- built-in dynamic parameters, custom parameters and assertions
Scientific research document management Zotero
MySQL——数据库备份
Go learning notes (4) basic types and statements (3)
Nacos common configuration
Link aggregation based on team mechanism
treevalue——Master Nested Data Like Tensor
(5) User login - services and processes - History Du touch date stat CP
随机推荐
Global and Chinese market of wireless hard disk 2022-2028: Research Report on technology, participants, trends, market size and share
Advanced technology management - how to examine candidates in the interview and increase the entry probability
请教大家一个问题,用人用过flink sql的异步io关联MySQL中的维表吗?我按照官网设置了各种
Redis data migration (II)
Notes on MySQL related knowledge points (startup, index)
What is the maximum number of concurrent TCP connections for a server? 65535?
treevalue——Master Nested Data Like Tensor
MySQL - database backup
Day 9 HomeWrok-ClassHierarchyAnalysis
使用dnSpy对无源码EXE或DLL进行反编译并且修改
MySQL -- standardize database design
上周内容回顾
Yiwen teaches you how to choose your own NFT trading market
Mysql database ----- common commands of database (based on database)
Global and Chinese market of recycled yarn 2022-2028: Research Report on technology, participants, trends, market size and share
Analyse de REF nerf
Discussion Net legacy application transformation
Leetcode daily question 540 A single element in an ordered array Valentine's Day special article looking for a single dog in a pile of lovers ~ the clown is myself
Persistence of Nacos
Global and Chinese market of AC induction motors 2022-2028: Research Report on technology, participants, trends, market size and share