【星海出品】SQLite的进阶操作

因为去年到今年初和一些DBA做了一些大型项目,深刻的了解了数据库操作的运维与可靠性操作是非常消耗精力和时间的。所以这篇文章用来记录一些数据库的操作,方便后续直接使用。

因为今年一直在做Hermes和OpenClaw以及一些自己的开源项目,所以用到SQLite 比较多,由于深刻的认识到传统的大型多并发数据库要做好是需要非常大的精力去操作的,所以后续一些个人文章非大型场景且专题数据库的操作,都会用SQLite来进行。

重新建表,保留表中原来数据,主要为重新增加主键问题

bash 复制代码
-- 直接在文件层面复制 english_learning.db

-- 建新表(带 domain,主键含领域)
CREATE TABLE MemoryNodes_new (
    user_id INTEGER NOT NULL,
    word_id INTEGER NOT NULL,
    domain TEXT NOT NULL DEFAULT 'web_programming',
    review_count INTEGER DEFAULT 1,
    ease_factor REAL DEFAULT 2.5,
    interval_days INTEGER DEFAULT 1,
    memory_strength REAL DEFAULT 0.5,
    forgetting_curve REAL DEFAULT 1.0,
    next_review_at DATETIME,
    memory_status TEXT DEFAULT 'learning',
    difficulty_score REAL DEFAULT 0.5,
    priority_score REAL DEFAULT 0.5,
    is_active INTEGER DEFAULT 1,
    word_root_score REAL DEFAULT 0.5,
    word_affix_score REAL DEFAULT 0.5,
    etymology_score REAL DEFAULT 0.5,
    spelling_score REAL DEFAULT 0.5,
    pos_score REAL DEFAULT 0.5,
    context_score REAL DEFAULT 0.5,
    frequency_score REAL DEFAULT 0.5,
    consecutive_correct INTEGER DEFAULT 0,
    consecutive_wrong INTEGER DEFAULT 0,
    static_priority_score REAL DEFAULT 0.0,
    last_reviewed_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (user_id, domain, word_id),
    FOREIGN KEY (user_id) REFERENCES Users(user_id) ON DELETE CASCADE,
    FOREIGN KEY (word_id) REFERENCES Words(word_id) ON DELETE CASCADE
);

-- 迁移旧数据(存量行归到默认领域 web_programming)
INSERT INTO MemoryNodes_new
    (user_id, word_id, domain, review_count, ease_factor, interval_days,
     memory_strength, forgetting_curve, next_review_at, memory_status,
     difficulty_score, priority_score, is_active, word_root_score, word_affix_score,
     etymology_score, spelling_score, pos_score, context_score, frequency_score,
     consecutive_correct, consecutive_wrong, static_priority_score,
     last_reviewed_at, created_at, updated_at)
SELECT
    user_id, word_id, 'web_programming', review_count, ease_factor, interval_days,
    memory_strength, forgetting_curve, next_review_at, memory_status,
    difficulty_score, priority_score, is_active, word_root_score, word_affix_score,
    etymology_score, spelling_score, pos_score, context_score, frequency_score,
    consecutive_correct, consecutive_wrong, static_priority_score,
    last_reviewed_at, created_at, updated_at
FROM MemoryNodes;

-- 换表
DROP TABLE MemoryNodes;
ALTER TABLE MemoryNodes_new RENAME TO MemoryNodes;

-- 重建索引(原 idx_memory_user 已随旧表删除)
CREATE INDEX IF NOT EXISTS idx_memory_user ON MemoryNodes(user_id);
CREATE INDEX IF NOT EXISTS idx_memory_user_domain ON MemoryNodes(user_id, domain);
相关推荐
云祺vinchin18 分钟前
医疗行业灾备实践:一套方案实现HIS、EMR、PACS系统安全闭环
数据库·安全·数据备份·医疗安全·容灾备份
梦想不只是梦与想22 分钟前
MySQL 中的窗口函数
数据库·mysql·窗口函数
严同学正在努力1 小时前
Oracle数据技术运维大全(下篇)
运维·数据库·ai·oracle·架构
catino1 小时前
spring-事务@Transactional
java·数据库·spring
数智启示录1 小时前
Apache Doris 4.0.8 数据建模实战(第 2 篇):同一批订单写进三种模型,为什么得到三个答案
大数据·数据库·面试
数智启示录2 小时前
Apache Doris 4.0.8 CDC 正确性(第 9 篇):Flink Checkpoint 一直成功,表里为什么仍是旧数据
大数据·数据库·经验分享·面试·flink
拾光Ծ3 小时前
【MySQL】对表数据的操作:增删查改(CRUD)
android·数据库·sql·mysql
这个DBA有点耶16 小时前
2026年分布式数据库有哪些主流选择?先评估这5个维度再决定
数据库·分布式·dba