MySQL基础知识01数据类型字符串相关类型

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL基础知识01数据类型字符串相关类型相关的知识,希望对你有一定的参考价值。

1. CHAR

语法:

CHAR (length)

其中length的取值范围为1255

 

超过255则报错:

mysql> create table s3 ( value char(1023) primary key);

ERROR 1074 (42000): Column length too big for column ‘value‘ (max = 255); use BLOB or TEXT instead

 

例如:CHAR(20)表示最多包含20个字符的字符串。存储时,不足20个字符时后面加空格。

在默认情况下,提取时,自动删除掉后面的空格;在PAD_CHAR_TO_FULL_LENGTH时,则不会删除后面的空格,即提取后的结果包含20个字符。

 

假定存在如下数据:

mysql> show create table s1;

+-------+-----------------------------------------------------------------------------------------------------------------+

| Table | Create Table                                                                                                    |

+-------+-----------------------------------------------------------------------------------------------------------------+

| s1    | CREATE TABLE `s1` (

  `value` char(10) NOT NULL,

  PRIMARY KEY (`value`)

) ENGINE=InnoDB DEFAULT CHARSET=latin1 |

+-------+-----------------------------------------------------------------------------------------------------------------+

1 row in set (0.00 sec)

 

mysql> insert into s1 values (‘ABC‘) ,  (‘1234567890‘) , (‘ 12 3 45 ‘);

Query OK, 3 rows affected (0.01 sec)

Records: 3  Duplicates: 0  Warnings: 0

 

 

在默认情况下:

mysql> set sql_mode=‘‘;

Query OK, 0 rows affected (0.00 sec)

 

mysql> select length(value) , char_length(value) , value from s1;

+---------------+--------------------+------------+

| length(value) | char_length(value) | value      |

+---------------+--------------------+------------+

|             8 |                  8 |  12 3 45   |

|            10 |                 10 | 1234567890 |

|             3 |                  3 | ABC        |

+---------------+--------------------+------------+

3 rows in set (0.00 sec)

 

 

sql_modePAD_CHAR_TO_FULL_LENGTH时,将不会删除CHAR类型的字符串后面附加的空格。

mysql> set sql_mode=‘pad_char_to_full_length‘;

Query OK, 0 rows affected (0.00 sec)

 

mysql> select length(value) , char_length(value) , value from s1;

+---------------+--------------------+------------+

| length(value) | char_length(value) | value      |

+---------------+--------------------+------------+

|            10 |                 10 |  12 3 45   |

|            10 |                 10 | 1234567890 |

|            10 |                 10 | ABC        |

+---------------+--------------------+------------+

3 rows in set (0.00 sec)

 

mysql> select @sql_mode;

+-----------+

| @sql_mode |

+-----------+

| NULL      |

+-----------+

1 row in set (0.00 sec)

 

mysql> select @@sql_mode;

+-------------------------+

| @@sql_mode              |

+-------------------------+

| PAD_CHAR_TO_FULL_LENGTH |

+-------------------------+

1 row in set (0.00 sec)

 

 

 

 

2. VARCHAR

 

语法:

VARCHAR(max-length)

其中,max-length是该类型字符串最多可以存储的字符的个数。整个字段占用的空间大小是长度占有的字节数加上实际字符所占有的字节数。当字符串长度在0255时,长度占1个字节,当字符串长度超过255个字符时,长度占2个字节。VARCHAR类型最多65535个字符。

 

mysql> create table s13 ( value varchar(10) primary key);

Query OK, 0 rows affected (0.02 sec)

 

mysql> insert into s13 values (‘ABCD‘) , (‘ A B ‘);

Query OK, 2 rows affected (0.05 sec)

Records: 2  Duplicates: 0  Warnings: 0

 

mysql> select @@sql_mode;

+-------------------------+

| @@sql_mode              |

+-------------------------+

| PAD_CHAR_TO_FULL_LENGTH |

+-------------------------+

1 row in set (0.00 sec)

 

mysql> select length(value), char_length(value), value from s13;

+---------------+--------------------+-------+

| length(value) | char_length(value) | value |

+---------------+--------------------+-------+

|             5 |                  5 |  A B  |

|             4 |                  4 | ABCD  |

+---------------+--------------------+-------+

2 rows in set (0.00 sec)

 

从上述结果可以看到,PAD_CHAR_TO_FULL_LENGTH模式对于VARCHAR类型没有作用,这个模式只对CHAR类型有作用。

 

3. 字段长度限制

(1)主键为VARCHAR类型时的长度限制为3072个字符。

mysql> create table s5 ( value varchar(65535) primary key);

ERROR 1071 (42000): Specified key was too long; max key length is 3072 bytes

