【星海出品】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);
相关推荐
djjjx.6 小时前
Mysql数据库基础篇(八):详细讲解复合查询、多表查询、自连接、多行、多列子查询与合并查询
数据库·mysql
CoderYanger10 小时前
Java EE 进阶:9.MyBatis 操作数据库(进阶)(第八弹):MyBatis Generator
java·数据库·spring boot·spring·java-ee·tomcat·mybatis
MSTcheng.12 小时前
【MySQL】MySQL数据库——表的操作
数据库·mysql·adb
Yeats_Liao12 小时前
物联网平台MongoDB实战|(一)为什么选择MongoDB
数据库·物联网·mongodb
鹤落晴春13 小时前
MySQL日志梳理
运维·数据库·mysql
五彩小白14 小时前
Redis的过期删除和内存淘汰机制
数据库·redis·缓存
学长毕设14 小时前
基于SpringBoot 博物馆文物租赁及修复管理系统-附源码
大数据·数据库·spring boot
apcipot_rain14 小时前
C++转Java日记(14)
数据库
龙腾AI白云17 小时前
数字孪生与计算机视觉:让机器看懂现场
大数据·数据库·机器学习·知识图谱·pygame
千寻技术帮18 小时前
基于Spring Boot+Vue的农产品销售管理系统(源码+文档+数据库,附部署教程)
java·数据库·spring boot·后端·mysql·vue