Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLITE equivalent for Oracle's ROWNUM?

I'm adding an 'index' column to a table in SQLite3 to allow the users to easily reorder the data, by renaming the old database and creating a new one in its place with the extra columns.

The problem I have is that I need to give each row a unique number in the 'index' column when I INSERT...SELECT the old values.

A search I did turned up a useful term in Oracle called ROWNUM, but SQLite3 doesn't have that. Is there something equivalent in SQLite?

like image 935
Raceimaztion Avatar asked Sep 02 '12 02:09

Raceimaztion


People also ask

Does SQLite support ROW_NUMBER?

Using SQLite ROW_NUMBER() for paginationThe ROW_NUMBER() function can be useful for pagination. For example, if you want to display customers information on a table by pages with 10 rows per page. In this example: First, the ROW_NUMBER() function assigns each row a sequential integer.

Can we use Rownum in Oracle?

You can use ROWNUM to limit the number of rows returned by a query, as in this example: SELECT * FROM employees WHERE ROWNUM < 10; If an ORDER BY clause follows ROWNUM in the same query, then the rows will be reordered by the ORDER BY clause. The results can vary depending on the way the rows are accessed.

What datatype is Rownum in Oracle?

In Oracle, the ROWNUM is assigned to each row of a set as the records are returned. It is an integer data type that starts at 1, with monotonically increasing numbers. This pseudo column applies to the entire row, regardless of the origin of the row.

What is Rowid in SQLite?

A rowid value is a 64 bit integer. The rowid value can be queried with the rowid keyword. Synonyms for rowid are: oid and _rowid . If a table has a column that is defined to be an integer primary key , this column stores the rowid, that is, the respective column names is an alias for rowid (or vice versa).


2 Answers

Many people here seems to mix up ROWNUM with ROWID. They are not the same concept and Oracle has both.

ROWID is a unique ID of a database ROW. It's almost invariant (changed during import/export but it is the same across different SQL queries).

ROWNUM is a calculated field corresponding to the row number in the query result. It's always 1 for the first row, 2 for the second, and so on. It is absolutely not linked to any table row and the same table row could have very different rownums depending of how it is queried.

Sqlite has a ROWID but no ROWNUM. The only equivalent I found is ROW_NUMBER() function (see http://www.sqlitetutorial.net/sqlite-window-functions/sqlite-row_number/).

You can achieve what you want with a query like this:

insert into new 
    select *, row_number() over () 
    from old;
like image 187
Pyrollo Avatar answered Oct 18 '22 22:10

Pyrollo


You can use one of the special row names ROWID, OID or _ROWID_ to get the rowid of a column. See http://www.sqlite.org/lang_createtable.html#rowid for further details (and that the rows can be hidden by normal columns called ROWID and so on).

like image 14
Johannes Kuhn Avatar answered Oct 18 '22 22:10

Johannes Kuhn