Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to enforce SQLite datatypes?

Tags:

sqlite

From my understanding, sqlite's datatype isn't associated to its column but to the data themselves: this practically means that you can insert any data to any column.

Is it possible to prohibit such a behaviour? I mean, I want sqlite to raise an error (or at least a warning) when I accidentally try to insert text to integer, for example.

like image 684
akai Avatar asked Mar 12 '18 07:03

akai


1 Answers

You can use CHECK constraints and typeof() to check the actual data type:

CREATE TABLE MyTable (
    Col1 INTEGER  CHECK (typeof(Col1) = 'integer'),
    Col2 TEXT     CHECK (typeof(Col2) IN ('text', 'null')
);
like image 75
CL. Avatar answered Dec 16 '22 14:12

CL.