Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLITE Custom DataTypes?

Tags:

types

sql

sqlite

I am fairly new to SQLITE and I noticed that there are only a 4 datatypes but I see examples online where people are putting in their own datatypes. I don't really understand this and was wondering if someone could explain that to me. For example I see a column that will hold a date and the datatype that was given was timestamp which does not exist. What does it default to? Does it default to a text when you make your own?

like image 461
Pittfall Avatar asked Dec 07 '11 14:12

Pittfall


People also ask

What are the 4 main data types in sqlite3?

SQLite only has four primitive data types: INTEGER, REAL, TEXT, and BLOB.

Does SQLite support a BLOB type?

(10) Does SQLite support a BLOB type? SQLite allows you to store BLOB data in any column, even columns that are declared to hold some other type. BLOBs can even be used as PRIMARY KEYs.

Can SQLite store JSON?

SQLite stores JSON as ordinary text. Backwards compatibility constraints mean that SQLite is only able to store values that are NULL, integers, floating-point numbers, text, and BLOBs. It is not possible to add a sixth "JSON" type. SQLite does not (currently) support a binary encoding of JSON.

Is SQLite slow?

The SQLite docs explains why this is so slow: Transaction speed is limited by disk drive speed because (by default) SQLite actually waits until the data really is safely stored on the disk surface before the transaction is complete.


1 Answers

sqlite3 uses a dynamic type system. There are only five storage classes: NULL, integer, real, text and blob. (Source: Datatypes In SQLite Version 3.)

And, to quote that page:

Any column in an SQLite version 3 database, except an INTEGER PRIMARY KEY column, may be used to store a value of any storage class.

Apart from the integer primary key exception, SQLite doesn't enforce types at all. Which means that the type name you put in your create table is purely informative.

create table mytab (a apples, b bananas);

is a valid create table statement. You can insert timestamps, text, blobs into both columns (not saying that you should, but you can).

Look at the linked reference documentation for the type system for more information.

sqlite> create table mytab (a apples, b bananas);
sqlite> insert into mytab values (CURRENT_TIME, NULL);
sqlite> insert into mytab values ('hello', 3.14159);
sqlite> select * from mytab;
14:59:18|
hello|3.14159

To answer your question directly: there is no default. A storage type is associated with each value stored in the database, not to columns of a table.

like image 122
Mat Avatar answered Sep 23 '22 02:09

Mat