KingbaseES中一共3种自增长类型sequence,serial,identity,他们的区别如下表:

SEQUENCE:
create sequence seq_1 increment by 1 minvalue 1 start with 1 ;
create table test_seq(id int not null default nextval('seq_1') primary key) ;
SELECT * FROM test ;
SELECT currval('test_serial')
INSERT INTO test values('c',8) ; ---手工添加id
SELECT setval('test_serial',(SELECT max(id) FROM test LIMIT 1)); ----设置自增序列当前值是手工添加之后的id;
INSERT INTO test(sex) VALUES ('b') ;
alter sequence sql_1 restart with 1; ------重置序列
Serial
SELECT sys_get_serial_sequence(' test','id'); ---查看表的id序列名称
SELECT SETVAL((SELECT sys_get_serial_sequence(' test','id')),1);
SELECT setval('test_serial',1); ---重置serial
Identity
R6数据库支持,R3数据库不支持
id int generated always as identity (START WITH 1 INCREMENT BY 1) primary key ---不允许显示插入
id int generated always as identity (START WITH 1 INCREMENT BY 1) primary key
overriding system value --可以显示插入
例子: insert intotest_identiy_1(id,name)overriding system value values (5,'ccc');
id int generated by default as identity (START WITH 1 INCREMENT BY 1) primary key ---允许显示插入
查看表上id字段序列值
SELECT sys_get_serial_sequence(' test','id');
重置identity2种方法:
alter table test_seq alter COLUMN id restart with 1;
truncate table test_seq restart identity;




