Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在PostgreSQL中设置自动递增主键?_Sql_Postgresql_Auto Increment - Fatal编程技术网

如何在PostgreSQL中设置自动递增主键?

如何在PostgreSQL中设置自动递增主键?,sql,postgresql,auto-increment,Sql,Postgresql,Auto Increment,我在PostgreSQL中有一个包含许多列的表,我想添加一个自动递增主键 我试图创建一个名为id的列,类型为BIGSERIAL,但pgadmin返回了一个错误: 有人知道如何解决这个问题吗?如何在不重新创建表的情况下在PostgreSQL中添加或创建自动递增主键?请尝试以下命令: ALTER TABLE your_table ADD COLUMN key_column BIGSERIAL PRIMARY KEY; 请使用与创建表的DB用户相同的DB用户进行尝试。postgresql中的自动递增

我在PostgreSQL中有一个包含许多列的表,我想添加一个自动递增主键

我试图创建一个名为
id
的列,类型为
BIGSERIAL
,但pgadmin返回了一个错误:

有人知道如何解决这个问题吗?如何在不重新创建表的情况下在PostgreSQL中添加或创建自动递增主键?

请尝试以下命令:

ALTER TABLE your_table ADD COLUMN key_column BIGSERIAL PRIMARY KEY;
请使用与创建表的DB用户相同的DB用户进行尝试。

postgresql中的自动递增主键: 步骤1,创建表格:

CREATE TABLE epictable
(
    mytable_key    serial primary key,
    moobars        VARCHAR(40) not null,
    foobars        DATE
);
el@voyager$ psql -U pgadmin -d kurz_prod -c "select * from epictable"
mytable_key  |        moobars        |  foobars   
-------------+-----------------------+------------
           1 | delicious moobars     | 2012-05-01
           2 | world wide interblags | 2012-05-02
(2 rows)
create sequence splog_adfarm_seq
    start 1
    increment 1
    NO MAXVALUE
    CACHE 1;
ALTER TABLE fact_stock_data_detail_seq
OWNER TO pgadmin;
CREATE table dummyTable (
    id SERIAL NOT NULL,
    name character varying(50)
)
第2步,像这样在表中插入值,注意第一个参数列表中没有指定mytable_键,这会导致默认序列自动递增。

insert into epictable(moobars,foobars) values('delicious moobars','2012-05-01')
insert into epictable(moobars,foobars) values('worldwide interblag','2012-05-02')
第3步,从表格中选择*:

CREATE TABLE epictable
(
    mytable_key    serial primary key,
    moobars        VARCHAR(40) not null,
    foobars        DATE
);
el@voyager$ psql -U pgadmin -d kurz_prod -c "select * from epictable"
mytable_key  |        moobars        |  foobars   
-------------+-----------------------+------------
           1 | delicious moobars     | 2012-05-01
           2 | world wide interblags | 2012-05-02
(2 rows)
create sequence splog_adfarm_seq
    start 1
    increment 1
    NO MAXVALUE
    CACHE 1;
ALTER TABLE fact_stock_data_detail_seq
OWNER TO pgadmin;
CREATE table dummyTable (
    id SERIAL NOT NULL,
    name character varying(50)
)
步骤4,解释输出:

CREATE TABLE epictable
(
    mytable_key    serial primary key,
    moobars        VARCHAR(40) not null,
    foobars        DATE
);
el@voyager$ psql -U pgadmin -d kurz_prod -c "select * from epictable"
mytable_key  |        moobars        |  foobars   
-------------+-----------------------+------------
           1 | delicious moobars     | 2012-05-01
           2 | world wide interblags | 2012-05-02
(2 rows)
create sequence splog_adfarm_seq
    start 1
    increment 1
    NO MAXVALUE
    CACHE 1;
ALTER TABLE fact_stock_data_detail_seq
OWNER TO pgadmin;
CREATE table dummyTable (
    id SERIAL NOT NULL,
    name character varying(50)
)
请注意,mytable_键列已自动递增

ProTip:

CREATE TABLE epictable
(
    mytable_key    serial primary key,
    moobars        VARCHAR(40) not null,
    foobars        DATE
);
el@voyager$ psql -U pgadmin -d kurz_prod -c "select * from epictable"
mytable_key  |        moobars        |  foobars   
-------------+-----------------------+------------
           1 | delicious moobars     | 2012-05-01
           2 | world wide interblags | 2012-05-02
