ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

KingbaseES 自增列三种方式

2021-11-27 14:32:05  阅读:200  来源: 互联网

标签:insert name seq into 增列 三种 test values KingbaseES


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;

  

标签:insert,name,seq,into,增列,三种,test,values,KingbaseES
来源: https://www.cnblogs.com/kingbase/p/15611849.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有