What is the difference between related SQLite data-types like INT, INTEGER, SMALLINT and TINYINT?

前端 未结 5 1630
被撕碎了的回忆
被撕碎了的回忆 2020-12-02 06:15

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

5条回答
  •  清歌不尽
    2020-12-02 06:24

    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.

提交回复
热议问题