Why can inserting code 0007 into a NUMERIC column remove its leading zeros?

NUMERIC affinity attempts to store well-formed numeric text as an integer or real number.

SQLite example and interpretation

The same input becomes seven in a NUMERIC column and stays four characters in TEXT. Decide whether a field is a quantity or identifier before choosing affinity.

CREATE TABLE t(n NUMERIC,s TEXT);
INSERT INTO t VALUES('0007','0007');
SELECT n,s,typeof(n),typeof(s) FROM t;

Result [[7, "0007", "integer", "text"]]

中文

为什么编号 0007 写进 NUMERIC 列后零消失了?

NUMERIC 亲和性会尝试把可解析为数值的文本存成整数或实数。

SQLite 示例与适用条件

同一原始编号写入 NUMERIC 与 TEXT 列,前者成为数字 7,后者保留四字符。设计标签列时先区分量值与标识。

CREATE TABLE t(n NUMERIC,s TEXT);
INSERT INTO t VALUES('0007','0007');
SELECT n,s,typeof(n),typeof(s) FROM t;

结果 [[7, "0007", "integer", "text"]]

References / 参考来源

Keep reading / 继续阅读