創建一個表
CREATE TABLE `test` (
`id` tinyint(4) NOT NULL AUTO_INCREMENT COMMENT '取值範圍[-128,127]',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
自增插入一條記錄
insert into test (id) values(null);
查詢結果:
select * from test;
+---------------+
| id |
+---------------+
| 1 |
+---------------+
插入越界數據
- 插入大於max的數據
insert into test (id) values(128);
mysql warning:1 row(s) affected, 1 warning(s): 1264 Out of range value for column ‘id’ at row 1
查詢結果:
select * from test;
+---------------+
| id |
+---------------+
| 1 |
| 127 |
+---------------+
- 插入小於min的數據
insert into test (id) values(-129);
mysql warning:1 row(s) affected, 1 warning(s): 1264 Out of range value for column ‘id’ at row 1
查詢結果:
select * from test;
+---------------+
| id |
+---------------+
| 1 |
| 127 |
| -128 |
+---------------+
結論:id插入值越界後,數據庫報警,數據插入值爲距離最近的邊界值。
id自增到最大後,再插入一條數據
- 往最大值方向插入
insert into test (id) values(null);
mysql error:Error Code: 1062. Duplicate entry ‘127’ for key ‘PRIMARY’
- 往最小值方向插入
insert into test (id) values(-129);
Error Code: 1062. Duplicate entry ‘-128’ for key ‘PRIMARY’
結論:id邊界值使用後,越過此邊界值插入數據會失敗