SQLite 中的 NUMERIC 和 INTEGER 类型是否完全相同?

Is NUMERIC and INTEGER types are exaxtly same in SQLite?

SQLite 数据库中的 Numeric 和 Integer 类型是否完全相同?

我对这些类型有些困惑。如果有什么区别,那么区别是什么?

NUMERIC 不是一种类型(也称为存储 class),它是一种 亲和性 INTEGER 两者都是。

来自 the documentation(强调已添加):

A column with NUMERIC affinity may contain values using all five storage classes. When text data is inserted into a NUMERIC column, the storage class of the text is converted to INTEGER or REAL (in order of preference) if the text is a well-formed integer or real literal, respectively. If the TEXT value is a well-formed integer literal that is too large to fit in a 64-bit signed integer, it is converted to REAL. For conversions between TEXT and REAL storage classes, only the first 15 significant decimal digits of the number are preserved. If the TEXT value is not a well-formed integer or real literal, then the value is stored as TEXT. For the purposes of this paragraph, hexadecimal integer literals are not considered well-formed and are stored as TEXT. (This is done for historical compatibility with versions of SQLite prior to version 3.8.6 2014-08-15 where hexadecimal integer literals were first introduced into SQLite.) No attempt is made to convert NULL or BLOB values.

A string might look like a floating-point literal with a decimal point and/or exponent notation but as long as the value can be expressed as an integer, the NUMERIC affinity will convert it into an integer. Hence, the string '3.0e+5' is stored in a column with NUMERIC affinity as the integer 300000, not as the floating point value 300000.0.

A column that uses INTEGER affinity behaves the same as a column with NUMERIC affinity. The difference between INTEGER and NUMERIC affinity is only evident in a CAST expression.

(阅读并理解整个页面及其描述的概念,如亲和力,对于学习 sqlite 的工作原理至关重要。)

CAST() 的规则过于复杂,无法完整引用,但可以阅读 here。总而言之,转换为 INTEGER 将始终 return 一个整数,转换为 NUMERIC 可以 return 一个整数或一个实数值,具体取决于。