【星海出品】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);
相关推荐
一只积极向上的小咸鱼1 小时前
第一性原理思考:从拆解问题到用实验检验答案
数据库
这个DBA有点耶2 小时前
时序数据库InfluxDB高基数问题深度拆解:内存索引膨胀机制与OOM完整链路
数据库·架构·dba
加油yx2 小时前
MySQL数据类型
数据库·mysql
蓝胖的四次元口袋2 小时前
Redis知识梳理(1)
数据库·redis·缓存
夜雪一千2 小时前
MySQL 数据加密实战:从传输、存储到字段级加密,分清场景不踩坑
数据库·mysql
wefg13 小时前
【Redis】C++ 自定义客户端
数据库·redis·缓存
这个DBA有点耶3 小时前
为什么UPDATE比SELECT危险得多?锁范围等于扫描行数
数据库·架构·代码规范
天国梦4 小时前
国密门禁品牌怎么选?2026年最新3个避坑要点
运维·服务器·数据库·python
DB哥讲数据库4 小时前
【达梦数据库安装教程】超详细图文讲解-附达梦安装包
数据库·oracle
Hoxy.R4 小时前
【金仓数据库产品体验官】KingbaseES V9R2C16 坏块检测与修复完整测试流程
数据库