# 二进制类型
M-Compatibility支持的二进制类型如[表1]所示。
 表1二进制类型 
| 名称              | 描述                                              | 存储空间            |
|:---|:---|:---|
| BINARY\[(n)\] | 二进制定长字符串，不足补空格。n是指字节长度，如不带精度n，默认精度为1。            | 最大为255个字节。     |
| VARBINARY(n)  | 二进制变长字符串。n是指字节长度。                                | 最大为65532个字节。   |
| BLOB\[(n)\]    | 二进制大对象。n是指该类型的可选字节长度，指定n后将列创建为最小的但足以容纳n字节的BLOB类型。 | 最大为65535个字节。   |
| TINYBLOB      | 二进制大对象。                                            | 最大为255个字节。       |
| MEDIUMBLOB   | 二进制大对象。                                            | 最大为16777215个字节。 |
| LONGBLOB         | 二进制大对象。                                           | 最大为1GB-1个字节。  |
   
![](https://support.huaweicloud.com/centralized-ref-v10-gaussdb/public_sys-resources/note_3.0-zh-cn.png)
- sql_mode参数值包含"strict_trans_tables"时，非法输入或者输入超过范围时，会报error。
- sql_mode参数值不包含"strict_trans_tables"时，非法输入或者输入超过范围时，会报warning信息，并返回截断后的值。
- 不支持'\|\|'连接符连接数据。
- 当开启enable_m_format_fix_strfunc_rettype参数，并且当前数据库的字符集为"binary"或collation_connection的取值为"binary"时，若返回值为BLOB类型的字符串函数结果超出其最大范围限制，该结果仍可被插入到BLOB类型的表字段中。例如：
  ```
  m_db=# SET enable_m_format_fix_strfunc_rettype=on;
  SET
  m_db=# SET collation_connection='binary';
  SET
  m_db=# CREATE TABLE blob_type_t1(BT_COL1 BLOB);
  CREATE TABLE
  m_db=# INSERT INTO blob_type_t1 VALUES (REPEAT('A', 65536));
  INSERT 0 1
  ```
  
 
#### 示例
显示用字符串输出二进制类型。
```
--创建表。
m_db=# CREATE TABLE blob_type_t1  
(
    BT_COL1 BINARY(10),
    BT_COL2 VARBINARY(10),
    BT_COL3 BLOB,
    BT_COL4 BLOB(10)
);
--插入数据。
m_db=# INSERT INTO blob_type_t1 VALUES ('blob_test1', 'blob_test2','blob_test3','blob_test4');
--查询表中的数据。
m_db=# SELECT *  FROM blob_type_t1;
  bt_col1   |  bt_col2   |  bt_col3   |  bt_col4
------------+------------+------------+------------
 blob_test1 | blob_test2 | blob_test3 | blob_test4
(1 row)
--删除表。
m_db=# DROP TABLE blob_type_t1;
--示例：BINARY类型
--创建表。
m_db=# CREATE TABLE blob_type_t2  
(
    BT_COL1 BINARY(5)
);
--严格模式下插入的数据长度超过类型规定的长度报错。
m_db=# SET SQL_MODE = 'strict_trans_tables,only_full_group_by,no_zero_in_date,no_zero_date,error_for_division_by_zero,
no_auto_create_user,no_engine_substitution';
SET
m_db=# INSERT INTO blob_type_t2 VALUES ('too long');
ERROR:  The value of the binary type exceeds 5.
CONTEXT:  referenced column: bt_col1
--宽松模式下插入的数据长度超过类型规定的长度提示warning,数据截断插入。
m_db=# SET SQL_MODE = '';
SET
m_db=# INSERT INTO blob_type_t2 VALUES ('too long');
WARNING:  The value of the binary type exceeds 5.
CONTEXT:  referenced column: bt_col1
INSERT 0 1
--查询表中的数据。
m_db=# SELECT * FROM blob_type_t2;
 bt_col1
---------
 too l
(1 row)
--删除表。
m_db=# DROP TABLE blob_type_t2;
```