(2 rows)
create sequence splog_adfarm_seq
    start 1
    increment 1
    NO MAXVALUE
    CACHE 1;
ALTER TABLE fact_stock_data_detail_seq
OWNER TO pgadmin;
CREATE table dummyTable (
    id SERIAL NOT NULL,
    name character varying(50)
)

您应该始终在表上使用主键,因为postgresql在内部使用哈希表结构来提高插入、删除、更新和选择的速度。如果主键列(强制唯一且非null)可用,则可以依赖它为哈希函数提供唯一的种子。如果没有可用的主键列,则哈希函数会变得效率低下,因为它会选择其他列集作为键。

使用自定义序列在postgresql中创建自动递增的主键:

CREATE TABLE epictable
(
    mytable_key    serial primary key,
    moobars        VARCHAR(40) not null,
    foobars        DATE
);
el@voyager$ psql -U pgadmin -d kurz_prod -c "select * from epictable"
mytable_key  |        moobars        |  foobars   
-------------+-----------------------+------------
           1 | delicious moobars     | 2012-05-01
           2 | world wide interblags | 2012-05-02
(2 rows)
create sequence splog_adfarm_seq
    start 1
    increment 1
    NO MAXVALUE
    CACHE 1;
ALTER TABLE fact_stock_data_detail_seq
OWNER TO pgadmin;
CREATE table dummyTable (
    id SERIAL NOT NULL,
    name character varying(50)
)
第1步,创建序列:

CREATE TABLE epictable
(
    mytable_key    serial primary key,
    moobars        VARCHAR(40) not null,
    foobars        DATE
);
el@voyager$ psql -U pgadmin -d kurz_prod -c "select * from epictable"
mytable_key  |        moobars        |  foobars   
-------------+-----------------------+------------
           1 | delicious moobars     | 2012-05-01
           2 | world wide interblags | 2012-05-02
(2 rows)
create sequence splog_adfarm_seq
    start 1
    increment 1
    NO MAXVALUE
    CACHE 1;
ALTER TABLE fact_stock_data_detail_seq
OWNER TO pgadmin;
CREATE table dummyTable (
    id SERIAL NOT NULL,
    name character varying(50)
)
第2步,创建表格

CREATE TABLE splog_adfarm
(
    splog_key    INT unique not null,
    splog_value  VARCHAR(100) not null
);
insert into splog_adfarm values (
    nextval('splog_adfarm_seq'), 
    'Is your family tree a directed acyclic graph?'
);

insert into splog_adfarm values (
    nextval('splog_adfarm_seq'), 
    'Will the smart cookies catch the crumb?  Find out now!'
);
第3步,插入表格中

CREATE TABLE splog_adfarm
(
    splog_key    INT unique not null,
    splog_value  VARCHAR(100) not null
);
insert into splog_adfarm values (
    nextval('splog_adfarm_seq'), 
    'Is your family tree a directed acyclic graph?'
);

insert into splog_adfarm values (
    nextval('splog_adfarm_seq'), 
    'Will the smart cookies catch the crumb?  Find out now!'
);
第4步,观察行

el@defiant ~ $ psql -U pgadmin -d kurz_prod -c "select * from splog_adfarm"

splog_key |                            splog_value                             
----------+--------------------------------------------------------------------
        1 | Is your family tree a directed acyclic graph?
        2 | Will the smart cookies catch the crumb?  Find out now!
(3 rows)
这两行的键从1开始,按序列定义递增1

精英奖励计划:

CREATE TABLE epictable
(
    mytable_key    serial primary key,
    moobars        VARCHAR(40) not null,
    foobars        DATE
);
el@voyager$ psql -U pgadmin -d kurz_prod -c "select * from epictable"
mytable_key  |        moobars        |  foobars   
-------------+-----------------------+------------
           1 | delicious moobars     | 2012-05-01
           2 | world wide interblags | 2012-05-02
(2 rows)
create sequence splog_adfarm_seq
    start 1
    increment 1
    NO MAXVALUE
    CACHE 1;
ALTER TABLE fact_stock_data_detail_seq
OWNER TO pgadmin;
CREATE table dummyTable (
    id SERIAL NOT NULL,
    name character varying(50)
)
程序员讨厌打字,而把
nextval('splog\u adfarm\u seq')
打字很烦人。您可以为该参数键入
DEFAULT
,如下所示:

