
# 有符号整数类型
M-Compatibility支持的有符号整数类型如[表1]所示。
 表1有符号整数类型 
| 名称                                  | 描述               | 存储空间 | 范围                          |
|:---|:---|:---|:---|
| TINYINT \[SIGNED\] \[ZEROFILL\]     | 微整数，类型别名为INT1。   | 1字节  | \[-128, 127\]               |
| SMALLINT \[SIGNED\] \[ZEROFILL\]    | 小范围整数，类型别名为INT2。 | 2字节  | \[-32768, 32767\]           |
| MEDIUMINT \[SIGNED\] \[ZEROFILL\]   | 中等范围整数。          | 4字节  | \[-8388608, 8388607\]       |
| INT/INTEGER \[SIGNED\] \[ZEROFILL\] | 常用整数，类型别名为INT4。  | 4字节  | \[-2147483648, 2147483647\] |
| BIGINT \[SIGNED\] \[ZEROFILL\]      | 大范围整数，类型别名为INT8。 | 8字节  | \[-2\^63, 2\^63-1\]         |
   
![](https://support.huaweicloud.com/distributed-m-comp-devg-v8-gaussdb/public_sys-resources/note_3.0-zh-cn.png)
- sql_mode参数值包含"strict_trans_tables"时，有符号整数类型列输入非数值类型的字符，会报ERROR。
- sql_mode参数值不包含"strict_trans_tables"时，有符号整数类型列输入非数值类型的字符，会返回截断后的值。
- 创建表列时，整数类型后增加SIGNED语法与直接使用有符号整数类型功能含义相同。
- 支持有符号整数类型设置ZEROFILL属性：
  - 设置ZEROFILL属性时，会按照显示宽度在数值前添零。如果没有设置显示宽度，会按照数据类型的默认显示宽度作为具体显示宽度。例如，对于声明为INT(4) ZEROFILL的列，将检索5的值作为0005。
  
  - 设置ZEROFILL属性时，将自动添加UNSIGNED属性，即使设置为SIGNED属性，也会转成UNSIGNED属性。
   
 
#### 示例
```
-- 创建列类型为有符号整数类型的表。
m_db=# CREATE TABLE test_int
(
    a TINYINT, 
    b SMALLINT,
    c MEDIUMINT,
    d INTEGER,
    e BIGINT
);
-- 插入数据。
m_db=# INSERT INTO test_int VALUES (100, 200, 300, 400, 500);
-- 查询表中的数据。
m_db=# SELECT * FROM test_int;
  a  |  b  |  c  |  d  |  e
-----+-----+-----+-----+-----
 100 | 200 | 300 | 400 | 500
(1 row)
-- 严格模式下插入非数值类型的字符报错。
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 test_int VALUES ('1@2@3', '200&20&2', '4%4', '200$30', '1@3');
ERROR:  invalid input syntax for integer: "1@2@3"
LINE 1: INSERT INTO test_int VALUES ('1@2@3', '200&20&2', '4%4', '20...
                                     ^
CONTEXT:  referenced column: a
-- 宽松模式下插入非数值类型的字符提示warning，自动截断。
m_db=# SET SQL_MODE = '';
SET
m_db=# INSERT INTO test_int VALUES ('1@2@3', '200&20&2', '4%4', '200$30', '1@3');
WARNING:  invalid input syntax for integer: "1@2@3"
LINE 1: INSERT INTO test_int VALUES ('1@2@3', '200&20&2', '4%4', '20...
                                     ^
CONTEXT:  referenced column: a
WARNING:  invalid input syntax for integer: "200&20&2"
LINE 1: INSERT INTO test_int VALUES ('1@2@3', '200&20&2', '4%4', '20...
                                              ^
CONTEXT:  referenced column: b
WARNING:  invalid input syntax for integer: "4%4"
LINE 1: INSERT INTO test_int VALUES ('1@2@3', '200&20&2', '4%4', '20...
                                                          ^
CONTEXT:  referenced column: c
WARNING:  invalid input syntax for integer: "200$30"
LINE 1: ...INTO test_int VALUES ('1@2@3', '200&20&2', '4%4', '200$30', ...
                                                             ^
CONTEXT:  referenced column: d
WARNING:  invalid input syntax for integer: "1@3"
LINE 1: ...st_int VALUES ('1@2@3', '200&20&2', '4%4', '200$30', '1@3');
                                                                ^
CONTEXT:  referenced column: e
INSERT 0 1
m_db=# SELECT * FROM test_int;
  a  |  b  |  c  |  d  |  e
-----+-----+-----+-----+-----
 100 | 200 | 300 | 400 | 500
 1   | 200 | 4   | 200 |   1
(2 rows)
-- 设置zerofill属性。
m_db=# dROP TABLE IF EXISTS t1;
DROP TABLE
m_db=# CREATE TABLE t1 (a int zerofill);
CREATE TABLE
m_db=# INSERT INTO t1 VALUES(1);
INSERT 0 1
m_db=# SELECT * FROM t1;
     a      
------------
 0000000001
(1 row)
-- 删除表。
m_db=# DROP TABLE test_int;
```
