Curt Rand
Curt Rand

Reputation: 1045

SQLite: Should I use Integer primary key or Integer when storing data from API?

I've inherited an iOS project that stores record ids in a column using the data type Integer primary key. This works well, however, I'm not sure why it isn't just using regular Integer. The iOS App grabs records from an API and stores it in the local SQLite database. These records from the API each have a unique id already (e.g. id:100101394).

Is there a reason or an advantage in using Integer primary key over regular integer in this situation?

Edit: The app uses the record's unique id for the Integer primary key if that makes sense, but I don't know why I can't just use a regular integer.

Edit 1: Everyone, I understand I can use a primary key to access records. What I want to understand is if there is a reason why I should use it over a regular Integer that I can store the record's id in. Each record id the API returns is unique.

Upvotes: 1

Views: 172

Answers (2)

Scott White
Scott White

Reputation: 58

Integer Primary Key is typed, you can only put in an integer. It also will guarantee an integer is inserted when null is put in. I believe it can also act as an alias as RowID If that exists for the table.

Here is a link to the documentation on it.

https://sqlite.org/lang_createtable.html#rowid

Upvotes: 1

user10521372
user10521372

Reputation:

I believe you use a key when you expect to want to find the record by that identifier. I would expect that to impose additional overhead when storing the item, but if you want to access a record by the 'integer primary key' id, you can potentially save a lot of time and system overhead.

Upvotes: 0

Related Questions