ROWID equivalent in postgres 9.2

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-01 02:29:37
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.

See: Chapter 5. Data Definition > 5.4. System Columns

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:

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:

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:

select * from bills 
order by date desc limit 1;

Use a Sequence. You can choose 4 or 8 byte values.

http://www.neilconway.org/docs/sequences/

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!