本文介绍如何使用 OceanBase 数据库中 MySQL 模式的自增列。
前提条件
当前用户需要对表具有 CREATE 和 ALTER 权限。
背景信息
如果创建表时需要某个数值列的值不重复并且保持递增,这就是自增列。在 MySQL 租户里,列的类型可以定义为 AUTO_INCREMENT,即 MySQL 租户的自增列。
属性
自增列有三个重要属性:自增起始值、自增步长、自增列缓存大小,通过以下三个租户变量参数控制。
语法
obclient> show variables where variable_name in (
'auto_increment_increment',
'auto_increment_offset',
'auto_increment_cache_size');
+---------------------------+---------+
| Variable_name | Value |
+---------------------------+---------+
| auto_increment_cache_size | 1000000 |
| auto_increment_increment | 1 |
| auto_increment_offset | 1 |
+---------------------------+---------+
3 rows in set
参数说明
| 系统变量 | 说明 |
|---|---|
| auto_increment_cache_size | 用于设置自增的缓存个数,取值范围为 [1, 100000000],默认值为 1000000。 |
| auto_increment_increment | 用于设置自增步长,取值范围为 [1, 65535],默认值为 1。 |
| auto_increment_offset | 用于确定 AUTO_INCREMENT 列值的起点,取值范围为 [1, 65535],默认值为 1。 |
示例
下面创建了一个自增列,在使用 INSERT 语句插入记录时不需要指定自增列,OceanBase 数据库会自动为该列填充值。
如果在 INSERT 时指定了自增列的值,且这个值为 0,则 OceanBase 数据库会用自增列的下一个值填充列的值;如果这个值比当前最大值小,则不影响自增列的下一个值的计算;如果这个值比当前最大值大,则自增列会把插入值和自增列缓存值的和作为下次自增的起始值。
创建 t1 表并指定列约束条件。
obclient> CREATE TABLE t1(id bigint not null auto_increment primary key, name varchar(50), gmt_create timestamp not null default current_timestamp); Query OK, 0 rows affected obclient> INSERT INTO t1(name) VALUES('A'),('B'),('C'); Query OK, 3 rows affected (0.01 sec) Records: 3 Duplicates: 0 Warnings: 0 obclient> SELECT * FROM t1; +----+------+---------------------+ | id | name | gmt_create | +----+------+---------------------+ | 1 | A | 2020-04-03 17:09:55 | | 2 | B | 2020-04-03 17:09:55 | | 3 | C | 2020-04-03 17:09:55 | +----+------+---------------------+ 3 rows in set说明
- 指定
id列数字类型为bigint。 - 非空约束(
NOT NULL):不允许约束包含的列的值为NULL。 - 设置自增列:
auto_increment。 primary key:设置 id 列为主键列。name varchar(50):允许 name 列插入的数据类型为字符型(varchar),50表示最大字符长度。- 由于设置了
id列为自增列,所以在使用INSERT语句插入记录时不需要指定自增列,OceanBase 数据库会自动为该列填充值。
- 指定
在
t1表中插入一条数据且指定自增列的值为0。obclient> INSERT INTO t1(id, name) VALUES(0, 'D'); Query OK, 1 row affected obclient> select * from t1; +----+------+---------------------+ | id | name | gmt_create | +----+------+---------------------+ | 1 | A | 2021-12-17 14:21:53 | | 2 | B | 2021-12-17 14:21:53 | | 3 | C | 2021-12-17 14:21:53 | | 4 | D | 2021-12-17 14:22:36 | +----+------+---------------------+- 在
INSERT时指定了自增列的值,且这个值为0,则 OceanBase 数据库会用自增列的下一个值填充列的值。
- 在
在
t1表中插入一条数据并指定自增列的值为-1。obclient> INSERT INTO t1(id, name) VALUES(-1,'E'); Query OK, 1 row affected obclient> select * from t1; +----+------+---------------------+ | id | name | gmt_create | +----+------+---------------------+ | -1 | E | 2021-12-17 15:25:04 | | 1 | A | 2021-12-17 14:21:53 | | 2 | B | 2021-12-17 14:21:53 | | 3 | C | 2021-12-17 14:21:53 | | 4 | D | 2021-12-17 14:22:36 | +----+------+---------------------+ 5 rows in set因为
-1比当前自增列的最大值小,所以不影响自增列的下一个值的计算。在
t1表中插入一条数据并指定自增列的值为10。obclient> INSERT INTO t1(id, name) VALUES(10,'F'); Query OK, 1 row affected (0.01 sec) obclient> select * from t1; +----+------+---------------------+ | id | name | gmt_create | +----+------+---------------------+ | -1 | E | 2021-12-17 15:25:04 | | 1 | A | 2021-12-17 14:21:53 | | 2 | B | 2021-12-17 14:21:53 | | 3 | C | 2021-12-17 14:21:53 | | 4 | D | 2021-12-17 14:22:36 | | 10 | F | 2021-12-17 15:33:28 | +----+------+---------------------+ 6 rows in set因为
10比当前自增列的最大值大,所以此时不指定自增列的值,在name列插入一条数据G,则自增列会把插入值 (10)和自增列缓存值(1000000)的和作为下次自增的起始值(1000011)。查询结果如下:
obclient> INSERT INTO t1(name) VALUES('G'); Query OK, 1 row affected obclient> SELECT * FROM t1; +---------+------+---------------------+ | id | name | gmt_create | +---------+------+---------------------+ | -1 | E | 2020-04-03 17:10:24 | | 1 | A | 2020-04-03 17:09:55 | | 2 | B | 2020-04-03 17:09:55 | | 3 | C | 2020-04-03 17:09:55 | | 4 | D | 2020-04-03 17:10:19 | | 10 | F | 2020-04-03 17:10:29 | | 1000011 | G | 2020-04-03 17:10:34 | +---------+------+---------------------+ 7 rows in set