且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

pg_dump串行数据类型问题

更新时间:2023-02-02 20:17:35

来自文档:

数据类型smallserialserialbigserial不是真实类型,而仅仅是创建唯一标识符列的方便符号(类似于某些其他数据库支持的AUTO_INCREMENT属性).在当前的实现中,指定:

The data types smallserial, serial and bigserial are not true types, but merely a notational convenience for creating unique identifier columns (similar to the AUTO_INCREMENT property supported by some other databases). In the current implementation, specifying:

CREATE TABLE tablename (
    colname SERIAL
);

等同于指定:

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;

因此,我们创建了一个整数列,并安排其默认值从序列生成器中分配.应用NOT NULL约束以确保不能插入null值. (在大多数情况下,您还希望附加一个UNIQUE或PRIMARY KEY约束,以防止意外插入重复的值,但这不是自动的.)最后,该序列被标记为列拥有者",因此该列如果删除了列或表,则将删除它们.

Thus, we have created an integer column and arranged for its default values to be assigned from a sequence generator. A NOT NULL constraint is applied to ensure that a null value cannot be inserted. (In most cases you would also want to attach a UNIQUE or PRIMARY KEY constraint to prevent duplicate values from being inserted by accident, but this is not automatic.) Lastly, the sequence is marked as "owned by" the column, so that it will be dropped if the column or table is dropped.