MogDB中也提供了sequence序列功能,使用Oracle的用户应该都非常喜欢使用这个功能。所以如果从Oracle迁移到MogDB,那么这项功能可以完全替代了。
接下来我们简单测试一下:
[omm@test26 ~]$ gsql -d enmotech -Uroger -p26000
Password for user roger:
gsql ((MogDB 2.0.1 build f892ccb7) compiled at 2021-07-09 16:12:59 commit 0 last mr )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.
enmotech=> drop table test;
DROP TABLE
enmotech=> create table test(id serial,name varchar(20));
NOTICE: CREATE TABLE will create implicit sequence "test_id_seq" for serial column "test.id"
CREATE TABLE
enmotech=> \d+ test
Table "public.test"
Column | Type | Modifiers | Storage | Stats target | Description
--------+-----------------------+---------------------------------------------------+----------+--------------+-------------
id | integer | not null default nextval('test_id_seq'::regclass) | plain | |
name | character varying(20) | | extended | |
Has OIDs: no
Options: orientation=row, compression=no
enmotech=> insert into test values (nextval('test_id_seq'),'enmotech');
INSERT 0 1
enmotech=> insert into test values (nextval('test_id_seq'),'killdb.com');
INSERT 0 1
enmotech=> insert into test values (nextval('test_id_seq'),'www.killdb.com');
INSERT 0 1
enmotech=> select * from test;
id | name
----+----------------
2 | enmotech
3 | killdb.com
4 | www.killdb.com
(3 rows)
同时我们也可以单独创建序列,然后指定给某个表所使用,如下是create sequence的语法:
CREATE SEQUENCE name [ INCREMENT [ BY ] increment ]
[ MINVALUE minvalue | NO MINVALUE | NOMINVALUE ] [ MAXVALUE maxvalue | NO MAXVALUE | NOMAXVALUE]
[ START [ WITH ] start ] [ CACHE cache ] [ [ NO ] CYCLE | NOCYCLE ]
[ OWNED BY { table_name.column_name | NONE } ];
接下来我们单独创建使用sequence试试:
enmotech=> create sequence kill_seq cache 1000;
CREATE SEQUENCE
enmotech=>
enmotech=> drop table test;
DROP TABLE
enmotech=> create table test(id int not null default nextval('kill_seq'),name varchar(200));
CREATE TABLE
enmotech=>
enmotech=> \d+ test
Table "public.test"
Column | Type | Modifiers | Storage | Stats target | Description
--------+------------------------+------------------------------------------------+----------+--------------+-------------
id | integer | not null default nextval('kill_seq'::regclass) | plain | |
name | character varying(200) | | extended | |
Has OIDs: no
Options: orientation=row, compression=no
enmotech=> alter sequence kill_seq increment by 10 NOCYCLE;
ERROR: ALTER SEQUENCE is not yet supported.
enmotech=>
enmotech=> \d+ kill_seq
Sequence "public.kill_seq"
Column | Type | Value | Storage
---------------+---------+---------------------+---------
sequence_name | name | kill_seq | plain
last_value | bigint | 1000 | plain
start_value | bigint | 1 | plain
increment_by | bigint | 1 | plain
max_value | bigint | 9223372036854775807 | plain
min_value | bigint | 1 | plain
cache_value | bigint | 1000 | plain
log_cnt | bigint | 32 | plain
is_cycled | boolean | f | plain
is_called | boolean | t | plain
uuid | bigint | 0 | plain
enmotech=> alter sequence kill_seq nomaxvalue;
ALTER SEQUENCE ^
enmotech=> alter sequence kill_seq cache 10000;
ERROR: ALTER SEQUENCE is not yet supported.
enmotech=> alter sequence kill_seq start 888;
ERROR: ALTER SEQUENCE is not yet supported.
enmotech=> \d+ kill_seq
Sequence "public.kill_seq"
Column | Type | Value | Storage
---------------+---------+---------------------+---------
sequence_name | name | kill_seq | plain
last_value | bigint | 1000 | plain
start_value | bigint | 1 | plain
increment_by | bigint | 1 | plain
max_value | bigint | 9223372036854775807 | plain
min_value | bigint | 1 | plain
cache_value | bigint | 1000 | plain
log_cnt | bigint | 0 | plain
is_cycled | boolean | f | plain
is_called | boolean | t | plain
uuid | bigint | 0 | plain
尽管sequence的属性跟Oracle类似,但是我们可以看到,目前MogDB暂时还不支持alter sequence的方式去修改序列增长步长或其他属性。只能修改owner属主。
查了一下官方文档,发现alter sequence只支持如下的语法操作:
ALTER SEQUENCE [ IF EXISTS ] name
[MAXVALUE maxvalue | NO MAXVALUE | NOMAXVALUE]
[ OWNED BY { table_name.column_name | NONE } ] ;
需要注意的是,mogDB中的sequence跟Oracle 中的序列不一样的是,Oracle 由于集群的原因,序列还存在一个order或noorder选项。
但在MogDB中sequence是不存在这个属性的。