05_MySQL data type

Introduction to data types

  1. The data table is composed of multiple column fields, and each field specifies a different data type. After the data type is specified, the data content to be inserted into the field is determined.
  2. Different data types also determine the way MySQL uses them when storing them, and what arithmetic symbols are selected for calculations when using them.
  3. Numerical data types: TINYINT, SMALLINT, MEDIUMINT, INT, BIGINT, FLOAT, DOUBLE, DECIMAL
  4. Date/time type: YEAR, TIME, DATE, DATETIME, TIMESTAMP
  5. String type: CHAR, VARCHAR, BINARY, VARBINARY, BLOB, TEXT, ENUM, SET

1. Numerical type

  1. Numerical value types are mainly used to store numbers. Different value types provide different value ranges. The larger the value range that can be stored, the larger the storage space required.
  2. Numerical types are divided into: integer type, floating-point number type, fixed-point number type
Integer type
type name Description Storage requirements Signed value range Unsigned value range
TINYINT Very small integer 1 byte -128~127 0~255
SMALLINT Small integer 2 bytes -132768~32767 0~65535
MEDIUMINT Medium integer 3 bytes -8388608 ~ 8388607 0 ~ 16777215
INT Ordinary size integer 4 bytes -2147483648 ~ 2147483647 0 ~ 4294967295
TINYINT Very small integer 1 byte -128~127 0~255
BIGINT Large integer 8 bytes -9223372036854775808 ~9223372036854775807 0~18446744073709551615

//The default is a signed value range. To create an unsigned value range, you only need to add the UNSIGNEDfollowing when creating :

mysql> create table t1
    -> ( a tinyint UNSIGNED
    -> );
Query OK, 0 rows affected (0.01 sec)

mysql> insert into t1 values(255); //插入最大值为255
Query OK, 1 row affected (0.00 sec)

// zero paddingZEROFILL

mysql> alter table t1 modify a int(3) ZEROFILL; //少于三位在前面自动补零
Query OK, 3 rows affected (0.01 sec)
Records: 3  Duplicates: 0  Warnings: 0

//插入数据验证
mysql> insert into t1 values (1),(22);
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select * from t1;
+------------+
| a          |
+------------+
|        255 |
|        001 |
|        022 |
+------------+
5 rows in set (0.00 sec)

Floating point number type and fixed point number type

  1. MySQL uses floating-point numbers and fixed-point numbers to represent decimals. There are two types of floating-point numbers: single-precision floating-point numbers (FLOAT) and double-precision floating-point numbers (DOUBLE), fixed-point numbers only (DECIMAL).
  2. Both floating-point and fixed-point numbers can be represented by (M, N), where M is the precision, which means the total number of digits, and N is the scale, which means the number of digits after the decimal point.
  3. DECIMAL is actually stored in the form of a string. It is better to use the DECIMAL type when the accuracy requirements are relatively high (such as currency, scientific data, etc.).
  4. The advantage of floating-point numbers over fixed-point numbers is that with a certain length, floating-point numbers can represent a larger range of data. Its disadvantage is that it will cause precision problems.
type name Description Storage requirements Signed value range Unsigned value range
FLOAT Single precision floating point 4 bytes -3.402823466E + 38 ~ -1.175494351E-38 0 and -1.175494351E-38~-3.402823466E+38
DOUBLE Double-precision floating-point number 8 bytes -1.7976931348623157E + 308 -2.2250738585072014E-308 0 and -2.2250738585072014E-308~-1.7976931348623157E+308
DECIMAL Compressed "strict" fixed-point number M+2 bytes Not fixed Not fixed

//Set the total number of digits to 7, and the number of digits after the decimal point to 3

mysql> create table t5 ( a float(7,3) );
Query OK, 0 rows affected (0.01 sec)

//Insert verification

mysql> insert into t5 values(1234.567);
Query OK, 1 row affected (0.00 sec)

mysql> insert into t5 values(1234.5679);
Query OK, 1 row affected (0.00 sec)

mysql> select * from t5;
+----------+
| a        |
+----------+
| 1234.567 |
| 1234.568 |
+----------+
2 rows in set (0.00 sec)

//Single precision and double precision will cause precision problems! ! But fixed-point will not

