Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What is the use of rowid in SQLite database

Shulou Source: shulou.com Published: 2022-06-01 18:39:20 12月03日 Update

This article introduces how rowid is used in SQLite database, the content is very detailed, interested friends can refer to it, I hope it can be helpful to you.

Rowid is used in SQLite database

Each table in SQLite contains a hidden column rowid by default, except for tables defined using WITHOUT ROWID. Typically, rowid can uniquely mark each record in the table. The rowid of the first record inserted in the table is 1, and the rowid of subsequent inserted records is incremented by 1. Even if the insert fails, the rowid will be incremented by one. Therefore, the rowid in the entire table is not necessarily contiguous, even if the user has not deleted the record.

Because of its uniqueness, rowid is used as a primary key in many cases. When in use, select * from tablename cannot get the rowid and must be explicitly specified. For example, select rowid, * from tablename can get the rowid column. Querying rowid is very efficient, so using rowid directly as a query condition is a good way to optimize the query.

However, the rowid column, as the primary key, is potentially dangerous in extreme cases. Because the rowidth value will always increase, if it reaches the maximum allowed value of 9223372036854775807, it will automatically search for values that are not used, reuse them, and will not prompt the user. At this point, using rowid to sort records will cause disorder and introduce other logic problems. Therefore, if there is such a possibility in the user's database, you should use AUTOINCREMENT to define the primary key to avoid this problem. Use AUTOINCREMENT to set the self-incrementing primary key, although you will encounter the problem of 9223372036854775807, but it will report an error and prompt the user to avoid the problems caused by rowid.

On the SQLite database is how the use of rowid is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Tags: Data database user question situation query content more help prompt yes maximum interest uniqueness occasion guy partner efficiency article method Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Docker Microsoft NVidia OPPO Reno