当前位置:网站首页>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
边栏推荐
- Monkey/ auto traverse test, integrate screen recording requirements
- [secretly kill little buddy pytorch20 days -day02- example of image data modeling process]
- Under the double reduction policy, research travel may become a big winner
- Capturing and sorting out external articles -- autoresponder, composer, statistics [III]
- 技术管理进阶——如何在面试中考察候选人并增大入职概率
- Read the root directory of the folder, write txt and generate random samples
- APEC industry +: father of the king of the ox mill, industrial Internet "king of the ox mill anti-wear faction" Valentine's Day greetings | Asia Pacific Economic media | ChinaBrand
- An expression that regularly matches one of two strings
- Discussion Net legacy application transformation
- Xai+ network security? Brandon University and others' latest "interpretable artificial intelligence in network security applications" overview, 33 page PDF describes its current situation, challenges,
猜你喜欢

gslb(global server load balance)技术的一点理解

Mysql database ----- common commands of database (based on database)

常用sql集合

Summary of common operation and maintenance commands

Such as the visual appeal of the live broadcast of NBA Finals, can you still see it like this?

Decompile and modify the non source exe or DLL with dnspy

TiDB 之 TiCDC6.0 初体验

Yyds dry inventory hcie security Day12: concept of supplementary package filtering and security policy

Hcie security Day12: supplement the concept of packet filtering and security policy

Xai+ network security? Brandon University and others' latest "interpretable artificial intelligence in network security applications" overview, 33 page PDF describes its current situation, challenges,
随机推荐
MySQL - idea connects to MySQL
技术管理进阶——如何在面试中考察候选人并增大入职概率
@Transactional注解失效的场景
90 後,辭職創業,說要卷死雲數據庫
Common SQL sets
Memory analyzer (MAT)
Let me ask you a question. Have you ever used the asynchronous io of Flink SQL to associate dimension tables in MySQL? I set various settings according to the official website
Hcie security Day11: preliminarily learn the concepts of firewall dual machine hot standby and vgmp
Install and use Chrony, and then build your own time server
What is the maximum number of concurrent TCP connections for a server? 65535?
Après 90 ans, j'ai démissionné pour démarrer une entreprise et j'ai dit que j'allais détruire la base de données Cloud.
Selenium has three waiting methods (forced waiting, implicit waiting, and display waiting)
Refer to some books for the distinction between blocking, non blocking and synchronous asynchronous
Collections SQL communes
[vulnhub shooting range] impulse: lupinone
Compilation Principle -- syntax analysis
Analyse de REF nerf
Volley source code analysis
Implementation principle of inheritance, encapsulation and polymorphism
MySQL——SQL注入问题