Understanding SQLite Data Types: A Complete Guide
SQLite Data Types: A Complete Overview
SQLite uses a dynamic type system for handling data types. Unlike traditional databases that enforce strict type rules, SQLite employs "type affinity," allowing for flexibility in storing and retrieving data. This article delves into SQLite's data types, their syntax, and examples, explaining how they work and the advantages they bring to database management.
SQLite Data Types Overview
SQLite does not have fixed data types for columns. Instead, it uses five basic storage classes to determine how values are stored:
1. NULL: Represents missing or undefined values.
2. INTEGER: A signed integer stored in 1, 2, 3, 4, 6, or 8 bytes.
3. REAL: A floating-point number stored as an 8-byte IEEE float.
4. TEXT: A string of text stored using UTF-8, UTF-16BE, or UTF-16LE encoding.
5. BLOB: Binary Large Object, stored exactly as it is.
SQLite columns are assigned a type affinity instead of a fixed type, providing flexibility in handling data.
Syntax for Defining Data Types in SQLite
CREATE TABLE table_name ( column_name TYPE );
- column_name: Name of the column.
- TYPE: Any declared type. SQLite applies type affinity based on the declared type.
Examples of SQLite Data Types
Defining Columns with Data Types
Code:
-- Create a table with various data types
CREATE TABLE sample_table (
id INTEGER PRIMARY KEY, -- INTEGER type with primary key
name TEXT, -- TEXT type for storing strings
price REAL, -- REAL type for floating-point numbers
image BLOB, -- BLOB type for binary data
created_at TEXT -- TEXT type for datetime values
);
Explanation:
- The id column stores integers.
- The name column stores string data.
- The price column stores floating-point numbers.
- The image column stores binary data.
- The created_at column can store datetime as a string.
Type Affinity in Action
Code:
-- Insert various types of data into the table
INSERT INTO sample_table (id, name, price, image, created_at)
VALUES (1, 'Item A', 19.99, X'FFD8FFE0', '2024-12-11 12:00:00');
-- Retrieve the data
SELECT * FROM sample_table;
Explanation:
- The X'FFD8FFE0' is a BLOB literal representing binary data.
- The created_at stores a datetime value as TEXT.
- SQLite automatically converts and handles the data based on the column’s affinity.
Understanding Type Affinity
Declared Type | Type Affinity | Example Use Case |
---|---|---|
INT or INTEGER | INTEGER | Storing whole numbers like IDs or counts. |
CHAR, TEXT | TEXT | Storing names, descriptions, or strings. |
REAL, FLOAT | REAL | Storing prices or measurements. |
NONE | BLOB | Storing binary data like images. |
Advantages of SQLite Data Types
- 1. Flexibility: Columns can store any type of data, making SQLite versatile.
- 2. Simplified Schema Design: No need for strict type declarations.
- 3. Compact Storage: Stores data efficiently based on size and type.
Common Practices with Data Types
- Datetime Storage: Use TEXT or INTEGER (UNIX timestamp) to store date and time values.
- Binary Data: Store images, files, or other binary data using BLOB type.
- Numeric Values: Prefer REAL for floating-point and INTEGER for whole numbers.
Conclusion
SQLite’s dynamic typing system provides immense flexibility while managing data. Understanding its data types and type affinity ensures better database performance and storage efficiency. Proper use of these features can lead to optimized and maintainable SQLite databases.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/sqlite/snippets/sqlite-data-types.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics