我知道使用SERIAL主键的PostgreSQL表最终会有一个由PostgreSQL创建的隐式索引,序列和约束.问题是在重命名表时如何重命名这些隐式对象.下面是我尝试在最后通过具体问题解决这个问题.
给出一个像这样的表
CREATE TABLE foo (
pkey SERIAL PRIMARY KEY,
value INTEGER
);
Run Code Online (Sandbox Code Playgroud)
Postgres输出
CREATE TABLE foo (
pkey serial NOT NULL,
value integer,
CONSTRAINT foo_pkey PRIMARY KEY (pkey )
);
ALTER TABLE foo OWNER TO postgres;
Run Code Online (Sandbox Code Playgroud)
PgAdmin III显示以下作为表的DDL
ALTER table foo RENAME TO bar;
Run Code Online (Sandbox Code Playgroud)
现在重命名表
CREATE TABLE bar (
pkey integer NOT NULL DEFAULT nextval('foo_pkey_seq'::regclass),
value integer,
CONSTRAINT foo_pkey PRIMARY KEY (pkey )
);
ALTER TABLE bar OWNER TO postgres;
Run Code Online (Sandbox Code Playgroud)
Postgres输出
ALTER SEQUENCE foo_pkey_seq RENAME TO bar_pkey_seq;
Run Code Online (Sandbox Code Playgroud)
表格的PgAdmin III …
我想知道何时选择序列更好,何时使用串口更好.
我想要的是在插入后使用返回最后一个值
SELECT LASTVAL();
Run Code Online (Sandbox Code Playgroud)
我读了这个问题 PostgreSQL Autoincrement
我从来没有使用过串口.
我正在开发一个Ruby on Rails应用程序.我们正在使用PostgreSQL数据库.
有一个以scores下列列命名的表:
Column | Type
--------------+-----------------------
id | integer
value | double precision
ran_at | timestamp
active | boolean
build_id | bigint
metric_id | integer
platform_id | integer
mode_id | integer
machine_id | integer
higher_better | boolean
job_id | integer
variation_id | integer
step | character varying(255)
Run Code Online (Sandbox Code Playgroud)
我需要一个添加序列来job_id(注:没有模型job).
如何创建此序列?
postgresql activerecord ruby-on-rails rails-postgresql rails-activerecord
我如何串行更改表列。这个专栏已经有数据了,我不想丢失它们..我试过这个:
ALTER TABLE tbl_user ADD COLUMN usr_id SERIAL PRIMARY KEY;
Run Code Online (Sandbox Code Playgroud)
我收到这个错误
错误:关系“tbl_user”的列“usr_id”已经存在********** Erreur ************
我也试过这个:
UPDATE tbl_user SET usr_id = nextval('seq_user') WHERE usr_id IS NULL;
查询成功返回:0 行受影响,71 毫秒执行时间。
查询成功但不更改列类型
我在 Postgres 10.10 中有下表:
Table "public.client"
Column | Type | Collation | Nullable | Default
---------------------+---------+-----------+----------+------------------------------------------
clientid | integer | | not null | nextval('client_clientid_seq'::regclass)
account_name | text | | not null |
last_name | text | | |
first_name | text | | |
address | text | | not null |
suburbid | integer | | |
cityid | integer | | |
post_code | integer | | not null |
business_phone | text | | |
home_phone …Run Code Online (Sandbox Code Playgroud) PostgreSQL中存在smallserial,serial和bigserial数值数据类型,它们分别对 32767、2147483647 和 9223372036854775807 有明显的限制。
但是呢GENERATED { ALWAYS | BY DEFAULT } AS IDENTITY,它有什么限制吗?或者也许它们是根据提供的数据类型(SMALLINT, INT, BIGINT)计算的?