行创建时间戳在 table 中是否唯一?

Is row created timestamp is unique in a table?

我知道时间戳是一个时刻的实例。

但是来到我的table,我的table没有主键。

所以我需要一个唯一的交易列。

我可以使用行创建的时间戳列具有唯一的列 table。

Table定义

Create Table order_processing_logs(
    order_number integer,
    processing_status integer,
    cust_id character varying(200),
    vendor_id character varying(200),
    .
    .
    .
    .
    .
    .,etc,
    created_time timestamp without time zone DEFAULT now(),
    updated_time timestamp without time zone DEFAULT now()
); 

我可以将 created_time 作为 table 中的唯一列吗?

现在,如果我更改 table 的体系结构,它将影响数百万条记录。

没有。通常,您无法确定没有两行完全同时出现。

如果您使用 transactions 并同时提交所有行,则情况会更加严重。 fiddle 表明两行的时间戳相等:demo: db<>fiddle

也许您可以使用两列的组合作为

PRIMARY KEY (order_number, current_timestamp)

但由于您无法确保每一列(或组合)的唯一性,因此您永远不会安全。


最好添加类型为 serial 的列,它是一个自动递增的 (bigint) 数字。

因为 Postgres 10 你可以使用 GENERATE IDENTITY 而不是 serial 类型:

id INT GENERATED ALWAYS AS IDENTITY

(http://www.postgresqltutorial.com/postgresql-identity-column/)

我不认为只使用 created_time 是明智的,因为 2 个不同的订单可能会同时放置

我更喜欢 order_number,created_time 列的组合以使其独一无二