mysql> alter table t5  modify a decimal(7,3);
Query OK, 2 rows affected (0.02 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> insert into t5 values(1111+1111.11);
Query OK, 1 row affected (0.00 sec)

mysql> select * from t5;
+----------+
| a        |
+----------+
| 1235.000 |
| 1235.000 |
| 1111.111 |
| 1111.123 |
| 1111.123 |
| 1111.124 |
| 2222.110 |
+----------+
7 rows in set (0.00 sec)

Two, MySQL date/time type

  1. MySQL has a variety of data types that represent dates. For example, when you only record year information, you can use the YEAR type instead of the DATE type.
  2. Each type has a legal value range. When an illegal value is specified, the system inserts the "zero" value into the database.
Type command Date format Date range Storage requirements
YEAR YYYY 1901~2155 1 byte
TIME HH:MM:SS -838:59:59 ~ 838:59:59 3 bytes
DATE YYYY-MM-DD 1000-01-01 ~ 9999-12-31 3 bytes
DATETIME YYYY-MM-DD HH:MM:SS 1000-01-01 00:00:00 ~ 9999-12-31 23:59:59 8 bytes
TIMESTAMP YYYY-MM-DD HH:MM:SS 1970-01-01 00:00:01 UTC ~ 2038-01-19 03:14:07UTC 4 bytes

1. YEAR (year)

  1. Format: YEAR expressed in 4-digit string format, the range is '1901' ~ '2155'
  2. Format: YEAR expressed in 4-digit format, ranging from 1901 to 2155
  3. Format: expressed in a 2-digit string format, the range is '00' ~ '99', where '00' ~ '69' is converted to 2000 ~ 2069, and '70' ~ '99' is converted to 1970 ~ 1999
  4. Format: expressed in 2 digit format, 1 ~ 69 is converted to 2001 ~ 2069, 70 ~ 99 is converted to 1970 ~ 1999

Example:
//Expressed in a 4-character number format

mysql> create table t6 (a year);
Query OK, 0 rows affected (0.01 sec)

mysql> insert into t6  values(2020);
Query OK, 1 row affected (0.00 sec)

mysql> select * from t6;
+------+
| a    |
+------+
| 2020 |
+------+
1 row in set (0.00 sec)

//Expressed in 4-character string format

mysql> insert into t6 values('2023');
Query OK, 1 row affected (0.00 sec)

mysql> select * from t6;
+------+
| a    |
+------+
| 2020 |
| 2023 |
+------+
2 rows in set (0.00 sec)

//Expressed as a 2-character string and a number.
Note: '00' ~ '69' are converted to 2000 ~ 2069, and '70' ~ '99' are converted to 1970 ~ 1999

mysql> insert into t6 values('69'),(70);  //69字符串格式 70数字格式
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select * from t6;
+------+
| a    |
+------+
| 2020 |
| 2023 |
| 2069 |
| 1970 |
+------+
4 rows in set (0.00 sec)

2. TIME (hours, minutes and seconds)

  1. The format of TIME type is HH:MM:SS, HH means hour, MM means minute, SS means second
  2. Format: TIME expressed in the format of'HHMMSS', for example, '101112' is understood as 10:11:12
  3. Format: TIME expressed in the string format of'D HH:MM:SS', where D represents the day and can take a value between 0 and 34. When inserting into the database, D will be converted into hours, 24 hours a day, such as '2 10:10' is represented in the database as: 58:10:00, which is 2x24+10=58

Example:
//Insert in normal format

mysql> create table t7 (a time);
Query OK, 0 rows affected (0.01 sec)
mysql> insert into t7 values('13:46:32');
Query OK, 1 row affected (0.00 sec)

mysql> select * from t7;
+----------+
| a        |
+----------+
| 13:46:32 |
+----------+
1 row in set (0.00 sec)
=

//Insert in the format of'HHMMSS', for example, '101112' is understood as 10:11:12

mysql> insert into t7 values(123454);
Query OK, 1 row affected (0.00 sec)

mysql> select * from t7;
+----------+
| a        |
+----------+
| 13:46:32 |
| 12:34:54 |
+----------+
2 rows in set (0.00 sec)

//Insert in the string format of'D HH:MM:SS' (2x24+10=58)

mysql> insert into t7 values('2 10:10:33');
Query OK, 1 row affected (0.00 sec)

mysql> select * from t7;
+----------+
| a        |
+----------+
| 13:46:32 |
| 12:34:54 |
| 58:10:33 |
+----------+

3, DATE (date)

  1. The format of the DATE type is YYYY-MM-DD, where YYYY represents the year, MM represents the month, and DD represents the day.
  2. Format:'YYYY-MM-DD' or'YYYYMMDD', the value range is '1000-01-01' ~ '9999-12-3'
  3. Format:'YY-MM-DD' or'YYMMDD', where YY represents the two-digit year value, and the range is '00' ~ '99', where '00' ~ '69' is converted to 2000 ~ 2069,' 70' ~ '99' are converted to 1970 ~ 1999
  4. Format: YY-MM-DD or YYMMDD, the date expressed in digital format, where the YY range is 00 ~ 99, where 00 ~ 69 is converted to 2000 ~ 2069, and 70 ~ 99 is converted to 1970 ~ 1999

Example:
//Insert in normal format

mysql> create table t8 (a date);
Query OK, 0 rows affected (0.00 sec)

mysql> insert into t8 values('2008-12-12');
Query OK, 1 row affected (0.00 sec)

mysql> select * from t8;
+------------+
| a          |
+------------+
| 2008-12-12 |
+------------+
1 row in set (0.00 sec)

//YYYYMMDD format insert

mysql> insert into t8 values(20070102);
Query OK, 1 row affected (0.01 sec)

mysql> select * from t8;
+------------+
| a          |
+------------+
| 2008-12-12 |
| 2007-01-02 |
+------------+

//'YY-MM-DD' or'YYMMDD' format insert

mysql> insert into t8 values('69-12-31');
Query OK, 1 row affected (0.00 sec)

mysql> select * from t8;
+------------+
| a          |
+------------+
| 2008-12-12 |
| 2007-01-02 |
| 2069-12-31 |
+------------+

or

mysql> insert into t8 values('701231');
Query OK, 1 row affected (0.00 sec)

mysql> select * from t8;
+------------+
| a          |
+------------+
| 2008-12-12 |
| 2007-01-02 |
| 2069-12-31 |
| 1970-12-31 |
+------------+

4. DATETIME (year, month, day, hour, minute, second)

  1. The format of the DATETIME type is YYYY-MM-DD HH:MM:SS, where YYYY represents the year, MM represents the month, DD represents the day, HH represents the hour, MM represents the minute, and SS represents the second
  2. Format:'YYYY-MM-DD HH:MM:SS' or'YYYYMMDDHHMMSS', string format, the value range is '1000-01-0100:00:00' ~ '9999-12-31 23:59:59 '
  3. Format:'YY-MM-DD HH:MM:SS' or'YYMMDDHHMMSS', string format, where the YY range is '00' ~ '99', where '00' ~ '69' is converted to 2000 ~ 2069 , '70' ~ '99' are converted to 1970 ~ 1999
  4. Format: YYYYMMDDHHMMSS or YYMMDDHHMMSS, number format, the value range is the same as above

Example
// Normal format insertion

mysql> create table t9 (a datetime);
Query OK, 0 rows affected (0.01 sec)

mysql> insert into t9 values('1999-12-02 12:30:44');
Query OK, 1 row affected (0.00 sec)

mysql> select * from t9;
+---------------------+
| a                   |
+---------------------+
| 1999-12-02 12:30:44 |
+---------------------+
1 row in set (0.00 sec)

//'YYYYMMDDHHMMSS' format insert

mysql> insert into t9 values('19991202123044');
Query OK, 1 row affected (0.00 sec)

mysql> select * from t9;
+---------------------+
| a                   |
+---------------------+
| 1999-12-02 12:30:44 |
| 1999-12-02 12:30:44 |
+---------------------+
2 rows in set (0.00 sec)

//'YY-MM-DD HH:MM:SS' or'YYMMDDHHMMSS' format insert

mysql> insert into t9 values('691202123044');
Query OK, 1 row affected (0.00 sec)
或
mysql> insert into t9 values('69-12-02 12:30:44');
Query OK, 1 row affected (0.00 sec)

mysql> select * from t9;
+---------------------+
| a                   |
+---------------------+
| 1999-12-02 12:30:44 |
| 1999-12-02 12:30:44 |
| 2069-12-02 12:30:44 |
| 2069-12-02 12:30:44 |
+---------------------+
4 rows in set (0.00 sec)

5、TIMESTAMP

  1. The format of TIMESTAMP type is YYYY-MM-DD HH:MM:SS, and the display width is fixed at 19 characters
  2. The difference between TIMESTAMP and DATETIME is that the value range of TIMESTAMP is smaller than the value range of DATETIME
  3. The value range of TIMESTAMP is 1970-01-01 00:00:01 UTC ~ 2038-01-19 03:14:07 UTC, where UTC is the universal time. The current time zone will be converted during storage, and then converted during retrieval. Back to current time zone

For example

mysql> create table t11  ( a timestamp ,b int );
Query OK, 0 rows affected (0.01 sec)

mysql> insert into t11(b) values (1),(2),(3),(4);
Query OK, 4 rows affected (0.01 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> select * from t11;
+---------------------+------+
| a                   | b    |
+---------------------+------+
| 2020-11-29 22:36:31 |    1 |
| 2020-11-29 22:36:31 |    2 |
| 2020-11-29 22:36:31 |    3 |
| 2020-11-29 22:36:31 |    4 |
+---------------------+------+
4 rows in set (0.00 sec)

Three, string type

  1. The string type is used to store string data, as well as binary data such as pictures and sounds.
  2. MySQL supports two types of strings: text strings and binary strings.

Text string

Types of Description Storage requirements
CHAR(M) Fixed-length text string M bytes, 1 <= M <= 255
VARCHAR(M) Variable-length text string L+1 bytes, where L <= M and 1 <= M <= 255
TINYTEXT Very small text string M bytes, L+1 bytes, where L <2^8
TEXT Small text string L+2 bytes, where L <2^16
MEDIUMBLOB Medium-sized text string L+3 bytes, where L <2^24
LONGTEXT Large text string L+4 bytes, where L <2^32
ENUM Enumeration type, there can only be one enumeration string value 1 or 2 bytes, depending on the number of enumerated values ​​(maximum 65535)
SET A setting, the string object can have zero or more SET members 1, 2, 3, 4 or 8 bytes, depending on the number of set members (up to 64 members)

Binary string type

Types of Description Storage requirements
BIT(M) Bit field type Approximately (M+7)/8 bytes
BINARY(M) Fixed-length binary string M bytes
VARBINARY(M) Variable-length binary string M+1 bytes
TINYBLOB(M) Very small blob L+1 bytes, where L <2^8
BLOB(M) Small blob L+2 bytes, where L <2^16
MEIDUMBLOB(M) Medium-sized BLOB L+3 bytes, where L <2^24
LONGBLOB(M) Very large blob L+4 bytes, where L <2^32

1、CHAR 和 VARCHAR

  1. char(m)位固定长度字符串,在定义时指定字符串列长,当保存时在右侧填充空格以达到指定的长度,m表示列长度,取值范围是0~255个字符,列如,char(4)定义了一个固定长度的字符串列,其包含的字符个数最大为 4,当检索到 CHAR 值时,尾部的空格将被删掉。
  2. varchar(m)为可变长度的字符串,m 表示最大列长度,取值范围是 0~65535 ,VARCHAR 的最大实际长度由最长的行的大小和使用的字符集确定,而其实际占用的空间为字符串的实际长度加一(一个字符串结束符) 插入值 CHAR(4) 存储需求 VARCHAR(4) 存储需求 ‘’ ’ ’ 4个字节 ’ 1个字节 ‘ab’ ‘ab ’ 4个字节’ab’ 3个字节 ‘abc’ ‘abc’ 4个字节 ‘abc’ 4个字节 ‘abcd’ ‘abcd’ 4个字节 ‘abcd’ 5个字节 ‘abcde’ ‘abcd’ 4个字节 ‘abcd’ 5个字节

2、其他

  • TINYTEXT 最大长度为 255 个字符
  • TEXT 最大长度为 65536 个字符
  • MEDIUMTEXT 最大长度为 16777215个字符
  • LONGTEXT 最大长度为 4294967295 个字符

3、ENUM
在基本的数据类型中,无外乎就是些数字和字符,但是某些事物是较难用数字和字符来准确地表示的。比如一周有七天,分别是Sunday、Monday、Tuesday、Wednesday、Thursday、Friday 和 Saturday。如果我们用整数 0、1、2、3、4、5、6 来表示这七天,那么多下来的那些整数该怎么办?而且这样的设置很容易让数据出错,即取值超出范围。我们能否自创一个数据类型,而数据的取值范围就是这七天呢?因此有了 ENUM 类型(Enumeration,枚举),它允许用户自己来定义一种数据类型,并且列出该数据类型的取值范围。ENUM 是一个字符串对象,其值为表创建时在列规定中枚举(即列举)的一列值,语法格式为:字段名 ENUM (‘值1’, ‘值2’, … ‘值n’) 字段名指将要定义的字段,值 n 指枚举列表中的第 n 个值,ENUM类型的字段在取值时,只能在指定的枚举列表中取,而且一次只能取一个。如果创建的成员中有空格时,其尾部的空格将自动删除。ENUM 值在内部用整数表示,每个枚举值均有一个索引值:列表值所允许的成员值从 1 开始编号,MySQL 存储的就是这个索引编号。枚举最多可以有 65535 个元素。

举例

mysql> create table tmp10
    -> (
    -> soc int,
    -> level enum('excellent','good','bad')
    -> );
Query OK, 0 rows affected (0.01 sec)

mysql> insert into tmp10 values (70,'good'),(90,1),(75,2),(50,3); 
//插入数据,后面三个是根据索引来插入的,也就是(1=excellent,2=good,3=bad)
Query OK, 4 rows affected (0.00 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> insert into tmp10 values(100,'best');
//如果插入一个没有定义过的枚举值'best'会报错
ERROR 1265 (01000): Data truncated for column 'level' at row 1
mysql> select * from tmp10;
+------+-----------+
| soc  | level     |
+------+-----------+
|   70 | good      |
|   90 | excellent |
|   75 | good      |
|   50 | bad       |
+------+-----------+
4 rows in set (0.00 sec)

4、SET

  1. SET 是一个字符串对象,可以有零个或多个值,SET 列最多可以有 64 个成员,其值为表创建时规定的一列值,语法:SET(‘值1’,‘值2’,… ‘值n’) 2. 与 ENUM 类型相同,SET 值在内部用整数表示,列表中每一个值都有一个索引编号
  2. 与 ENUM 类型不同的是,ENUM 类型的字段只能从定义的列值中选择一个值插入,而 SET 类型的列可从定义的列值中选择多个字符的联合
  3. 如果插入 SET 字段中列值有重复,则 MySQL 自动删除重复的值,插入 SET 字段的值的顺序并不重要,MySQL 会在存入数据库时,按照定义的顺序显示

举例

mysql> create table tmp11 (s set('a','b','c','d'));
Query OK, 0 rows affected (0.01 sec)

mysql> insert into tmp11 values ('a'),('a,b,a'),('c,a,d');
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from tmp11;
+-------+
| s     |
+-------+
| a     |
| a,b   |
| a,c,d |
+-------+
3 rows in set (0.00 sec)

5、BIT

  1. BIT 数据类型用来保存位字段值,即以二进制的形式来保存数据,如保存数据 13,则实际保存的是 13 的二进制值,即 1101
  2. BIT 是位字段类型,BIT(M) 中的 M 表示每个值的位数,范围为 1~64 ,如果 M 被省略,则默认为 1 ,如果为 BIT(M) 列分配的值的长度小于 M 位,则在值得左边用 0 填充
  3. 如果需要位数至少为 4 位的 BIT 类型,即可定义为 BIT(4) ,则大于 1111 的数据是不能被插入的

举例

mysql> create table tmp12(b bit(4));
Query OK, 0 rows affected (0.01 sec)

mysql> insert into tmp12 values(2),(9),(15);
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select bin(b+0) from tmp12;
+----------+
| bin(b+0) |
+----------+
| 10       |
| 1001     |
| 1111     |
+----------+
3 rows in set (0.00 sec)

6、BINARY 和 VARBINARY

  1. BINARY 和 VARBINARY 类型类似于 CHAR 和 VARCHAR,不同的是它们包含二进制字节字符串
  2. BINARY 类型的长度是固定的,指定长度之后,不足最大长度的,将在它们右边填充 ‘\0’ 以补齐指定长度
  3. VARBINARY 类型的长度是可变的,指定长度之后,其长度可以在 0 到最大值之间

举例

mysql> create table tmp13
    -> (
    -> b binary(3),
    -> vb varbinary(30)
    -> );
Query OK, 0 rows affected (0.01 sec)

mysql> insert into tmp13 values(5,5);
Query OK, 1 row affected (0.00 sec)

mysql> select * from tmp13;
+------+------+
| b    | vb   |
+------+------+
| 5    | 5    |
+------+------+
1 row in set (0.00 sec)

mysql> select length(b),length(vb) from tmp13;
+-----------+------------+
| length(b) | length(vb) |  # BINARY占用的空间为固定的指定的值
+-----------+------------+ 
|         3 |          1 |# VARBINARY占用的空间为可变的插入的值
+-----------+------------+
1 row in set (0.01 sec)

7、BLOB

  1. BLOB 用来存储可变数量的二进制字符串,分为 TINYBLOB 、BLOB 、MEDIUMBLOB 、LONGBLOB 四种类型。
  2. BLOB 存储的是二进制字符串,TEXT 存储的是文本字符串
  3. BLOB 没有字符集,并且排序和比较基于列值字节的数值;TEXT 有一个字符集,并且根据字符集对值进行排序和比较 数据类型 |存储范围| —|---|—| TINYBLOB |最大长度为255 BLOB |最大长度为65535 MEDIUMBLOB| 最大长度为16777215 LONGBLOB |最大长度为4294967295

Guess you like

Origin blog.csdn.net/weixin_45310323/article/details/110306703
Recommended