KingbaseES中3种自增长类型sequence,serial,identity的区别:

对象

显示插入

显示插入后更新最大值

清空表后是否重置

是否跟事务一起回滚

多对象共享

支持重置

出现错误后序列值是否增长

sequence

serial

identity

1、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,
name varchar(10)
); 隐式插入
insert into test_seq (name) values ('aaa');
insert into test_seq (name) values ('bbb');
insert into test_seq (name) values ('ccc');

显式插入
insert into test_seq (id,name) values (5,'ddd');

再次隐式插入
test1=# insert into test_seq (name) values ('eee');
INSERT 0 1
test1=# insert into test_seq (name) values ('fff');
错误: 重复键违反唯一约束"test_seq_pkey"
描述: 键值"(id)=(5)" 已经存在

再次执行语句可正常插入,序列因为之前的错误调用自动增加
test1=# insert into test_seq (name) values ('fff');
INSERT 0 1

重置序列

--重置序列的最大值
select setval('seq_1',(select max(id) from test_seq)::BIGINT); --事务回滚后,序列号并不会回滚
test1=# begin;
BEGIN
test1=# insert into test_seq (name) values ('ggg');
INSERT 0 1
test1=# rollback;
ROLLBACK -- truncate 表之后,序列不受影响
test1=# truncate table test_seq;
TRUNCATE TABLE
test1=# insert into test_seq (name) values ('ggg');
INSERT 0 1
test1=# select * from test_seq;
id | name
----+------
9 | ggg
(1 行记录) --重置序列
ALTER SEQUENCE seq_1 RESTART WITH 1;
test1=# ALTER SEQUENCE seq_1 RESTART WITH 1;
ALTER SEQUENCE
test1=# insert into test_seq (name) values ('ggg');
INSERT 0 1
test1=# select * from test_seq;
id | name
----+------
9 | ggg
1 | ggg

  

2、Serial

create table  test_serial
(
id serial primary key,
name varchar(100)
);
隐式插入
insert into test_serial(name) values ('aaa');
insert into test_serial(name) values ('bbb');
insert into test_serial(name) values ('ccc');
显示插入
insert into test_serial(id,name) values (5,'ddd);
select * from test_serial; --再次隐式插入,第二次会报错
test1=# insert into test_serial(id,name) values (5,'ddd);
INSERT 0 1
test1=# insert into test_serial(name) values ('eee');
INSERT 0 1
test1=# insert into test_serial(name) values ('fff');
错误: 重复键违反唯一约束"test_serial_pkey"
描述: 键值"(id)=(5)" 已经存在 --再次执行语句可正常插入,序列因为之前的错误调用自动增加
test1=# insert into test_serial(name) values ('fff');
INSERT 0 1 --重置serial
SELECT SETVAL((SELECT sys_get_serial_sequence(' test_serial', 'id')), 1, false);

  

3、Identity

Identity是R6版本新增的语法,R3数据库不支持该语法。

identity定义成generated by default as identity允许显式插入,

identity定义成always as identity 不允许显示插入,但是加上overriding system value也可以显式插入。

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 ('bbb');
insert into test_identiy_1(name) values ('ccc'); --显式插入值
如果定义为generated always as identity则不允许显式插入,除非增加overriding system value 提示。
test1=# insert into test_identiy_1(id,name) values (5,'ccc');
错误: 无法插入到列"id"
描述: 列"id"是定义为GENERATED ALWAYS的标识列.
提示: 使用OVERRIDING SYSTEM VALUE覆盖.
test1=# insert into test_identiy_1(id,name)overriding system value values (5,'ccc');
INSERT 0 1

generate by default:

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(name) values ('ccc'); test1=# insert into test_identiy_2(id,name) values (5,'ccc');
INSERT 0 1

重置 Identity  

重置Identity的方式有2种:
1. ALTER TABLE test_identiy_1 ALTER COLUMN id RESTART WITH 100;
2. TRUNCATE table test_identiy_1 RESTART IDENTITY;

  

最新文章

  1. linux centos 6.5下安装nodejs
  2. Nancy之基于Nancy.Hosting.Self的小Demo
  3. UML类图简单介绍
  4. VC保存当面某个区域的图片
  5. NFS工作原理及配置文件详解
  6. 网页二维码推广App的实现
  7. easyUI之tabs
  8. C语言之移位操作
  9. css中文字体乱码解决方案
  10. 【应用】Markdown 在线阅读器
  11. MY WAY程序(十八) 团队开发
  12. lightOJ 1017 Brush (III) DP
  13. java学习笔记之字符流文件复制
  14. mxnet框架样本,使用C++接口
  15. (二)jdk8学习心得之Lambda表达式
  16. Python3 tkinter基础 Scale orient 横竖 resolution单步步长 length 长度 tickinterval 指示刻度
  17. zuul源码(2)
  18. JS模块化工具require.js教程(一):初识require.js
  19. IO在Socket中的应用
  20. 2017-12-18python全栈9期第三天第四节之str的索引与切片

热门文章

  1. easyui combobox重复渲染问题
  2. 在两台配置了Win10操作系统的电脑之间直接拷贝文件
  3. 安装pystaller
  4. 搭建zabbix及报错处理
  5. Python 中多线程共享全局变量的问题
  6. 基于.NetCore开发博客项目 StarBlog - (14) 实现主题切换功能
  7. Docker容器搭建android编译环境
  8. idea 内置tomcat jersey 跨服务器 上传文件报400错误
  9. Maven3 入门到入门
  10. [HNOI2009]【一本通提高组合数学】有趣的数列