金仓数据库KingbaseES 自增列特性
关键字:
KingbaseES、mysql、自增列,serial
1.Kes 的自增列
Kingbase本身具有的自整列的三种方式:
*Sequence *
序列本身并不是一个自增列数据类型,需要定义通过nextval才能使用,如下:
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, name varchar(10) );
insert into test_seq (name) values ('aaa');
insert into test_seq (id,name) values (5,'ddd');
insert into test_seq (name) values ('aaa');
--重置序列的最大值
select setval('seq_1',(select max(id) from test_seq)::BIGINT);
SEQUENCE与表没有任何关系,只有向插入数据时,直接引用SEQUENCE的值。 一个sequence可被多个用户同时使用,因此可能会存在gaps。
Serial
serial数据类型的底层实现仍是序列。KES的serial列是通过语法映射的方式,将smallserial、serial和bigserial映射为smallint、int和bigint类型,并且在列上附加了一个对应类型的序列。serial列的序列值是通过在列上附加了一个默认值表达式实现的。插入数据时,默认值表达直接调用了nextval序列函数产生序列值。
create table test_serial ( id serial primary key, name varchar(100) );
insert into test_serial(name) values ('aaa');
insert into test_serial(id,name) values (5,'ddd');
insert into test_serial(name) values ('aaa');
--重置serial
SELECT SETVAL((SELECT sys_get_serial_sequence(' test_serial', 'id')), 1, false);
*Identity *
Identity 类似于SQLserver 的自增列实现方式。使用方式如下:
create table test_identiy_1 ( id int generated always as identity (START WITH 1 INCREMENT BY 1) primary key , name varchar(100) );
insert into test_identiy_1(name) values ('aaa');
insert into test_identiy_1(name) values ('aaa');
insert into test_identiy_1(name) values ('bbb');
insert into test_identiy_1(id,name) values (5,'ccc');
insert into test_identiy_1(name) values ('ccc');
或者
create table test_identiy_2 ( id int generated by default as identity (START WITH 1 INCREMENT BY 1) primary key , name varchar(100) );
insert into test_identiy_2(name) values ('aaa');
insert into test_identiy_2(name) values ('bbb');
insert into test_identiy_2(id,name) values (5,'ccc');
insert into test_identiy_2(name) values ('ccc');
上两种定义的区别在于显示的插入值时,有没有使用标识列的值。 也可以使用如下方式:
create table t(id int identity(2,1),name text);
insert into t (name) values ('aaa');
insert into t (id,name) values (5,'ddd');
insert into t (name) values ('aaa');
SELECT * FROM t;
** [identity [(seed, increment)]**
在对列的说明中增加identity属性,表示该列是标识列。其中,seed装载到表中的第一个行所使用的值;increment 增量值,该值被添加到前一个已装载的行的标识值上。seed和increment只能取整数,值域和所在列的类型的值域相同;increment不能为0; 必须同时指定种子和增量,或者二者都不指定。如果二者都未指定,则取默认值 (1,1)。 当向表中添加新行时,KingbaseES将为该标识列提供一个唯一的、递增的值。
2.兼容Mysql模式中的自增列
兼容MySQL整型自增列 支持整型(tinyint、smallint、mediumint、int、bigint)以及浮点类型自增列(Float,double)
CREATE TABLE语句支持创建自增列
drop table t;
CREATE TABLE t(col INT AUTO_INCREMENT PRIMARY KEY,col2 int);
INSERT INTO t VALUES (NULL,1), (NULL,2),(5,3), (NULL,4), (NULL,5);
SELECT * FROM t;
自增列序列的值域为[1,数据类型的最大值],序列值永远为正整数,允许向自增列插入0和null。当自增列序列达到最大值时,序列值不会再增长。当向自增列中插入序列值时,如果序列值已存在,将会触发主键约束或唯一约束,而执行失败。 当指定自增列属性AUTO_INCREMENT时,总是会隐式添加NOT NULL约束,自增列上最终的NULL约束状态取决于NULL、NOT NULL和AUTO_INCREMENT三者中在SQL语句中排在最后的子句。'
支持GUC参数auto_increment_offset
SET auto_increment_offset=10; --起点 offset < increment
SET auto_increment_increment=100; --间隔 (1-65585)
drop table t;
CREATE TABLE t(col INT AUTO_INCREMENT PRIMARY KEY,col2 int);
INSERT INTO t VALUES (NULL,1), (NULL,2),(5,3), (NULL,4), (NULL,5);
SELECT * FROM t;
支持GUC参数auto_increment_increment
SET auto_increment_increment=100; --间隔 (1-65585)
drop table t;
CREATE TABLE t(col INT AUTO_INCREMENT PRIMARY KEY,col2 int);
INSERT INTO t VALUES (NULL,1), (NULL,2),(5,3), (NULL,4), (NULL,5);
SELECT * FROM t;