当前位置:网站首页>Summary of the most complete MySQL data types in history (Part 2)

Summary of the most complete MySQL data types in history (Part 2)

2022-07-25 01:12:00 51CTO

Then continue to sort out the above , Above we wrote about ​​time​​​ type , Then we'll write the date and time type ​​date​​ type .

date type

​date​​ Type is used when only date values are required , There is no time part , When storing, you need 3 byte .

The date format is ​​YYYY-MM-DD​​​. among ​​YYYY​​​ Indicate year ,​​MM​​​ Represents the month ,​​DD​​ Means day .

(1) With 'YYYY-MM-DD' perhaps 'YYYYMMDD' Date in string format , The value range is '1000-01-01' ~ '9999-12-3' ; For example, the input '2022-12-31' perhaps ‘20221231’, The date when the database is inserted is 2022-12-31;

(2) With ‘YY-MM-DD’ perhaps ‘YYMMDD’ Date in string format , ad locum ‘YY’ Represents the annual value of two digits . Dates with two digit year values can be confusing , Because I don't know the century .​​mysql​​​ Use the following rules to interpret two digit year values :‘00~69’ The annual value of the range is converted to ​​2000-2069​​​,'70-99' The annual value of the range is converted to ​​1970~1999​​.

(3) With YY-MM-DD perhaps YYMMDD Date in numeric format , Similar to the front ,00~69 The annual value of the range is converted to 2000~2069,70~99 The annual value of the range is converted to 1970~1999.

(4) Use ​​CURRENT_DATE​​​ perhaps ​​NOW()​​, Insert current system date .

for instance :

Create a file called ​​yunweijia_4​​ Data sheet for ;

      
      
mysql> create table yunweijia_4 (d date);
Query OK, 0 rows affected (0.03 sec)

mysql>
  • 1.
  • 2.
  • 3.
  • 4.

Insert some values into the table ;

      
      
mysql> insert into yunweijia_4 values ('1999-09-09'), ('19990909'), ('20221010');
Query OK, 3 rows affected (0.01 sec)
Records: 3 Duplicates: 0 Warnings: 0

mysql>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

Then check the results :

      
      
mysql> select * from yunweijia_4;
+------------+
| d |
+------------+
| 1999-09-09 |
| 1999-09-09 |
| 2022-10-10 |
+------------+
3 rows in set (0.00 sec)

mysql>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.

Another example :

Delete data table ​​yunweijia_4​​ Data in ;

      
      
delete from yunweijia_4;
  • 1.

Insert data into the table ;

      
      
mysql> insert into yunweijia_4 values ('99-09-09'), ('990909'), ('000101'), ('111111');
Query OK, 4 rows affected (0.01 sec)
Records: 4 Duplicates: 0 Warnings: 0

mysql>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

Check the results :

      
      
mysql> select * from yunweijia_4;
+------------+
| d |
+------------+
| 1999-09-09 |
| 1999-09-09 |
| 2000-01-01 |
| 2011-11-11 |
+------------+
4 rows in set (0.00 sec)

mysql>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

Another example :

Old rules , First delete the data in the table

      
      
mysql> delete from yunweijia_4;
Query OK, 4 rows affected (0.01 sec)

mysql>
  • 1.
  • 2.
  • 3.
  • 4.

Insert data into table ;

      
      
mysql> insert into yunweijia_4 values (19990909), (990909), (000101), (111111);
Query OK, 4 rows affected (0.01 sec)
Records: 4 Duplicates: 0 Warnings: 0

mysql>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

Check the results :

      
      
mysql> select * from yunweijia_4;
+------------+
| d |
+------------+
| 1999-09-09 |
| 1999-09-09 |
| 2000-01-01 |
| 2011-11-11 |
+------------+
4 rows in set (0.00 sec)

mysql>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

Another example :

Delete data in table ;

      
      
mysql> delete from yunweijia_4;
Query OK, 4 rows affected (0.01 sec)

mysql>
  • 1.
  • 2.
  • 3.
  • 4.

Insert the current time of the system :

      
      
