在 Hacker News Daily 上看到「STRICT Tables」這篇,在講 SQLite 目前在規劃 strict table,對應的討論可以參考「Strict Tables – Column type constraints in SQLite - Draft (sqlite.org)」這邊。
我在 draft 文件開頭看到這個驚人的事實:轉型失敗的時候會直接寫進去,不是錯誤或是 0
或是 NULL
之類的值 XDDD
For example, if a table column has a type of "INTEGER", then SQLite tries to convert anything inserted into that column into an integer. So an attempt to insert the string '123' results in an integer 123 being inserted. But if the content cannot be losslessly converted into an integer, for example if the input is 'xyz', then the original string is inserted instead. See the Datatypes In SQLite document for additional information.
實際上測試建了一個表格測試:
SQLite version 3.31.1 2020-01-27 19:55:54 Enter ".help" for usage hints. sqlite> CREATE TABLE a (id INTEGER PRIMARY KEY NOT NULL, col1 INTEGER); sqlite> INSERT INTO a (id, col1) VALUES (1, 'a'); sqlite> SELECT * FROM a; id col1 ---------- ---------- 1 a
我果然跟 SQLite 不熟...