postgresql postgres 9.2 中的 ROWID 等价物
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14626481/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ROWID equivalent in postgres 9.2
提问by jobi88
Is there any way to get rowid of a record in postgres??
有没有办法在postgres中获得记录的rowid?
In oracle i can use like
在 oracle 我可以使用像
SELECT MAX(BILLS.ROWID) FROM BILLS
回答by baklarz2048
Yes, there is ctid column which is equivalent for rowid. But is useless for you. Rowid and ctid are physical row/tuple identifiers => can change after rebuild/vacuum.
是的,有与 rowid 等效的 ctid 列。但是对你没用。Rowid 和 ctid 是物理行/元组标识符 => 可以在重建/真空后更改。
回答by Oliver Crow
The PostgreSQL row_number() window function can be used for most purposes where you would use rowid. Whereas in Oracle the rowid is an intrinsic numbering of the result data rows, in Postgres row_number() computes a numbering within a logical ordering of the returned data. Normally if you want to number the rows, it means you expect them in a particular order, so you would specify which column(s) to order the rows when numbering them:
PostgreSQL row_number() 窗口函数可用于大多数使用 rowid 的目的。在 Oracle 中 rowid 是结果数据行的固有编号,而在 Postgres row_number() 中计算返回数据的逻辑顺序内的编号。通常,如果您想对行进行编号,这意味着您希望它们按特定顺序排列,因此您可以在编号时指定哪些列对行进行排序:
select client_name, row_number() over (order by date) from bills;
If you just want the rows numbered arbitrarily you can leave the over clause empty:
如果您只想对行进行任意编号,您可以将 over 子句留空:
select client_name, row_number() over () from bills;
If you want to calculate an aggregate over the row number you'll have to use a subquery:
如果要计算行号的聚合,则必须使用子查询:
select max(rownum) from (
select row_number() over () as rownum from bills
) r;
If all you need is the last item from a table, and you have a column to sort sequentially, there's a simpler approach than using row_number(). Just reverse the sort order and select the first item:
如果您只需要表中的最后一项,并且有一列要按顺序排序,则有一种比使用 row_number() 更简单的方法。只需颠倒排序顺序并选择第一项:
select * from bills
order by date desc limit 1;
回答by JohnMudd
Use a Sequence. You can choose 4 or 8 byte values.
使用序列。您可以选择 4 或 8 字节值。
回答by Zaur Hajili
Add any unique column to your table(name maybe rowid). And prevent changing it by creating BEFORE UPDATE trigger, which will raise exception if someone will try to update. You may populate this column with sequence as @JohnMudd mentioned.
将任何唯一的列添加到您的表中(名称可能是 rowid)。并通过创建 BEFORE UPDATE 触发器来防止更改它,如果有人尝试更新,这将引发异常。您可以使用@JohnMudd 提到的序列填充此列。