KDB knowledge

2. Your first database

What you will learn

The examples use the database market with the sample data (see the quick start).

A database is a folder

A KDB database is one folder. You choose the name. When you open a folder that does not exist, KDB creates a new empty database there.

Get-ChildItem market -Name
files
wal
wal_archive
_trash
kdb.lock
_manifest.a
_manifest.b
NameWhat it is
files\The data files. One file for each table part (partition), plus the table list (catalog).
wal\The write-ahead log. Every change goes here first.
wal_archive\Old log files. Only backups and repairs use them. The oldest are deleted after 1 GB (changeable: ALTER DATABASE ... SET WAL_ARCHIVE, or wal_archive_mb in kdb.conf for all databases).
_trash\Old file versions. KDB deletes them after 1 hour (trash_keep_hours in kdb.conf, or KDB_TRASH_KEEP_HOURS).
kdb.lockOnly one program may open the database. This file is locked while it is open.
_manifest.a, _manifest.bThe list of current files. It is saved twice, for safety.

Rules:

kdb shell: type SQL

kdb shell <folder> opens a prompt. Type SQL and end it with ;. A statement can use many lines. The prompt ...> means "the statement is not finished yet".

PS D:\kdb-work> kdb shell market
KDB 0.2.0-dev shell on market
End statements with ';'. Type .help for commands, .exit to quit.
kdb> .help
  .tables              list tables
  .schema <table>      show CREATE TABLE
  .format table|csv|json
  .maxrows <n>         rows shown per query (0 = all)
  .check               read every file and report damage
  .backup              make a verified backup (needs --backups)
  .exit                quit
kdb> .tables
name         | kind       | partition | columns | rows
-------------+------------+-----------+---------+-----
brokers      | relational | NONE      |       2 |    5
trade_log    | timeseries | DAY       |       8 |   32
live_data    | timeseries | DAY       |      12 |   12
ochl_day     | timeseries | YEAR      |       7 |   57
ochl_minutes | timeseries | MONTH     |       7 |  956
securities   | relational | NONE      |       3 |    5
(6 rows, 0.0 ms)
kdb> SELECT symbol, close
...> FROM ochl_day
...> WHERE time IN '2026-09-10';
symbol | close
-------+------
CITY   | 504.9
COAST  | 376.1
VOLT   | 191.2
(3 rows, 0.4 ms)
kdb> .format csv
kdb> .schema brokers
create_sql
"CREATE TABLE brokers (
  id INT PRIMARY KEY,
  name STRING NOT NULL
)"
kdb> SELECT id, name FROM brokers LIMIT 3;
id,name
1,Keystone Securities
34,Vision Securities
45,Imperial Securities
kdb> .exit

Shell commands

Shell commands start with a dot. They do not need ;.

CommandWhat it does
.helpShows the commands
.tablesLists the tables (same as SHOW TABLES)
.schema <table>Shows the CREATE TABLE statement of a table
.format table / csv / jsonHow results are printed. table is the default.
.maxrows <n>Shows at most n rows per query (default 1000). 0 = all rows.
.checkReads every file and reports damage
.backupMakes a backup. Start the shell with --backups <folder> first.
.exit or exitQuits the shell. Ctrl+C also works.

Tips:

kdb sql: run SQL once

kdb sql <folder> "SQL" opens the database, runs the SQL, prints the result and closes the database. This is good for scripts.

kdb sql market "SELECT count(*) AS minute_rows FROM ochl_minutes"
minute_rows
-----------
        956
(1 row, 0.5 ms)

Many statements

Separate statements with ;. KDB prints one result for each statement, with an empty line between them.

kdb sql market "SELECT count(*) AS securities FROM securities; SELECT count(*) AS brokers FROM brokers"
securities
----------
         5
(1 row, 0.2 ms)

brokers
-------
      5
(1 row, 0.1 ms)

If one statement fails, KDB stops there. The statements before it are already saved. The statements after it do not run. See chapter 4.

SQL from a file

Write the SQL in a file. Then send the file into kdb sql with a pipe. The - means "read the SQL from the pipe".

@"
-- report.sql: closing prices on the last day
SELECT symbol, close
FROM ochl_day
WHERE time IN '2026-09-10'
ORDER BY close DESC;
"@ | Set-Content report.sql -Encoding UTF8

Get-Content report.sql -Raw | kdb sql market -
symbol | close
-------+------
CITY   | 504.9
COAST  | 376.1
VOLT   | 191.2
(3 rows, 0.2 ms)

Options of kdb sql

OptionMeaning
--format tableText table (default)
--format csvCSV, good for Excel and scripts
--format jsonJSON, good for programs
--max-rows NAt most N rows per statement. Default: 1000 for table, no limit for csv and json.
--backups <folder>Backup folder, needed for BACKUP DATABASE

Chapter 7 shows the formats in detail.

Exit codes

Every kdb command ends with an exit code. Scripts use it to know if the command worked.

CodeMeaningExamples
0OKThe query ran. The backup was made. No damage found.
1ErrorWrong SQL. Duplicate key. The database is open in another program. Wrong arguments.
2Damagekdb check found damage. A file could not be read. A repair could not fix everything.
3Needs a newer KDBThe database uses a stored-format feature this kdb does not know. Nothing was changed. See chapter 24.

In PowerShell the exit code is in $LASTEXITCODE:

kdb sql market "SELECT count(*) AS n FROM brokers"
"exit code: $LASTEXITCODE"
kdb sql market "SELECT count(*) FROM brokerz"
"exit code: $LASTEXITCODE"
n
-
5
(1 row, 0.2 ms)
exit code: 0
error: line 1, column 22: table or view 'brokerz' does not exist
exit code: 1

Error messages say where the problem is: line 1, column 22 is the place of brokerz in the SQL. In bash the exit code is in $?.

kdb shell ends with the exit code of the last statement or command you ran.

Try it yourself

  1. Open the shell on market and list the columns of live_data.
  2. With one kdb sql command, count the rows of trade_log and live_data.
  3. Run kdb sql market "SELECT * FROM nothing". What is the exit code?
Answers
  1. kdb> SHOW COLUMNS FROM live_data;

    (or .schema live_data after .format csv)

  2. kdb sql market "SELECT count(*) AS trades FROM trade_log; SELECT count(*) AS snapshots FROM live_data"

    The answers are 32 and 12.

  3. The exit code is 1, because the table does not exist.

Next: 3. Tables