Explore key concepts in SQLite data types and type affinity with practical questions designed to enhance your understanding of column storage, type conversions, and common behaviors. Ideal for beginners seeking clarity on SQLite’s flexible data typing system and its practical implications.
When you define a column as INTEGER in an SQLite CREATE TABLE statement, which storage class does SQLite actually use for the data in that column?
Explanation: SQLite uses one of five storage classes, and columns declared as INTEGER use the INTEGER storage class. The other options, INTNUM, INT64, and NUMBER, are not valid SQLite storage classes and are commonly confused due to similarity to other database systems. Using the correct storage class is important for predictable behavior.
What is the type affinity of a column defined as VARCHAR(50) in SQLite?
Explanation: SQLite assigns TEXT affinity to columns declared with VARCHAR and similar types like CHAR. The BLOB affinity is for no specified type, NUMERIC affinity applies to columns with certain keywords (like NUMERIC), and INTEGER affinity is for declarations containing INT. TEXT affinity helps store any kind of text efficiently in SQLite.
If you insert the string '123' into an INTEGER-affinity column, how does SQLite store the value?
Explanation: SQLite tries to convert values inserted into a column based on its type affinity. Since '123' is numeric in content, it will be converted to the integer 123 in an INTEGER-affinity column. If conversion were not possible, it would store the text; however, in this case, string '123' converts cleanly. BLOB and NULL are not chosen unless the value cannot convert or is explicitly null.
What happens if you declare a column in SQLite without any type or with the exact type name 'BLOB'?
Explanation: In SQLite, columns with no type specified, or those declared exactly as BLOB, are given BLOB affinity. NUMERIC and TEXT affinities are reserved for certain other type declarations, and REAL affinity is for specific real number declarations. BLOB affinity means the value is stored with minimal conversion.
If you insert the floating-point value 5.73 into a column with REAL affinity, how is it typically stored in SQLite?
Explanation: SQLite uses 8-byte floating point numbers (known as 'double precision') for REAL storage class. A 4-byte integer would be for INTEGER storage. Strings are not the default for numeric storage, and BLOB is only used if the affinity or type is BLOB. The REAL affinity preserves the precision of floating-point values.
How strictly does SQLite enforce data types for inserted values compared to some other databases?
Explanation: SQLite uses dynamic typing with type affinity, meaning it attempts to convert data types as needed and only rejects if conversion is impossible. Unlike stricter systems, it does not reject mismatched types outright. Data is not always stored as strings or in text, and numbers are stored using their respective formats if compatible.
Which keyword in a SQLite column type declaration will NOT assign INTEGER affinity to the column?
Explanation: Keywords like INT, INTEGER, and TINYINT assign INTEGER affinity. FLOAT, however, triggers REAL affinity. This is a common point of confusion because FLOAT sounds numeric, but it is not treated as INTEGER affinity in SQLite.
What happens if you insert NULL into any column in SQLite, regardless of its declared type?
Explanation: SQLite stores NULL values in any column as actual NULLs, regardless of the column's affinity or storage class. NULL does not convert to an empty string or zero, whether as integer or blob. This behavior ensures the consistent representation of missing data.
If you define a column as BOOLEAN in SQLite, which type affinity does it get?
Explanation: SQLite does not have a separate BOOLEAN affinity. Columns declared as BOOLEAN are interpreted with NUMERIC affinity, allowing for values like 0 and 1 or true and false. BOOLEAN affinity and NO affinity are not valid, and INTEGER affinity is only assigned with certain integer keywords.
How does SQLite typically store date and time values if you define a column as DATETIME?
Explanation: SQLite can store dates and times as TEXT (ISO8601), REAL (Julian day number), or INTEGER (Unix timestamp), and does not enforce a single storage class for DATETIME. Restricting DATETIME to only TEXT or BLOB would not allow this flexibility. The storage adapts to the inserted data format.