KingbaseES 自增列三种方式

1、Sequence
1 create sequence seq_1 INCREMENT BY 1 MINVALUE 1 START WITH 1;
2 create table test_seq
3 (
4     id int not null default nextval(‘seq_1’) primary key,
5     name varchar(10)
6 );
7  
8 隐式插入
9 insert into test_seq (name) values (‘aaa’);
10 insert into test_seq (name) values (‘bbb’);
11 insert into test_seq (name) values (‘ccc’);
12
显式插入
13 insert into  test_seq (id,name) values (5,‘ddd’);
14
再次隐式插入
15 test1=# insert into test_seq (name) values (‘eee’);
16 INSERT 0 1
17 test1=# insert into test_seq (name) values (‘fff’);
18 错误: 重复键违反唯一约束"test_seq_pkey"
19 描述: 键值"(id)=(5)" 已经存在
20
再次执行语句可正常插入,序列因为之前的错误调用自动增加
21 test1=# insert into test_seq (name) values (‘fff’);
22 INSERT 0 1
重置序列
1 --重置序列的最大值
2 select setval(‘seq_1’,(select max(id) from  test_seq)::BIGINT);
3  
4 --事务回滚后,序列号并不会回滚
5 test1=# begin;
6 BEGIN
7 test1=# insert into  test_seq (name) values (‘ggg’);
8 INSERT 0 1
9 test1=# rollback;
10 ROLLBACK
11  
12 – truncate 表之后,序列不受影响
13 test1=# truncate table  test_seq;
14 TRUNCATE TABLE
15 test1=# insert into  test_seq (name) values (‘ggg’);
16 INSERT 0 1
17 test1=#  select * from test_seq;
18  id | name
19 ----±-----
20   9 | ggg
21 (1 行记录)
22  
23 --重置序列
24 ALTER SEQUENCE seq_1 RESTART WITH 1;
25 test1=# ALTER SEQUENCE  seq_1 RESTART WITH 1;
26 ALTER SEQUENCE
27 test1=# insert into  test_seq (name) values (‘ggg’);
28 INSERT 0 1
29 test1=#  select * from test_seq;                   
30  id | name
31 ----±-----
32   9 | ggg
33   1 | ggg
2、Serial
1 create table  test_serial
2 (
3     id serial primary key,
4     name varchar(100)
5 );
6 隐式插入
7 insert into   test_serial(name) values (‘aaa’);
8 insert into   test_serial(name) values (‘bbb’);
9 insert into   test_serial(name) values (‘ccc’);
10 显示插入
11 insert into   test_serial(id,name) values (5,‘ddd);
12 select * from  test_serial;
13  
14 --再次隐式插入,第二次会报错
15 test1=# insert into   test_serial(id,name) values (5,‘ddd);
16 INSERT 0 1
17 test1=# insert into   test_serial(name) values (‘eee’);
18 INSERT 0 1
19 test1=# insert into   test_serial(name) values (‘fff’);
20 错误:  重复键违反唯一约束"test_serial_pkey"
21 描述:  键值"(id)=(5)" 已经存在
22  
23 --再次执行语句可正常插入,序列因为之前的错误调用自动增加
24 test1=# insert into test_serial(name) values (‘fff’);
25 INSERT 0 1
26  
27 --重置serial
28 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也可以显式插入。
1 create table  test_identiy_1
2 (
3     id int generated always as identity (START WITH 1 INCREMENT BY 1)  primary key ,
4     name varchar(100)
5 );
6 insert into  test_identiy_1(name) values (‘aaa’);
7 insert into  test_identiy_1(name) values (‘bbb’);
8 insert into  test_identiy_1(name) values (‘ccc’);
9  
10 --显式插入值
11 如果定义为generated always as identity则不允许显式插入,除非增加overriding system value 提示。
12 test1=# insert into test_identiy_1(id,name) values (5,‘ccc’);
13 错误:  无法插入到列"id"
14 描述:  列"id"是定义为GENERATED ALWAYS的标识列.
15 提示:  使用OVERRIDING SYSTEM VALUE覆盖.
16 test1=# insert into test_identiy_1(id,name)overriding system value values (5,‘ccc’);
17 INSERT 0 1
 
generate by default:
1 create table  test_identiy_2
2 (
3     id int generated by default as identity (START WITH 1 INCREMENT BY 1)  primary key ,
4     name varchar(100)
5 );
6  
7 insert into  test_identiy_2(name) values (‘aaa’);
8 insert into  test_identiy_2(name) values (‘bbb’);
9 insert into  test_identiy_2(name) values (‘ccc’);
10  
11 test1=# insert into test_identiy_2(id,name) values (5,‘ccc’);
12 INSERT 0 1
 
重置 Identity
1 重置Identity的方式有2种:
2 1.  ALTER TABLE test_identiy_1 ALTER COLUMN id RESTART WITH 100;
3 2.  TRUNCATE table test_identiy_1 RESTART IDENTITY;

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值