insert into splog_adfarm values (
    DEFAULT, 
    'Sufficient intelligence to outwit a thimble.'
);

要使上述操作正常工作,必须在splog_adfarm表上为该键列定义一个默认值。哪个更漂亮。

如果您想在pgadmin中执行此操作,则更容易。在postgressql中,要向列添加自动增量,我们首先需要创建一个自动增量序列并将其添加到所需的列中。我确实喜欢这个

1) 首先,您需要确保表有一个主键。还要将主键的数据类型保留为bigint或smallint。(我使用了bigint,找不到其他答案中提到的名为serial的数据类型)

2) 然后右键单击序列->添加新序列,添加序列。 如果表中没有数据,请保持顺序不变,不要进行任何更改。留着吧。 如果存在现有数据,请将主键列中的最后一个或最高值添加到定义选项卡中的当前值,如下所示。

3) 最后,将行
nextval('your_sequence\u name'::regclass)
添加到主键中的默认值,如下所示


确保此处的序列名称正确无误。这就是全部,自动增量应该可以工作。

也许我回答这个问题有点晚了,但我正在工作中研究这个主题:)

我想写一栏‘a_代码’=c1,c2,c3,c4

首先,我打开了一列,名为
ref\u id
,类型为
serial
。 然后我用这个命令解决了我的问题:

update myschema.mytable set a_code=cast('c'||"ref_id" as text) 

如果要在序列中使用数字,请使用以下内容定义新序列

CREATE SEQUENCE public.your_sequence
    INCREMENT 1
    START 1
    MINVALUE 1
;
然后更改表以使用id的顺序:

ALTER TABLE ONLY table ALTER COLUMN id SET DEFAULT nextval('your_sequence'::regclass);

我已尝试使用以下脚本成功地在PostgreSQL中自动递增主键

CREATE SEQUENCE dummy_id_seq
    START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;

CREATE table dummyTable (
    id bigint DEFAULT nextval('dummy_id_seq'::regclass) NOT NULL,
    name character varying(50)
);
编辑:

CREATE TABLE epictable
(
    mytable_key    serial primary key,
    moobars        VARCHAR(40) not null,
    foobars        DATE
);
el@voyager$ psql -U pgadmin -d kurz_prod -c "select * from epictable"
mytable_key  |        moobars        |  foobars   
-------------+-----------------------+------------
           1 | delicious moobars     | 2012-05-01
           2 | world wide interblags | 2012-05-02
(2 rows)
create sequence splog_adfarm_seq
    start 1
    increment 1
    NO MAXVALUE
    CACHE 1;
ALTER TABLE fact_stock_data_detail_seq
OWNER TO pgadmin;
CREATE table dummyTable (
    id SERIAL NOT NULL,
    name character varying(50)
)

SERIAL关键字自动为各个列创建序列。

在PgAdmin上执行此操作的步骤:

  • 创建序列SEQUENCE_title START 1;//如果表存在最后一个id
  • 将此序列添加到主键,表-属性-列-列id(主键)编辑-约束-添加到 该字段为默认值

一个小小的吹毛求疵,
SERIAL
确实在幕后创建了一个
序列
:是否可以在表中创建主键(现有列),而不添加任何新列使用
thing\u id int references epictable(mytable\u key)声明的外键是否有效?(这里的关键是使用串行或BIGSERIAL数据类型,它在幕后创建一个序列,并在插入时递增/使用该序列),如果要从另一个表引用该序列,请使用integer或bigint@satishkilari:是,语法为
altertable mytable ADD主键(列)
.Postgresql将检查列是否不包含空值。在pgAdmin 4中获取此错误。
bigserial
serial
都给出了相同的错误:
error:syntax error在“bigserial”处或附近
使用bigserial或serial时也会出现语法错误。是否有最低限度的postgresql验证?自定义序列的好处是什么?可能是安全性?Masi自定义序列的一个用途是使主/主复制更容易-如果两个数据中心之间的数据链路断开,这将非常有用-allo在两台具有不同ID的服务器上创建wing记录,这样可以方便地同步数据库备份,同时将生成的ID保留在不同的位置。是否可以生成主键(现有列)在不添加任何新列的表中,我是否必须为每个表创建一个新序列?您可以为不同的表共享相同的序列,但每个表中的每条记录的序列都会增加。您是否可以像对序列那样重置序列?是的!我已使用
ALTER sequence dummytable\u id\u seq检查过用1重新启动判定元件