Is it bad to have text as a primary key in an SQLite database? I heard that it's bad for performance reasons, is this true? And will the rowid be used as the actual primary key in such a case?
Technically yes, but if a string makes sense to be the primary key then you should probably use it. This all depends on the size of the table you're making it for and the length of the string that is going to be the primary key (longer strings == harder to compare).
The PRIMARY KEY is optional for ordinary tables but is required for WITHOUT ROWID tables. If a table has a single column primary key and the declared type of that column is "INTEGER" and the table is not a WITHOUT ROWID table, then the column is known as an INTEGER PRIMARY KEY.
Primary keys must contain unique values. A primary key column cannot have NULL values. A table can have only one primary key, which may consist of single or multiple fields.
An SQLite database is limited in size to 281 terabytes (248 bytes, 256 tibibytes). And even if it could handle larger databases, SQLite stores the entire database in a single disk file and many filesystems limit the maximum size of files to something less than this.
Is it bad to have text as a primary key in an SQLite database? I heard that it's bad for performance reasons, is this true?
From correctness point of view, TEXT PRIMARY KEY
is all right.
From performance point of view, prefer INTEGER
keys. But as with any performance issue, measure it yourself to see if there's a significant difference with your data and use cases.
And will the rowid be used as the actual primary key in such a case?
Only INTEGER PRIMARY KEY
gets aliased with ROWID
. Other kinds of primary keys don't, and there will be the implicit integer rowid unless WITHOUT ROWID
is specified. Reference.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With