加入收藏 | 设为首页 | 会员中心 | 我要投稿 辽源站长网 (https://www.0437zz.com/)- 云专线、云连接、智能数据、边缘计算、数据安全!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

sql表中主键id之间的差距

发布时间:2021-02-02 02:21:31 所属栏目:MsSql教程 来源:网络整理
导读:我有一张桌子是: CREATE SEQUENCE id_seq;CREATE TABLE public."UserInfo"( id bigint NOT NULL DEFAULT nextval('id_seq'),phone text,password text,name text,surname text,middle_name text,email text,company text,title text,image_id text,CONSTRAIN

我有一张桌子是:

CREATE SEQUENCE id_seq;
CREATE TABLE public."UserInfo"
(
  id bigint NOT NULL DEFAULT nextval('id_seq'),phone text,password text,name text,surname text,middle_name text,email text,company text,title text,image_id text,CONSTRAINT "UserInfo_pkey" PRIMARY KEY (id),CONSTRAINT "UserInfo_image_id_key" UNIQUE (image_id),CONSTRAINT "UserInfo_phone_key" UNIQUE (phone)
)
WITH (
  OIDS=FALSE
);
ALTER SEQUENCE id_seq OWNED BY public."UserInfo".id;
ALTER TABLE public."UserInfo"
  OWNER TO postgres;

当我对唯一列的相同值的插入做出错误请求时. “id”正在增加……这是错误的id请求;

ERROR:  null value in column "id" violates not-null constraint
DETAIL:  Failing row contains (null,9921455867,mg123209,name,surname,namesurname@xxx.com,Company Name,Title Of Person,123asd).
********** Error **********

这是我的表结果;

1;"1234477867";"qweff";"Name";"Surname";"''";"namesurname@qwe.com";"Company";"Title";"qwer1234"
4;"5466477868";"1235dsf";"Name";"Surname";"''";"banesyrna@pwqe.com";"Company";"Title";"qwer1235"
6;"5051377828";"asd123";"Name";"Surname";"''";"qweg@sdcv.com";"Company";"Title";"qwesr1235"

请帮助我如何解决这个问题,我想要顺序1,2,3 ..顺序..

解决方法

这是 sequences的工作方式.

Important: To avoid blocking concurrent transactions that obtain
numbers from the same sequence,a nextval operation is never rolled
back; that is,once a value has been fetched it is considered used and
will not be returned again. This is true even if the surrounding
transaction later aborts,or if the calling query ends up not using
the value.

正如评论中指出的那样,序列中存在差距是没有害处的.如果您删除表中的某些行,原因是您在主键值中创建了间隙,并且通常不会重置它们以使它们顺序.

如果您坚持创建无间隙序列,请阅读本文:http://www.varlena.com/GeneralBits/130.php并为慢速插入做好准备.

(编辑:辽源站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读