南大通用GBase 8s 自增列在DML中的使用介绍

5 阅读4分钟

原文链接:www.gbase.cn/community/p…
更多精彩内容尽在南大通用GBase技术社区,南大通用致力于成为用户最信赖的数据库产品供应商。

在数据库应用中,自增列(AUTO_INCREMENT)是一种常见的需求,用于自动生成唯一的标识符。GBase 8s 提供了自增列功能,支持在 INSERT、UPDATE 和 DELETE 等 DML 操作中的灵活使用。本文将详细介绍 GBase 8s 自增列的使用方法,有助更好地理解和应用这一功能。

一、INSERT语句中的使用

自增值需要满足以下要求:

AUTO_INCREMENT = N

从N开始,寻找大于等于N且满足 (value - auto_increment_offset) %  auto_increment_increment = = 0 的最小value

1、设置相关变量

> set environment sqlmode 'mysql';
Environment set.
Elapsed time: 0.001 sec
> set environment NO_AUTO_VALUE_ON_ZERO off;
Environment set.
Elapsed time: 0.001 sec
> SET SESSION auto_increment_increment = 100;
Environment set.
Elapsed time: 0.001 sec
> SET SESSION auto_increment_offset = 1;
Environment set.
Elapsed time: 0.001 sec

2、建表是设置自增列起始值为100

> CREATE TABLE test (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(10)) AUTO_INCREMENT = 100;
Table created.
Elapsed time: 0.002 sec

 

3、插入数据

> INSERT INTO test (name) VALUES ('AA');
1 row(s) inserted.
Elapsed time: 0.001 sec
> INSERT INTO test (id, name) VALUES (0, 'BB');
1 row(s) inserted.
Elapsed time: 0.001 sec
> INSERT INTO test (id, name) VALUES (NULL, 'CC');
1 row(s) inserted.
Elapsed time: 0.001 sec
> SELECT * FROM test;

       ID NAME
       101 AA
       201 BB
       301 CC
3 row(s) retrieved.
Elapsed time: 0.001 sec

说明:
会话级环境变量auto_increment_increment 和 auto_increment_offset的默认值均为1,由于开始进行了修改,使得auto_increment_increment=100,auto_increment_offset=1。建表时的表级自增列起始值为100,那么计算方式如下:
value = 100:          (100-1) % 100 不等于0,不符合条件;
value + 1 = 101:    (101-1) % 100 等于0,符合条件。

二、UPDATE语句中的使用

更新AUTO_INCREMENT 列需满足主键和约束的限制要求,更新后再插入默认值,按照当前最大值 + 步长插入。

> update test set id=8 where name='BB';
1 row(s) updated.
Elapsed time: 0.001 sec
> insert into test (name) values ('update + 1');
1 row(s) inserted.
Elapsed time: 0.001 sec

> select * from test;
        ID NAME
       101 AA
         8 BB
       301 CC
       401 update + 1
4 row(s) retrieved.
Elapsed time: 0.001 sec

 

三、DALETE语句中的使用

(1) 删除AUTO_INCREMENT 列后再插入默认值,按照当前最大值+1插入;若删除的是当前最大值,不会复用已删除的最大值。

> insert into test (name) values ('update + 2');
1 row(s) inserted.
Elapsed time: 0.001 sec

> select * from test;
        ID NAME
       101 AA
         8 BB
       301 CC
       401 update + 1
       501 update + 2
5 row(s) retrieved.
Elapsed time: 0.001 sec
> delete from test where id=301;
1 row(s) deleted.
Elapsed time: 0.001 sec
> insert into test (name) values ('after');
1 row(s) inserted.
Elapsed time: 0.001 sec
> select * from test;
        ID NAME
       101 AA
         8 BB
       601 after
       401 update + 1
       501 update + 2
5 row(s) retrieved.
Elapsed time: 0.001 sec

 

(2)若删除的是最大边界值,则可以复用最大边界值

> insert into test (id, name) values (2147483647, 'max value');
1 row(s) inserted.
Elapsed time: 0.001 sec
> select * from test;
        ID NAME
       101 AA
         8 BB
       601 after
       401 update + 1
       501 update + 2
2147483647 max value
6 row(s) retrieved.
Elapsed time: 0.001 sec
> delete from test where name = 'max value';
1 row(s) deleted.
Elapsed time: 0.001 sec
> insert into test (name) values ('max');
1 row(s) inserted.
Elapsed time: 0.001 sec
> select * from test;
        ID NAME
       101 AA
         8 BB
       601 after
       401 update + 1
       501 update + 2
2147483647 max
6 row(s) retrieved.
Elapsed time: 0.001 sec

说明:

  • 删除最大边界值 500 后,可以复用该值。

  • 再次插入时,自增值可以为 500(如果满足计算规则)。

通过本文的介绍,介绍了 GBase 8s 中自增列在 DML 操作中的使用方法,包括 INSERT、UPDATE 和 DELETE 语句中的行为。自增列的灵活使用可以简化数据操作,确保唯一标识符的自动生成。希望这些内容能帮助你在实际开发中更高效地使用自增列。如果你在使用过程中遇到任何问题,欢迎随时在社区中提问,我们在这里为你提供支持!

原文链接:www.gbase.cn/community/p…
更多精彩内容尽在南大通用GBase技术社区,南大通用致力于成为用户最信赖的数据库产品供应商。