104

When creating a table in SQLite3, I get confused when confronted with all the possible datatypes which imply similar contents, so could anyone tell me the difference between the following data-types?

INT, INTEGER, SMALLINT, TINYINT

DEC, DECIMAL

LONGCHAR, LONGVARCHAR

DATETIME, SMALLDATETIME

Is there some documentation somewhere which lists the min./max. capacities of the various data-types? For example, I guess smallint holds a larger maximum value than tinyint, but a smaller value than integer, but I have no idea of what these capacities are.

0

5 Answers 5

100

SQLite, technically, has no data types, there are storage classes in a manifest typing system, and yeah, it's confusing if you're used to traditional RDBMSes. Everything, internally, is stored as text. Data types are coerced/converted into various storage locations based on affinities (ala data types assigned to columns).

The best thing that I'd recommend you do is to :

  1. Temporarily forget everything you used to know about standalone database datatypes

  2. Read the above link from the SQLite site.

  3. Take the types based off of your old schema, and see what they'd map to in SQLite

  4. Migrate all the data to the SQLite database.

Note: The datatype limitations can be cumbersome, especially if you add time durations, or dates, or things of that nature in SQL. SQLite has very few built-in functions for that sort of thing. However, SQLite does provide an easy way for you to make your own built-in functions for adding time durations and things of that nature, through the sqlite3_create_function library function. You would use that facility in place of traditional stored procedures.

5
  • 1
    Thanks for the reply and the link. Does this mean I should stick to the basic types of text, numeric, integer, real, none? Seems very limited to me, especially coming from a MSSQL, Foxpro and Oracle background. Regards. Commented May 3, 2010 at 22:18
  • 6
    I agree with you, it does seem limited at first. However, I think you'll find that SQLite is very forgiving in how you put data into the database via its affinity system. SQLite isn't standalone - it's designed to be a little database backend that you place in small applications to be accessed only via a database access API in code. Solving problems in SQLite is usually a matter of figuring out how they want you to do it.
    – J. Polfer
    Commented May 3, 2010 at 22:25
  • I'd stick to the basic types.
    – J. Polfer
    Commented May 3, 2010 at 22:31
  • 4
    @Alan: You might find it useful to declare numeric-affinity columns as DATE or BOOLEAN, but I wouldn't bother distinguishing between different sizes of integers. This is especially true for the case of INTEGER PRIMARY KEY, the one case where the exact type name matters.
    – dan04
    Commented May 4, 2010 at 0:12
  • "Everything is stored as text" seems to be wrong according to sqlite.org/fileformat.html, which says numbers are stored as compact varints / float64, and only blob and text is stored as strings
    – phiresky
    Commented May 17, 2019 at 10:12
51

The difference is syntactic sugar. Only a few substrings of the type names matter as for as the type affinity is concerned.

  • INT, INTEGER, SMALLINT, TINYINT → INTEGER affinity, because they all contain "INT".
  • LONGCHAR, LONGVARCHAR → TEXT affinity, because they contain "CHAR".
  • DEC, DECIMAL, DATETIME, SMALLDATETIME → NUMERIC, because they don't contain any of the substrings that matter.

The rules for determining affinity are listed at the SQLite site.

If you insist on strict typing, you can implement it with CHECK constraints:

CREATE TABLE T (
   N   INTEGER CHECK(TYPEOF(N) = 'integer'),
   Str TEXT CHECK(TYPEOF(Str) = 'text'),
   Dt  DATETIME CHECK(JULIANDAY(Dt) IS NOT NULL)
);

But I never bother with it.

As for the capacity of each type:

  • INTEGER is always signed 64-bit. Note that SQLite optimizes the storage of small integers behind-the-scenes, so TINYINT wouldn't be useful anyway.
  • REAL is always 64-bit (double).
  • TEXT and BLOB have a maximum size determined by a preprocessor macro, which defaults to 1,000,000,000 bytes.
1
  • 1
    Nice trick. Do note that this approach does require a value-to-be-inserted/updated to have the same storage class as the one in TYPEOF. So, attempts to insert a TEXT that would otherwise be converted to NUMERIC/INTEGER storage class by SQlite (i.e., such conversion is lossless as per sqlite.org/datatype3.html#affinity) would fail. In other words this approach is stricter than the ad-hoc approach of inserting the value and then somehow magically validating the storage class used to store that value by SQLite. For a more permissive approach, see my answer below.
    – eold
    Commented Sep 11, 2015 at 21:50
10

Most of those are there for compatibility. You really only have integer, float, text, and blob. Dates can be stored as either a number (unix time is integer, microsoft time is float) or as text.

1
  • I've used it for web based projects where ultimately everything ended up being text put into an html page anyway. I dispensed with a lot of data conversion and just used text for most of the columns. It worked out well.
    – Jay
    Commented May 4, 2010 at 15:26
4

NULL. The value is a NULL value.

INTEGER. The value is a signed integer, stored in 1, 2, 3, 4, 6, or 8 bytes depending on the magnitude of the value.

REAL. The value is a floating point value, stored as an 8-byte IEEE floating point number.

TEXT. The value is a text string, stored using the database encoding (UTF-8, UTF-16BE or UTF-16LE).

BLOB. The value is a blob of data, stored exactly as it was input.

1

As an addition to answer from dan04, if you want to blindly insert a NUMERIC other than zero represented by a TEXT but ensure that text is convertible to a numeric:

your_numeric_col NUMERIC CHECK(abs(your_numeric_col) <> 0)

Typical use case is in a query from a program that treats all data as text (for uniformity & simplicity, since SQLite already does so). The nice thing about this is that it allows constructs like this:

INSERT INTO table (..., your_numeric_column, ...) VALUES (..., some_string, ...)

which is convenient in case you're using placeholders because you don't have to handle such non-zero numeric fields specially. An example using Python's sqlite3 module would be,

conn_or_cursor.execute(
    "INSERT INTO table VALUES (" + ",".join("?" * num_values) + ")",   
    str_value_tuple)  # no need to convert some from str to int/float

In the above example, all values in str_value_tuple will be escaped and quoted as strings when passed to SQlite. However, since we're not checking explicitly the type via TYPEOF but only convertibility to type, it will still work as desired (i.e., SQLite will either store it as a numeric or fail otherwise).

Not the answer you're looking for? Browse other questions tagged or ask your own question.