Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use SQLite 3 type affinity?

Tags:

sqlite

According to SQLite 3 documentation says it has "type affinity" feature to enforce column data types. But how to turn it on?

like image 995
Ivan Avatar asked Apr 08 '12 00:04

Ivan


People also ask

What are type affinities in SQLite?

SQLite supports the concept of type affinity on columns. Any column can still store any type of data but the preferred storage class for a column is called its affinity. Each table column in an SQLite3 database is assigned one of the following type affinities − This column stores all data using storage classes NULL, TEXT or BLOB.

What is type affinity in SQL Server?

Type affinity does not force column data types - it simply makes a suggestion as to how the underlying storage engine stores the data. The type affinity of a column is the recommended type for data stored in that column.

What is a data type in SQLite?

In SQLite, table can be created without specifying a data type for their columns. And even, if tables are created with a data type, it is still possible to store values of different data types in the same column. The data type in which a value of a given data type is stored in a table is determined by a column's type affinity.

What is type affinity and why does it matter?

Type affinity does not force column data types - it simply makes a suggestion as to how the underlying storage engine stores the data. The type affinity of a column is the recommended type for data stored in that column. The important idea here is that the type is recommended, not required. Any column can still store any type of data.


2 Answers

Type affinity does not force column data types - it simply makes a suggestion as to how the underlying storage engine stores the data.

Under the "type affinity" section:

The type affinity of a column is the recommended type for data stored in that column. The important idea here is that the type is recommended, not required. Any column can still store any type of data.

(emphasis added)

As far as "how to turn it on," there's no such thing. This is how SQLite works all the time. There's nothing to turn on or off in order to get this functionality.

like image 148
jefflunt Avatar answered Nov 03 '22 10:11

jefflunt


The column data types are not enforced, just suggested by their declared types in the create table statement. This is what they mean by type affinity.

The type affinity of a column is the recommended type for data stored in that column. The important idea here is that the type is recommended, not required. Any column can still store any type of data. It is just that some columns, given the choice, will prefer to use one storage class over another. The preferred storage class for a column is called its "affinity".

like image 45
Jordão Avatar answered Nov 03 '22 08:11

Jordão