SQLite 数据库在 2021 年 11 月发布的 3.37.0 版本中引入了新的"STRICT"表功能,旨在通过强制严格的类型检查来防止插入错误 [1]。该特性要求用户在创建新表时添加 STRICT 关键字,以增强数据完整性并减少潜在的软件缺陷 [1]。
启用 STRICT 模式后,数据库将禁止执行类型不匹配的操作,例如试图向整数列中插入文本内容将被拒绝 [1]。此外,在定义严格表时,无效的数据类型(如 GARBAGE、DATETIME)也会被直接拒收 [1]。尽管 SQLite 官方文档一直支持灵活的类型系统,但文章作者认为采用严格的类型检查能更有效地降低 bug 发生率 [1]。
对于现有的非 STRICT 表,无法通过简单的配置修改将其转换为严格模式;若需应用此功能,必须重建数据库并迁移数据 [1]。
SQLite version 3.37.0, released in November 2021, introduced a new feature allowing users to create "STRICT" tables [1]. This functionality enforces strict type checking during data insertion operations to prevent errors such as inserting text values into integer columns [1]. Additionally, the STRICT keyword rejects invalid or unsupported datatype definitions at table creation time, specifically citing examples like GARBAGE and DATETIME which are not permitted in this mode [1].
While SQLite's official documentation supports flexible typing by default, an author on Hacker News argues that adopting strict types can significantly reduce bugs associated with type mismatches [1]. However, the transition to STRICT tables is not seamless for existing databases; non-strict tables cannot be directly modified into strict tables and must instead undergo a rebuild process involving data migration [1]. The article further analyzes these trade-offs and provides guidance on necessary precautions when migrating from flexible typing schemes to the new strict enforcement model [1].