【MogDB/openGauss如何实现自增主键】

news/2024/7/9 23:07:10 标签: postgresql, linux, 数据库

自增主键是我们在设计数据库表结构时经常使用的主键生成策略,主键的生成可以完全依赖数据库,无需人为干预,在新增数据的时候,我们只需要将主键的值设置为default,数据库就会为我们自动生成一个主键值。

MySQL主键自增使用AUTO_INCREMENT关键字,PostgreSQL自增使用SERIAL关键字或者序列。
而MogDB/openGauss里兼容两种语法。AUTO_INCREMENT在MogDB-3.1.0/openGauss-5.0.0以上适配。

下文会针对MogDB/openGauss里几种自增主键的实现进行一个简单的验证。

一、MySQL的方式(AUTO_INCREMENT)

注意,AUTO_INCREMENT功能,只有在MogDB/openGauss的B兼容模式下才可以使用,否则将会有如此下的提示。

MogDB=#  SELECT current_database(); 
 current_database 
------------------
 postgres
(1 row)

MogDB=# \l postgres
                                 List of databases
   Name   | Owner | Encoding | Collate | Ctype | Access privileges | Compatibility 
----------+-------+----------+---------+-------+-------------------+---------------
 postgres | om5   | UTF8     | C       | C     |                   | A
(1 row)

MogDB=# CREATE TABLE test_create_autoinc(id bool auto_increment primary key, name varchar(200),a int) auto_increment=1;
ERROR:  auto_increment is supported only in B-format database
MogDB=# 

正确的使用方式如下:

MogDB=# create database db_mysql with dbcompatibility ='B';
CREATE DATABASE
MogDB=# \c db_mysql 
Non-SSL connection (SSL connection is recommended when requiring high-security)
You are now connected to database "db_mysql" as user "om5".
db_mysql=# CREATE TABLE test_create_autoinc_source(id int auto_increment primary key) AUTO_INCREMENT = 100;
NOTICE:  CREATE TABLE will create implicit sequence "test_create_autoinc_source_id_seq" for serial column "test_create_autoinc_source.id"
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "test_create_autoinc_source_pkey" for table "test_create_autoinc_source"
CREATE TABLE

db_mysql=# \d test_create_autoinc_source
 Table "public.test_create_autoinc_source"
 Column |  Type   |        Modifiers        
--------+---------+-------------------------
 id     | integer | not null AUTO_INCREMENT
Indexes:
    "test_create_autoinc_source_pkey" PRIMARY KEY, btree (id) TABLESPACE pg_default


--->插入值进行验证

db_mysql=# INSERT INTO test_create_autoinc_source VALUES(DEFAULT);
INSERT 0 1
db_mysql=# INSERT INTO test_create_autoinc_source VALUES(DEFAULT);
INSERT 0 1
db_mysql=# SELECT id FROM test_create_autoinc_source ORDER BY 1;
 id  
-----
 100
 101
(2 rows)

二、PostgreSQL的方式(SERIAL)

postgresql(10+)提供了三种serial类型:smallserial,serial,bigserial,他不是真正的类型,而是在创建唯一标识符列的标志以方便使用。bigserial会创建一个bigint类型的自增,serial用以创建一个int类型的自增,smallserial用以创建一个smallint类型的自增,这几种类型在MogDB/openGauss里都是支持的。serial的MAXVALUE=9223372036854775807,起始值为1。
自增列的默认值是nextval(‘table_name_seq’::regclass)。

方式一

MogDB=# create table test_serial_a1(
id serial,
name character varying(256),
constraint pk_test_serial_id primary key( id)
);
NOTICE:  CREATE TABLE will create implicit sequence "test_serial_a1_id_seq" for serial column "test_serial_a1.id"
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "pk_test_serial_id" for table "test_serial_a1"
CREATE TABLE

MogDB=# \d test_serial_a1
                                 Table "public.test_serial_a1"
 Column |          Type          |                          Modifiers                          
--------+------------------------+-------------------------------------------------------------
 id     | integer                | not null default nextval('test_serial_a1_id_seq'::regclass)
 name   | character varying(256) | 
Indexes:
    "pk_test_serial_id" PRIMARY KEY, btree (id) TABLESPACE pg_default

MogDB=# \d test_serial_a1_id_seq
     Sequence "public.test_serial_a1_id_seq"
    Column     |  Type   |         Value         
---------------+---------+-----------------------
 sequence_name | name    | test_serial_a1_id_seq
 last_value    | bigint  | 2
 start_value   | bigint  | 1
 increment_by  | bigint  | 1
 max_value     | bigint  | 9223372036854775807
 min_value     | bigint  | 1
 cache_value   | bigint  | 1
 log_cnt       | bigint  | 31
 is_cycled     | boolean | f
 is_called     | boolean | t
 uuid          | bigint  | 0
Owned by: public.test_serial_a1.id

--->插入值进行验证

MogDB=# insert into test_serial_a1 values(DEFAULT,'no1'); 
INSERT 0 1
MogDB=# insert into test_serial_a1 values(DEFAULT,'no1');
INSERT 0 1
MogDB=# SELECT * FROM test_serial_a1;
 id | name 
----+------
  1 | no1
  2 | no1
(2 rows)

方式二

MogDB=# create table test_serial_a2(
id serial PRIMARY KEY,
name character varying(256)
);
NOTICE:  CREATE TABLE will create implicit sequence "test_serial_a2_id_seq" for serial column "test_serial_a2.id"
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "test_serial_a2_pkey" for table "test_serial_a2"
CREATE TABLE

MogDB=# \d test_serial_a2
                                 Table "public.test_serial_a2"
 Column |          Type          |                          Modifiers                          
