The VikiD Truth

Knowledge base / Computing

Read This! | Picolisp | Picolisp Machine | Pil Sources | Linux | BASH | C-Programmming | Databases | Javascipt | Python | Operating Systems | AssemblyLanguage | Computer Security | Firewalls | Exploitation | Social Engineering | Metasploit | Emacs | vim | Pharo Smalltalk | Picolisp DB’s

Databases

Here is the truth about databases: persistence is hard, and “just write it to a file” becomes a nightmare the moment two writers, a crash or a query show up. A database is the carefully engineered answer to those three problems.

What it is, in plain words

A database is a system that stores data so that you can find it again, change it safely, and not lose it when the power goes out. A database management system (DBMS) is the software that provides that – PostgreSQL, SQLite, MySQL, Redis, MongoDB, PicoLisp’s built-in DB.

The workhorse model is still the relational one: data in tables of rows and columns, queried with SQL, related by keys. Around it live other models that fit different shapes of data:

ModelGood forExamples
Relationalstructured records, joins, transactionsPostgreSQL, SQLite, MySQL
Key-valuecaches, sessions, simple lookupsRedis, BoltDB
DocumentJSON-shaped recordsMongoDB, PostgreSQL JSONB
Wide-columnhuge sparse tablesCassandra, ScyllaDB
Graphrelationships as first-class dataNeo4j, EdgeDB
Embedded Lisp DBtiny, transactional, in-processPicoLisp DB

Why it matters

Almost every real program eventually needs to remember something: users, posts, configs, sensor readings. Get persistence wrong and you get corruption, lost writes, race conditions and outages. Get the model wrong and you spend years fighting your own schema. Understanding databases means choosing the right tool and sleeping at night.

An everyday analogy: a library with a librarian

A pile of papers in a drawer is storage. A library is a database: a catalogue (the index) lets you find a book without scanning every shelf; the librarian (the DBMS) makes sure two people don’t reshelve the same book at once; and there’s a fireproof archive (write-ahead logging) so a fire in the reading room doesn’t erase the collection. SQL is the polite form you fill in at the desk.

flowchart LR
  App[Your program] --> SQL[SQL / API]
  SQL --> Engine[Query planner + executor]
  Engine --> Idx[Indexes]
  Engine --> Stor[Storage engine]
  Stor --> WAL[Write-ahead log]
  Stor --> Disk[Pages on disk]

A hands-on example: SQLite in five minutes

SQLite is a full relational database in a single file – no server. Perfect for learning, apps and even many production uses.

sudo xbps-install -S sqlite      # or: apt install sqlite3
sqlite3 blog.db
CREATE TABLE posts (
  id      INTEGER PRIMARY KEY,
  title   TEXT NOT NULL,
  body    TEXT NOT NULL,
  created TEXT DEFAULT (datetime('now'))
);

INSERT INTO posts (title, body) VALUES
  ('Hello', 'First post'),
  ('Lisp',  'Why PicoLisp keeps winning');

SELECT id, title, created FROM posts ORDER BY id DESC;

-- transactions: all or nothing
BEGIN;
UPDATE posts SET title = 'Hello, world' WHERE id = 1;
DELETE FROM posts WHERE id = 2;
COMMIT;          -- or ROLLBACK;

The same ideas in PostgreSQL (the open-source relational database I’d pick for anything networked):

sudo xbps-install -S postgresql
sudo -u postgres initdb -D /var/lib/postgres/data
# enable the service, then:
psql -U postgres -c "CREATE USER vikid WITH PASSWORD '…';"
psql -U postgres -c "CREATE DATABASE wiki OWNER vikid;"
psql -U vikid -d wiki -c 'CREATE TABLE posts (id SERIAL PRIMARY KEY, title TEXT);'

And in PicoLisp, persistence is a first-class language feature – entities and relations live in a transactional DB with almost no ceremony. See PicoLisp DB.

The words that actually matter

Common misconceptions

Connections

Persistance is a really headache, achieving it reliably in a distributed environment is a nightmare.

Last edited · 7 revisions

Tags: #Computing