Q
Problem solve Get help with specific problems with your technologies, process and projects.

Retrieving the last row inserted into a table

How can I retrieve the last row inserted into a table? I don't want to have to specify the row number.
In relational database systems, such as Oracle, there is no concept of first row, last row, or even the Nth row. That is because the order of the rows of data is not guaranteed. This is a fundamental tenet of relational database theory.

There are ways around this. If you want to know the last row entered into a table, then you will need to have a column that contains a timestamp of the row when it was inserted, or column with a sequential number stored in it. You can add the column to your table and then code a trigger to populate the column so that you do not have to change the application. Once you have that column populated, you can select the last row where that column contains the max value, similar to the following:

SELECT * FROM my_table t
WHERE t.colX = (SELECT MAX(colX) FROM my_table);

This was last published in February 2005

Dig Deeper on Oracle database design and architecture

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.

-ADS BY GOOGLE

SearchDataManagement

SearchBusinessAnalytics

SearchSAP

SearchSQLServer

TheServerSide.com

SearchDataCenter

SearchContentManagement

SearchHRSoftware

Close