gpt4 book ai didi

postgresql - 创建连载实际上创建了一个大连载

转载 作者:行者123 更新时间:2023-11-29 12:15:34 25 4
gpt4 key购买 nike

当我创建一个序列类型的测试表时,连续剧其实是大连载类型。这会导致任何问题吗?这是一个错误吗?PostgreSQL 9.5.3红帽 64 位

#> CREATE TABLE test (id SERIAL PRIMARY KEY, name text);
#> \d+ test
Table "public.test"
Column | Type | Modifiers | Storage | Stats target | Description
--------+---------+---------------------------------------------------+----------+--------------+-------------
id | integer | not null default nextval('test_id_seq'::regclass) | plain | |
name | text | | extended | |
Indexes:
"test_pkey" PRIMARY KEY, btree (id)

#> \d+ test_id_seq
Sequence "public.test_id_seq"
Column | Type | Value | Storage
---------------+---------+---------------------+---------
sequence_name | name | test_id_seq | plain
last_value | bigint | 1 | plain
start_value | bigint | 1 | plain
increment_by | bigint | 1 | plain
max_value | bigint | 9223372036854775807 | plain
min_value | bigint | 1 | plain
cache_value | bigint | 1 | plain
log_cnt | bigint | 0 | plain
is_cycled | boolean | f | plain
is_called | boolean | f | plain

最佳答案

SERIAL 并不是真正的类型;如 9.5 docs 中所述,它只是一组命令的简写:

The data types smallserial, serial and bigserial are not true types, but merely a notational convenience [...] In the current implementation, specifying:

CREATE TABLE tablename (
colname SERIAL
);

is equivalent to specifying:

CREATE SEQUENCE tablename_colname_seq;
CREATE TABLE tablename (
colname integer NOT NULL DEFAULT nextval('tablename_colname_seq')
);
ALTER SEQUENCE tablename_colname_seq OWNED BY tablename.colname;

如您所见,integer 类型只适用于表列,在序列的定义中并没有提到。 CREATE SEQUENCE 9.5 中的语句不允许您指定类型;所有序列都基于 bigint 计数器,因此 \d+ 输出中的 bigint 列。

从 Postgres 10 开始,情况不再如此:您现在可以将数据类型附加到序列,并且根据 new docs ,上面的 SERIAL 示例中的序列现在定义为:

CREATE SEQUENCE tablename_colname_seq AS integer;

这实际上只用于限制序列的最大值 - 内部计数器仍然是一个 bigint - 但它被 \d+ 报告为适当的类型(现在看起来完全不同了):

test=# \d+ tablename_colname_seq
Sequence "public.tablename_colname_seq"
Type | Start | Minimum | Maximum | Increment | Cycles? | Cache
---------+-------+---------+------------+-----------+---------+-------
integer | 1 | 1 | 2147483647 | 1 | no | 1

关于postgresql - 创建连载实际上创建了一个大连载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56401946/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com