CREATE TABLE tasks( task_id INTEGER PRIMARY KEY, title TEXT NOT NULL UNIQUE, active_id INTEGER, status TEXT NOT NULL DEFAULT 'backlog', creation_date DATE DEFAULT (date('now')), due_date TEXT, CHECK ( due_date IS NULL OR due_date GLOB '????-??-??' ) ); -- Basically when we add a new task the active id is set -- but when a task is done we want it to be able to be null so we need to increment -- COALESCE is a cool function that returns the first non-null argument which takes care of empty -- active task list CREATE TRIGGER autoinc_active_id AFTER INSERT ON tasks BEGIN UPDATE tasks SET active_id = (SELECT COALESCE((SELECT MAX(active_id) FROM tasks) + 1, 1)) WHERE task_id=NEW.task_id; END; CREATE TABLE entries( entry_id INTEGER PRIMARY KEY, task_id INTEGER NOT NULL REFERENCES tasks(task_id), body TEXT NOT NULL, creation_date DATETIME DEFAULT (datetime('now', 'localtime')) ); INSERT INTO tasks (title) VALUES ('test1'), ('test2'), ('test3'); INSERT INTO entries (task_id, body) VALUES (1, 'this is an entry for test1'), (2, 'this is an entry for test2'), (3, 'this is an entry for test3'); UPDATE tasks SET status = 'today' WHERE title = 'test2'; CREATE VIEW all_info AS SELECT * FROM entries JOIN tasks ON entries.task_id = tasks.task_id;