Sling Academy
Home/SQLite/Type Affinities in SQLite: How They Work with Columns

Type Affinities in SQLite: How They Work with Columns

Last updated: December 07, 2024

SQLite is a widely used database engine due to its portability and simplicity. One aspect that often confuses developers—particularly those accustomed to more traditional databases—is SQLite's handling of data types. Unlike other databases, SQLite has a dynamic type system, which means it uses a concept called type affinities instead of strictly enforcing a data type on a column. In this article, we'll explore what type affinities are, how they work, and provide some examples to demonstrate their application.

Understanding Type Affinities

SQLite uses five different type affinities to determine the type of data that can be associated with a column: TEXT, NUMERIC, INTEGER, REAL, and BLOB. Each column in an SQLite database has one of these type affinities, and it influences how SQLite attempts to store and process values in that column. Here’s a brief overview of each:

  • TEXT: This type lets the column store any data as a text string. If a value inserted doesn’t match another column type affinity, it defaults to TEXT.
  • NUMERIC: This type allows storage of any number, depending upon the context, converting it either to INTEGER or REAL.
  • INTEGER: A specialization of the NUMERIC affinity where values are attempted to be converted to an integer if possible.
  • REAL: This is another specialization of NUMERIC used to store floating-point values.
  • BLOB: Represents a binary object and causes no conversions. The data is entered and stored exactly as supplied.

How Type Affinities Are Assigned

When you create a table in SQLite, the type affinity is deduced from the declared type of a column. SQLite looks at the declared type of a column (for instance, "VARCHAR(255)") and assigns an affinity using a few simple rules:

  1. If the declared type contains the string "INT," then it is assigned INTEGER affinity.
  2. If the declared type contains the string "CHAR," "CLOB," or "TEXT," then it is assigned TEXT affinity.
  3. If the declared type contains the string "BLOB," or if no type is specified, it is assigned BLOB affinity.
  4. If the declared type contains any of "REAL," "FLOA," or "DOUB," then it is assigned REAL affinity.
  5. Otherwise, the affinity is NUMERIC.

Example: Checking Type Affinities in SQLite

Let's walk through a simple example to see how SQLite infers type affinities for a column based on its declared type:

CREATE TABLE demo (
    myInt INTEGER,
    myText VARCHAR(50),
    myReal REAL,
    myData BLOB
);

In the above table:

  • myInt has INTEGER affinity, as expected.
  • myText - despite the style of declaration (VARCHAR) common in other SQL dialects, this column has TEXT affinity.
  • myReal straightforwardly maps to REAL affinity.
  • myData is assigned the BLOB affinity.

Now, let's look at how the same SQLite handles these affinities when inserting data:

INSERT INTO demo (myInt, myText, myReal, myData) VALUES (42, 'Hello, world!', 3.14, x'123456');

For each inserte:

  • The integer value 42 is correctly stored as an integer due to the INTEGER affinity.
  • The string 'Hello, world!' undergoes no change, adhering to its TEXT affinity.
  • The float 3.14 gets stored naturally, thanks to REAL affinity.
  • The hex data x'123456' goes unaltered into the BLOB column, also compatible with its affinity.

Conclusion

SQLite's flexible typing system allows for efficient data management without the rigidity of traditional typing systems. Although it requires some adjustments for developers used to conventional SQL statements, utilizing type affinities can be incredibly powerful for lightweight applications. Understanding and leveraging these can significantly enhance your database practices. It is crucial to be mindful of affinities when designing your database to ensure data is stored and retrieved optimally. Thus, a firm grasp of type affinities not only helps in avoiding type errors but also boosts the robustness of applications that rely on SQLite databases.

Next Article: Designing Constraints in SQLite for Reliable Databases

Previous Article: SQLite Data Storage Classes: A Comprehensive Guide

Series: SQLite Data Types and Constraints

SQLite

You May Also Like

  • How to use regular expressions (regex) in SQLite
  • SQLite UPSERT tutorial (insert if not exist, update if exist)
  • What is the max size allowed for an SQLite database?
  • SQLite Error: Invalid Value for PRAGMA Configuration
  • SQLite Error: Failed to Load Extension Module
  • SQLite Error: Data Type Mismatch in INSERT Statement
  • SQLite Warning: Query Execution Took Longer Than Expected
  • SQLite Error: Cannot Execute VACUUM on Corrupted Database
  • SQLite Error: Missing Required Index for Query Execution
  • SQLite Error: FTS5 Extension Malfunction Detected
  • SQLite Error: R-Tree Node Size Exceeds Limit
  • SQLite Error: Session Extension: Invalid Changeset Detected
  • SQLite Error: Invalid Use of EXPLAIN Statement
  • SQLite Warning: Database Connection Not Closed Properly
  • SQLite Error: Cannot Attach a Database in Encrypted Mode
  • SQLite Error: Insufficient Privileges for Operation
  • SQLite Error: Cannot Bind Value to Parameter
  • SQLite Error: Maximum String or Blob Size Exceeded
  • SQLite Error: Circular Reference in Foreign Key Constraints