How can derived seconds stay consistent with stored milliseconds?
A generated column derives from other values in the row. A virtual generated column is evaluated on read, so callers do not maintain a second copy.
SQLite example and interpretation
Updating the raw duration from 1500 to 2500 milliseconds makes the derived reading 2.5 seconds. The formula must meet generated-column expression restrictions.
CREATE TABLE run(ms INTEGER,sec REAL AS(ms/1000.0) VIRTUAL);
INSERT INTO run(ms) VALUES(1500);
UPDATE run SET ms=2500;
SELECT ms,sec FROM run;Result [[2500, 2.5]]
中文
怎样让毫秒到秒的换算始终与原始读数一致?
生成列由同一行的其他值计算。虚拟生成列在读取时计算,无须调用方同步写入两份值。
SQLite 示例与适用条件
原始耗时从 1500 改为 2500 毫秒,读取秒数自动变为 2.5。公式须遵守生成列表达式限制。
CREATE TABLE run(ms INTEGER,sec REAL AS(ms/1000.0) VIRTUAL);
INSERT INTO run(ms) VALUES(1500);
UPDATE run SET ms=2500;
SELECT ms,sec FROM run;结果 [[2500, 2.5]]