--------+------------------------+-------------------------------------------------------------
 id     | integer                | not null default nextval('test_serial_a2_id_seq'::regclass)
 name   | character varying(256) | 
Indexes:
    "test_serial_a2_pkey" PRIMARY KEY, btree (id) TABLESPACE pg_default



--->插入值进行验证

MogDB=# insert into test_serial_a2 values(DEFAULT,'no1');
INSERT 0 1
MogDB=# insert into test_serial_a2 values(DEFAULT,'no1');
INSERT 0 1
MogDB=# SELECT * FROM test_serial_a2;
 id | name 
----+------
  1 | no1
  2 | no1
(2 rows)

这两种方法用的是PostgreSQL的serial类型实现自增,drop表的时候指定的序列也会drop掉。

三、基于序列

基于序列的方式其实和第二种的基于serial的思路一样,一般的主键表,没有使用serial类型,那么我们可以通过创建序列,并在建表的时候指定默认值字段为序列的nextval来实现。

1.手动创建序列

MogDB=# CREATE SEQUENCE test_aaa_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
CREATE SEQUENCE

MogDB=# \d test_aaa_id_seq
       Sequence "public.test_aaa_id_seq"
    Column     |  Type   |        Value        
---------------+---------+---------------------
 sequence_name | name    | test_aaa_id_seq
 last_value    | bigint  | 1
 start_value   | bigint  | 1
 increment_by  | bigint  | 1
 max_value     | bigint  | 9223372036854775807
 min_value     | bigint  | 1
 cache_value   | bigint  | 1
 log_cnt       | bigint  | 0
 is_cycled     | boolean | f
 is_called     | boolean | f
 uuid          | bigint  | 0

2.创建主键表

MogDB=#  create table test_bbb (
id integer PRIMARY KEY default nextval('test_aaa_id_seq'::regclass),
name character varying(128)
);
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "test_bbb_pkey" for table "test_bbb"
CREATE TABLE
MogDB=# \d test_bbb
                                 Table "public.test_bbb"
 Column |          Type          |                       Modifiers                       
--------+------------------------+-------------------------------------------------------
 id     | integer                | not null default nextval('test_aaa_id_seq'::regclass)
 name   | character varying(128) | 
Indexes:
    "test_bbb_pkey" PRIMARY KEY, btree (id) TABLESPACE pg_default


--->插入值进行验证
MogDB=# insert into test_bbb values(DEFAULT,'no1');
INSERT 0 1
MogDB=# insert into test_bbb values(DEFAULT,'no2');
INSERT 0 1
MogDB=# select * from test_bbb;
 id | name 
----+------
  1 | no1
  2 | no2
(2 rows)

也可以创建完表、创建完序列后,使用alter语句,将序列赋值给主键,如下语句所示:

alter table test_aaa alter column id set default nextval('test_aaa_id_seq');

这种自行使用序列的方法在drop表的时候序列不会随着drop掉


http://www.niftyadmin.cn/n/5157352.html

相关文章

餐饮加盟信息展示预约小程序的内容如何

餐饮业规模持续增加,相关从业者逐渐增多,对中等规模以上的餐饮品牌来说,当有一定规模后除了开多家直营店外,还会开放招商加盟,扩展品牌、提升营收等。 由于餐饮加盟属于准属性业务,因此传统线下方式不太适…

JAVA中的内存泄漏和需要手动关闭的资源

1、java中什么是内存泄漏 在Java中,内存泄漏指的是程序中存在对不再需要的对象持续引用,导致这些对象无法被垃圾回收,从而占据了不必要的内存空间。这些对象会一直留在堆内存中,导致可用内存逐渐减少,最终可能导致Out…

centos获取服务器公网ip

查看公网IP 用下面几个命令: #curl ifconfig.me #curl icanhazip.com #curl cip.cc

华为政企园区网络交换机产品集

产品类型产品型号产品说明 核心/汇聚交换机CloudEngine S5731-H24P4XCCloudEngine S5731-H24P4XC 提供 24个10/100/1000BASE-T以太网端口,4个万兆SFP,CloudEngine S5731-H 系列交换机是华为公司推出的新一代智能千兆交换机,基于华为公司统…

maven 项目添加 git-hook 脚本,约束提交内容格式

git 提交代码&#xff0c;推送代码&#xff0c;可以通过在 .git/hooks 目录中的 bash 脚本来做一定的验证工作。 本例使用插件 maven-antrun-plugin 自动输出脚本至 .git/hooks 目录中&#xff0c;在 pom.xml 中的使用示例如下&#xff1a; <plugin><groupId>org.…

比SAM小60倍的分割一切模型:MobileSAM

1 MobileSAM SAM就是一类处理图像分割任务的通用模型。与以往只能处理某种特定类型图片的图像分割模型不同&#xff0c;SAM可以处理所有类型的图像。 在SAM出现前&#xff0c;基本上所有的图像分割模型都是专有模型。比如&#xff0c;在医学领域&#xff0c;有专门分割核磁图…

AJAX-解决回调函数地狱问题

一、同步代码和异步代码 1.同步代码 浏览器是按照我们书写代码的顺序一行一行地执行程序的。浏览器会等待代码的解析和工作&#xff0c;在上一行完成之后才会执行下一行。这也使得它成为一个同步程序。 总结来说&#xff1a;逐行执行&#xff0c;需原地等待结果后&#xff0…

关于 HTML 的一切:初学者指南

HTML 代表超文本标记语言&#xff0c;是用于创建网页和 Web 应用程序的标准语言。 本指南将全面介绍 HTML&#xff0c;涵盖从基本语法和语义到更高级功能的所有内容。 我的目标是用简单的术语解释 HTML&#xff0c;以便即使没有编码经验的人也能学习如何使用 HTML 构建网页。…