mysql> create table s5 ( value varchar(3072) primary key);

Query OK, 0 rows affected (0.02 sec)

 

(2)VARCHAR类型的字段的长度大于或等于65536时,自动转化为TEXT相关类型。

原因就是因为VARCHAR类型在存储时要保存字符串长度,而且最多占用2个字节。

mysql> create table s6 ( value varchar(3072) primary key , v varchar(65536));

Query OK, 0 rows affected, 1 warning (0.03 sec)

 

mysql> show warnings;

+-------+------+--------------------------------------------+

| Level | Code | Message                                    |

+-------+------+--------------------------------------------+

| Note  | 1246 | Converting column ‘v‘ from VARCHAR to TEXT |

+-------+------+--------------------------------------------+

1 row in set (0.00 sec)

 

mysql> show create table s6;

+-------+----------------------------------------------------------------------------------------------------------------------------------------+

| Table | Create Table                                                                                                                           |

+-------+----------------------------------------------------------------------------------------------------------------------------------------+

| s6    | CREATE TABLE `s6` (

  `value` varchar(3072) NOT NULL,

  `v` mediumtext,

  PRIMARY KEY (`value`)

) ENGINE=InnoDB DEFAULT CHARSET=latin1 |

+-------+----------------------------------------------------------------------------------------------------------------------------------------+

1 row in set (0.00 sec)

 

 

(3)一个行中的VARCHARCHAR类型的字符串的最大长度总和不能超过65535个字符。

mysql> create table s12 ( value  int  primary key , v varchar(65535));

ERROR 1118 (42000): Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65535. This includes storage overhead, check the manual. You have to change some columns to TEXT or BLOBs

 

mysql> create table s12 ( value  int  primary key , v varchar(65500));

Query OK, 0 rows affected (0.03 sec)

 

 

(4)CHAR类型的字段插入超过定义长度的数据时,将自动截取为定义长度。

mysql> show create table s1;

+-------+---------------------------------------------------------------------------------------------------------------+

| Table | Create Table                                                                                                  |

+-------+---------------------------------------------------------------------------------------------------------------+

