SQLite has a feature that helps enforce rigid typing, preventing mistakes like putting text into integer columns. This feature is called strict tables [1]. To create a strict table, simply add STRICT to the end of the CREATE TABLE statement. For example: CREATE TABLE people (name TEXT) STRICT; [2].
Strict tables help prevent type mismatches on insert and update operations. For instance, SQLite normally allows inserting text into an INTEGER column, but strict tables prevent this [3]. Additionally, strict tables require a column type, so you can't create a table without specifying the data type for each column [4]. If you need a column to be flexible, you can use the ANY data type, which allows any type of data [5].
However, strict tables are not without their drawbacks. For one, you can't alter an existing table to make it strict [6]. You'll need to create a new strict table and migrate the data from the old table to the new one. Moreover, strict tables are only available in SQLite version 3.37.0 and later [7].
Despite these limitations, the benefits of strict tables outweigh the cons. They help enforce good data integrity and prevent subtle bugs that can arise from datatype mismatches [8]. In conclusion, strict tables are a valuable feature in SQLite that can help improve data integrity and prevent common mistakes. By using strict tables, developers can ensure that their data is consistent and accurate, which is especially important in applications where data reliability is crucial.
Sources
- https://evanhahn.com/prefer-strict-tables-in-sqlite/
- https://sqlite.org/stricttables.html
- https://news.ycombinator.com/item?id=48873940
- https://daily.dev/posts/prefer-strict-tables-in-sqlite-rbj9vkd7s
- https://mastodon.social/@nixCraft/116903020807675963
- https://sqlite-utils.datasette.io/en/stable/changelog.html#v4.1
- https://sqlite.org/releaselog/3_37_0.html
- https://evanhahn.com/the-two-kinds-of-error/