mysql> insert into yunweijia_4 values (current_date()), (now());
Query OK, 2 rows affected, 1 warning (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 1

mysql>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

View results :

      
      
mysql> select * from yunweijia_4;
+------------+
| d |
+------------+
| 2022-07-09 |
| 2022-07-09 |
+------------+
2 rows in set (0.00 sec)

mysql>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.

​CURRENT_DATE​​​ Only the current date value is returned , Not including the time part ;​​NOW()​​ Function returns date and time values , When saving to the database , Only the date part is retained .

datetime type

​DATETIME​​​ Type is used for values that need to contain both date and time information , When storing, you need 8 byte . The date format is ‘YYYY-MM-DD HH:MM:SS’. among ,​​YYYY​​​ Indicate year ,​​MM​​​ Represents the month ,​​DD​​​ Means day ,​​HH​​​ For hours ,​​MM​​​ Represents minutes ,​​SS​​​ For seconds . In giving ​​DATETIME​​​ When assigning a field of type , You can insert data using either a string type or a number type , As long as meet ​​DATETIME​​ The date format can be .

(1) With ‘YYYY-MM-DD HH:MM:SS’ perhaps ‘YYYYMMDDHHMMSS’ Value in string format , The value range is ‘1000-01-01 00:00:00’~‘9999-12-3 23:59:59’;

(2) With ‘YY-MM-DD HH:MM:SS’ perhaps ‘YYMMDDHHMMSS’ Date in string format , ad locum YY Represents the annual value of two digits . Same as before ,‘00~69’ The annual value of the range is converted to ‘2000~2069’,‘70~99’ The annual value of the range is converted to ‘1970~1999’.

(3) With YYYYMMDDHHMMSS perhaps YYMMDDHHMMSS Date and time in numeric format .

TIMESTAMP type

​TIMESTAMP​​​ The display format is the same as ​​DATETIME​​​ identical , The display width is fixed at 19 Characters , The date format is ​​YYYY-MM-DD HH:MM:SS​​​, When storing, you need 4 byte .​​TIMESTAMP​​​ The value range of column is less than ​​DATETIME​​​ Value range of , by ‘1970-01-01 00:00:01’UTC~‘2038-01-1903:14:07’UTC. among ,​​UTC(Coordinated Universal Time)​​ For world standard time , So when inserting data , Ensure that it is within the legal value range .

Be careful :

because ​​datetime​​​ The type and ​​TIMESTAMP​​ It is quite different from the above usage , So here are no more examples .

Text string type

The string type is used to store string data , In addition to storing string data , Other data can also be stored , Such as binary data of pictures and sounds .

​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 .

stay ​​mysql​​ in , The text string type refers to :

  • CHAR
  • VARCHAR
  • TEXT
  • ENUM
  • SET

See the following table for details :

 The most complete in history mysql Data type summary ( Next )_ character string

​VARCHAR​​​ and ​​TEXT​​​ The type and ​​BLOB​​ All are Variable length type , Its storage requirements Depends on the actual length of the column value ( Use... In the previous table L Express ), Rather than depending on the maximum possible size of the type .

for example , One ​​VARCHAR(10)​​ The maximum length that the column can save is 10 Character string , The actual storage needs to be the length of the string L add 1 byte ( Record the length of the string ). For characters “abcd”,L yes 4 The storage requirements are 5 byte .

CHAR and VARCHAR type

​CHAR(M)​​​ Is a fixed length string , Specify the string column length when defining . Fill in the space on the right when saving , To reach the specified length .​​M​​​ Column length ,​​M​​ The range is 0~255 Characters .

for example :​​CAHR(4)​​​ Defines a fixed length character string , The maximum number of characters it contains is 4. When the retrieved ​​CHAR​​ When the value of , The trailing space will be deleted .

​VARCHAR(M)​​​ Is a variable length string ,​​M​​​ Indicates the maximum column length .​​M​​​ The range is 0~65535.​​VARCHAR​​ The maximum actual length of is determined by the size of the longest line and the character set used , The actual space occupied is the actual length of the string plus 1.

for example :​​VARCAHR(50)​​​ Defines a maximum length of 50 String , If the inserted string is only 10 Characters , Then the actual stored string is 10 Characters and a string ending character .​​VARCHAR​​ When the corresponding value is saved and retrieved, the trailing space remains .

Let's take a look at ​​CHAR(4)​​​ and ​​VARCHAR(4)​​ What is the difference between storage :

 The most complete in history mysql Data type summary ( Next )_mysql_02

We can see from the comparison results in the above figure ,​​CHAR(4)​​​ The fixed length is defined as 4 The column of , No matter how long the stored data is , The space occupied is 4 Bytes ;​​VARCHAR(4)​​ The number of bytes occupied by the defined column is the actual length plus 1;

But the query is different , We can look at it through the following example ;

for instance :

Create a file called ​​yunweijia_5​​ Data sheet for ;

      
      
mysql> create table yunweijia_5 (ch char(4), var varchar(4));
Query OK, 0 rows affected (0.03 sec)

mysql>
  • 1.
  • 2.
  • 3.
  • 4.

Insert some data into this data table ;

      
      
mysql> insert into yunweijia_5 values ('ab', 'ab');
Query OK, 1 row affected (0.01 sec)

mysql>
  • 1.
  • 2.
  • 3.
  • 4.

Check the results :

      
      
mysql> select concat ('(', ch, ')'), concat ('(', var, ')') from yunweijia_5;
+-----------------------+------------------------+
| concat ('(', ch, ')') | concat ('(', var, ')') |
+-----------------------+------------------------+
| (abc) | (abc) |
+-----------------------+------------------------+
1 row in set (0.00 sec)

mysql>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.

There's a problem here , The document above describes ,​​char​​​ The space will be deleted when saving ,​​varchar​​​ The space at the end will be reserved , But from the perspective of its own practice , Whether it's ​​char​​​ still ​​varchar​​ Have deleted the space at the end .

TEXT type

​TEXT​​ Column holds non binary strings , Such as the content of the article 、 Comments, etc .

When saving or querying ​​TEXT​​ Column value , Do not delete trailing spaces .

Text Types are divided into 4 Kind of :


For the rest, go to VX official account “ Operation and maintenance home ” , reply “196” see .


------ “ Operation and maintenance home ” , reply “196”  ------

------ “ Operation and maintenance home ” , reply “196”  ------

------ “ Operation and maintenance home ” , reply “196”  ------


Weifang operation and Maintenance Engineer Recruitment , The development prospect of hardware operation and maintenance engineers , Personal planning of operation and maintenance engineer , Shenzhen Sunshine rain dew operation and maintenance engineer , Public institutions recruit operation and maintenance engineers ;

Qianxin operation and Maintenance Engineer Interview , Sany Heavy Industry system operation and maintenance engineer , Operation and maintenance engineer interview questions , Operation and maintenance engineer of Environmental Engineering ;

What direction do operation and maintenance engineers have , Hangzhou operation and maintenance engineer recruits Ali , Bridge operation and maintenance engineer , Desktop operation and maintenance engineer training plan .


原网站

版权声明
本文为[51CTO]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/202/202207201219592435.html