| s1    | CREATE TABLE `s1` (

  `value` char(10) NOT NULL,

  PRIMARY KEY (`value`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8 |

+-------+---------------------------------------------------------------------------------------------------------------+

1 row in set (0.00 sec)

 

mysql> insert into s1 values (‘12345678901234‘);

Query OK, 1 row affected, 1 warning (0.01 sec)

 

mysql> show warnings;

+---------+------+--------------------------------------------+

| Level   | Code | Message                                    |

+---------+------+--------------------------------------------+

| Warning | 1265 | Data truncated for column ‘value‘ at row 1 |

+---------+------+--------------------------------------------+

1 row in set (0.00 sec)

 

mysql> select * from s1;

+--------------+

| value        |

+--------------+

|              |

| 1234567890   |

| 中           |

+--------------+

3 rows in set (0.00 sec)

 

 

(5)VARCHAR类型的字段在插入数据时长度超过定义的长度,将自动截取到定义的长度。

mysql> insert into s13 values (‘12345678901234‘);

Query OK, 1 row affected, 1 warning (0.01 sec)

 

mysql> show warnings;

+---------+------+--------------------------------------------+

| Level   | Code | Message                                    |

+---------+------+--------------------------------------------+

| Warning | 1265 | Data truncated for column ‘value‘ at row 1 |

+---------+------+--------------------------------------------+

1 row in set (0.00 sec)

 

mysql> select * from s13;

+------------+

| value      |

+------------+

|  A B       |

| 1234567890 |

| ABCD       |

+------------+

3 rows in set (0.00 sec)

 

 

 

 

 

4. 字符集

默认情况下,数据表的字符集为latin1。可以在create table或者alter table时修改数据表的字符集为utf8

 

mysql> show variables like ‘%character%‘;

+--------------------------+----------------------------+

| Variable_name            | Value                      |

+--------------------------+----------------------------+

| character_set_client     | utf8                       |

| character_set_connection | utf8                       |

| character_set_database   | latin1                     |

| character_set_filesystem | binary                     |

| character_set_results    | utf8                       |

| character_set_server     | latin1                     |

| character_set_system     | utf8                       |

| character_sets_dir       | /usr/share/mysql/charsets/ |

+--------------------------+----------------------------+

8 rows in set (0.01 sec)

 

mysql> show create table s1;

+-------+---------------------------------------------------------------------------------------------------------------+

| Table | Create Table                                                                                                  |

+-------+---------------------------------------------------------------------------------------------------------------+

| s1    | CREATE TABLE `s1` (

  `value` char(10) NOT NULL,

  PRIMARY KEY (`value`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8 |

+-------+---------------------------------------------------------------------------------------------------------------+

1 row in set (0.00 sec)

 

 

mysql> select length(value) , char_length(value) , value from s1;

+---------------+--------------------+-------+

| length(value) | char_length(value) | value |

+---------------+--------------------+-------+

|             3 |                  1 | 中    |

+---------------+--------------------+-------+

1 row in set (0.00 sec)

 

LENGTH表示字节数,CHAR_LENGTH表示字符数。在使用UTF8字符集时,一个汉字的字节数为3,即LENGTH3,;而一个汉字的字符数仍然为1,即CHAR_LENGTH仍然为1

 

5. BINARY

BINARY用法与CHAR类似,只是可以存储所有字符值,包括空字符(ASCII值为0),而且末尾补足长度的字符为空字符,而不是空格。BINARY类型最大长度255个字节,并非255个字符。

 

mysql> create table b1 (value binary(10) primary key);

Query OK, 0 rows affected (0.03 sec)

 

mysql> insert into b1 values(‘1234567890‘) , (‘123‘);

Query OK, 2 rows affected (0.01 sec)

Records: 2  Duplicates: 0  Warnings: 0

 

mysql> select value,hex(value) from b1;

+------------+----------------------+

| value      | hex(value)           |

+------------+----------------------+

| 123        | 31323300000000000000 |

| 1234567890 | 31323334353637383930 |

+------------+----------------------+

2 rows in set (0.00 sec)

 

 

使用unhex()函数可以将十六进制字符串转化为不可见字符组成的字符串,插入不可见字符的例子如下:

mysql> insert into b1 values ( unhex(‘01020300010203‘));

Query OK, 1 row affected (0.01 sec)

 

mysql> select value,hex(value) from b1;

+------------+----------------------+

| value      | hex(value)           |

+------------+----------------------+

|      | 01020300010203000000 |

+------------+----------------------+

1 row in set (0.00 sec)

 

 

BINARY类型超过255个字节时,将报错。

mysql> create table b10 ( value int  primary key, v binary(256));

ERROR 1074 (42000): Column length too big for column ‘v‘ (max = 255); use BLOB or TEXT instead

 

6. VARBINARY

VARBINARYBINARY类似,二者之间差异与 VARCHARCHAR之间的差异类似。而VARBINARYVARCHAR也很类似,二者之间的差异与BINARYCHAR之间的差异类似。

当最大长度为0255字节时,长度前缀为1个字节,字段总长度为1+最大长度个字节。

当最大长度为25665535字节时,长度前缀为2个字节。

 

mysql> create table b2 ( value varbinary(10) primary key);

Query OK, 0 rows affected (0.03 sec)

 

mysql> insert into b2 values ( ‘1234567890‘), ("123"), (unhex(‘01020300010203‘));

Query OK, 3 rows affected (0.01 sec)

Records: 3  Duplicates: 0  Warnings: 0

 

mysql> select value, hex(value) from b2;

+------------+----------------------+

| value      | hex(value)           |

+------------+----------------------+

|      | 01020300010203       |

| 123        | 313233               |

| 1234567890 | 31323334353637383930 |

+------------+----------------------+

3 rows in set (0.00 sec)

 

 

VARBINARY类型超过65535字节时,将自动转换为BLOB相关类型。

mysql> create table b9 ( value int  primary key, v varbinary(65536));

Query OK, 0 rows affected, 1 warning (0.02 sec)

 

mysql> show warnings;

+-------+------+----------------------------------------------+

| Level | Code | Message                                      |

+-------+------+----------------------------------------------+

| Note  | 1246 | Converting column ‘v‘ from VARBINARY to BLOB |

+-------+------+----------------------------------------------+

1 row in set (0.00 sec)

 

mysql> show create table b9;

+-------+----------------------------------------------------------------------------------------------------------------------------------+

| Table | Create Table                                                                                                                     |

+-------+----------------------------------------------------------------------------------------------------------------------------------+

| b9    | CREATE TABLE `b9` (

  `value` int(11) NOT NULL,

  `v` mediumblob,

  PRIMARY KEY (`value`)

) ENGINE=InnoDB DEFAULT CHARSET=latin1 |

+-------+----------------------------------------------------------------------------------------------------------------------------------+

1 row in set (0.00 sec)

 

 

7. TEXT

有四种TEXT相关类型,它们之间的差别在于允许的最大长度不相同。

TINYTEXT1255个字节。字段长度前缀1个字节。

 TEXT25665535个字节。字段长度前缀2个字节。

 MEDIUMTEXT65536224次方-1个字节。字段长度前缀3个字节。

LONGTEXT224次方到232次方-1个字节。字段长度前缀4个字节。

 

在创建数据表时,在CREATE TABLE语句中只使用TEXT类型即可,MySQL将根据TEXT(N)中的N自动决定实际使用TINYTEXTTEXTMEDIUMTEXTLONGTEXT。在使用其它三个类型时则没有这种根据长度自动决定实际类型的功能,而且不能指定最大长度。

 

mysql> create table b3 ( value int primary key , v text(30) );

Query OK, 0 rows affected (0.04 sec)

 

mysql> show create table b3;

+-------+--------------------------------------------------------------------------------------------------------------------------------+

| Table | Create Table                                                                                                                   |

+-------+--------------------------------------------------------------------------------------------------------------------------------+

| b3    | CREATE TABLE `b3` (

  `value` int(11) NOT NULL,

  `v` tinytext,

  PRIMARY KEY (`value`)

) ENGINE=InnoDB DEFAULT CHARSET=latin1 |

+-------+--------------------------------------------------------------------------------------------------------------------------------+

1 row in set (0.00 sec)

 

mysql> create table b4 ( value int primary key , v text(65536) );

Query OK, 0 rows affected (0.02 sec)

 

mysql> show create table b4;

+-------+----------------------------------------------------------------------------------------------------------------------------------+

| Table | Create Table                                                                                                                     |

+-------+----------------------------------------------------------------------------------------------------------------------------------+

| b4    | CREATE TABLE `b4` (

  `value` int(11) NOT NULL,

  `v` mediumtext,

  PRIMARY KEY (`value`)

) ENGINE=InnoDB DEFAULT CHARSET=latin1 |

+-------+----------------------------------------------------------------------------------------------------------------------------------+

1 row in set (0.00 sec)

 

mysql> create table b5 ( value int primary key , v text(300) );

Query OK, 0 rows affected (0.02 sec)

 

mysql> show create table b5;

+-------+----------------------------------------------------------------------------------------------------------------------------+

| Table | Create Table                                                                                                               |

+-------+----------------------------------------------------------------------------------------------------------------------------+

| b5    | CREATE TABLE `b5` (

  `value` int(11) NOT NULL,

  `v` text,

  PRIMARY KEY (`value`)

) ENGINE=InnoDB DEFAULT CHARSET=latin1 |

+-------+----------------------------------------------------------------------------------------------------------------------------+

1 row in set (0.01 sec)

 

TEXT类型和VARCHAR类型类似,并不会进行长度补足的操作。

 

8. BLOB

BLOB相关类型也有类似的四种类型:

有四种BLOB相关类型,它们之间的差别在于允许的最大长度不相同。

TINYBLOB1255个字节。字段长度前缀1个字节。

 BLOB25665535个字节。字段长度前缀2个字节。

 MEDIUMBLOB65536224次方-1个字节。字段长度前缀3个字节。

LONGBLOB224次方到232次方-1个字节。字段长度前缀4个字节。

mysql> create table b11 ( value int primary key, v blob(100));

Query OK, 0 rows affected (0.03 sec)

 

在定义数据表时,如果使用BLOB(N)的形式定义字段,则实际字段类型被自动调整为最适合最大长度NBLOB相关类型。

mysql> show create table b11;

+-------+---------------------------------------------------------------------------------------------------------------------------------+

| Table | Create Table                                                                                                                    |

+-------+---------------------------------------------------------------------------------------------------------------------------------+

| b11   | CREATE TABLE `b11` (

  `value` int(11) NOT NULL,

  `v` tinyblob,

  PRIMARY KEY (`value`)

) ENGINE=InnoDB DEFAULT CHARSET=latin1 |

+-------+---------------------------------------------------------------------------------------------------------------------------------+

1 row in set (0.00 sec)

 

 

BLOB相关类型在实际存储和读取时,不会发生长度补足操作和截取操作,而是存储和读取原始二进制数据内容。

 

mysql> insert into b11 values (1,unhex(‘01020300010203‘)),(2,‘1234567890‘);

Query OK, 2 rows affected (0.01 sec)

Records: 2  Duplicates: 0  Warnings: 0

 

mysql> select value, v,hex(v) from b11;

+-------+------------+----------------------+

| value | v          | hex(v)               |

+-------+------------+----------------------+

|     1 |      | 01020300010203       |

|     2 | 1234567890 | 31323334353637383930 |

+-------+------------+----------------------+

2 rows in set (0.00 sec)

 

以上是关于MySQL基础知识01数据类型字符串相关类型的主要内容,如果未能解决你的问题,请参考以下文章

MySQL基础知识03数据类型日期时间相关类型

Redis相关知识

MySQL学习基础篇

MySQL数据类型之字符相关

python的所有知识点(含讲解),不看就亏死了

MySQL数据类型之字符相关 -- 2019-08-07 10:35:07