Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String storage size in sqlite

Tags:

sqlite

After using lots of SQL Server and SQL Server Compact, I have recently started to use Sqlite. I'm currently creating tables in Sqlite and have noticed that there doesn't seem to be any need to input the string length limit when defining string columns. In SQL Server a 255-character string datatype is defined like: varchar(255). In SQL Server Compact it is defined like: nvarchar(255).

However, in Sqlite, it appears that the column datatype can be defined simply as text, without inputting any size limit. Is there a way to define a size limit in sqlite? Or is this not even necessary?

like image 414
Mike Baxter Avatar asked Jul 22 '13 10:07

Mike Baxter


People also ask

How much data we can store in SQLite?

SQLite database files have a maximum size of about 140 TB. On a phone, the size of the storage (a few GB) will limit your database file size, while the memory size will limit how much data you can retrieve from a query. Furthermore, Android cursors have a limit of 1 MB for the results.

Which storage class is SQLite?

SQLite does not have a storage class set aside for storing dates and/or times. Instead, the built-in Date And Time Functions of SQLite are capable of storing dates and times as TEXT, REAL, or INTEGER values: TEXT as ISO8601 strings ("YYYY-MM-DD HH:MM:SS. SSS").

How big is SQLite INTEGER?

The INTEGER values in SQLite are stored in either 1, 2, 3, 4, 6, or 8 bytes of storage depending on the value of the number.

How big is too big for SQLite?

An unlikely requirement for an engine popular on Android and iOS. SQLite, which claims to be "used more than all other database engines combined", has been updated to version 3.33. 0 with the maximum size increased to 281TB, around twice the previous capacity of 140TB.


2 Answers

SQLite does let you write VARCHAR(255) or NVARCHAR(255), but the only relevant part of that is CHAR (which gives the column "text affinity"). The number in parentheses is allowed for compatibility with standard SQL, but is ignored.

In SQLite, there's little need for length constraints on strings, because (1) it doesn't affect the amount of space the string takes up on disk and (2) there's no arbitrary limit (except for SQLITE_MAX_LENGTH) on the length of strings that can be used in an INDEX.

If you have an actual need to place a limit on the number of characters in a column, just add a constraint like CHECK(LENGTH(TheColumn) <= 255).

like image 59
dan04 Avatar answered Oct 16 '22 20:10

dan04


There is a managable string or blob limit in SQLite. The default is 10^9 bytes. See documentation BTW you don't need to specify it on column declaration anyway.

like image 23
gleb.kudr Avatar answered Oct 16 '22 21:10

gleb.kudr