简体   繁体   中英

First inserted record in a oracle table?

How do we know the first inserted record in table, let consider we have loaded 1 million records into a table and we don't have any time stamp columns here. How to find it?

If you have no column that indicate order of insert like timestamp, serial fields (ex: primary key) you have no method to find what was the first inserted row. SQL standard says clearly that data sets are not ordered (there is no "natural" order). All hacks like rownum, row id etc. are not warranted to work after reindexing database, reload database and similar operations.

Not 100% accurate (as piotrpo pointed out), but if you are ok with accuracy of the value provided by ORA_ROWSCN ( docs ), you can find out which row was inserted first (surely, if it wasn't updated later) . To be correct, not even row, but the block where row resides - you can use DBMS_ROWID functions to drill down to row level (again, not 100% precise) :

SELECT  DBMS_ROWID.ROWID_ROW_NUMBER(ROWID),
SCN_TO_TIMESTAMP(ORA_ROWSCN) 
FROM table1 ORDER BY 2 DESC, 1 ASC

If you had identifying column with ids you anyway couldn't know which row was first, because you don't know if someone manual added row with id lower then rest, one solution is to add to table field with timestamp/date format where you insert time for each row inserted or modified. but...

SELECT SCN_TO_TIMESTAMP(ora_rowscn),ora_rowscn FROM your_table

you can find when your row was modified/added... maybe there is some solution but not with rowid. with this you can find only first/last updated row...

Just use rowid like

Select * from my_table where rowid in ( Select min(rowid) from my_table )

it should work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM