# Reference Data in KDB-X DB Service

_This page explains what reference data is, how to define a reference table and link it to your timeseries data with a foreign key, and how to import and query it._

> **Note — Clustered deployment**
>
> The examples on this page define tables through the table management API, used in single-node deployments. In a clustered deployment, define reference tables and their foreign keys in the assembly YAML instead — refer to [Manage Tables (Clustered)](manage-tables-clustered.md).
>
## Reference data

Reference data is non-timeseries data that has some meaningful relation to other data. Reference data is typically small, static, and/or slowly changing.

A typical example of reference data would be organizational data, which may include postal codes, addresses, names, dates of birth, and more. Reference data are pieces of information that you want to _reference_ when you query or analyze other data.

The examples on this page use a `master` table, holding the description, security type, and listing exchange of each instrument, as reference data for the `quote` timeseries table. Both tables come from the [sample data](quickstart.md#import-sample-data).

## Memory considerations

For reference data the table [type](manage-tables.md#table-types) determines the memory requirements of having the table. With a type of `splayed` or `basic` the table is stored on disk and memory mapped, and only recent records or updates are held in memory until they are written down at end-of-interval. If instead, for performance reasons, you want the table to be held fully in memory, set the type to `splayed_mem`.

## Define a reference table

To define a reference table, define the schema and pick one or more columns as primary keys with `primaryKeys`. A primary key should uniquely identify the reference data. Refer to [keyed tables](manage-tables.md#keyed-tables) for the ordering rules that apply to the key columns.

=== "q"
    ```q
    // Define columns; the primary key column is listed first
    masterCols:([]name:("sym";"description";"cusip";"securityType";"testFlag";"ex";"roundLot";"issueprice");colType:("symbol";"string";"symbol";"symbol";"boolean";"char";"short";"float"))

    // Create the reference table ('master'), keyed on 'sym'
    session.createTable["master";([tableType:"splayed";primaryKeys:enlist"sym";columns:masterCols])]
    ```

=== "Python"
    ```python
    session.create_table(
        table="master",
        type="splayed",
        primaryKeys=["sym"],
        columns=[
            {"name": "sym", "type": "symbol"},
            {"name": "description", "type": "string"},
            {"name": "cusip", "type": "symbol"},
            {"name": "securityType", "type": "symbol"},
            {"name": "testFlag", "type": "boolean"},
            {"name": "ex", "type": "char"},
            {"name": "roundLot", "type": "short"},
            {"name": "issueprice", "type": "float"},
        ]
    )
    ```

=== "REST"
    ```bash
    curl -s -X POST "http://localhost:8080/api/v1/tables/master" \
    -H "Accept: application/json" \
    -H "Content-Type: application/json" \
    -d '{
        "type": "splayed",
        "primaryKeys": ["sym"],
        "columns": [
        {"name": "sym", "type": "symbol"},
        {"name": "description", "type": "string"},
        {"name": "cusip", "type": "symbol"},
        {"name": "securityType", "type": "symbol"},
        {"name": "testFlag", "type": "boolean"},
        {"name": "ex", "type": "char"},
        {"name": "roundLot", "type": "short"},
        {"name": "issueprice", "type": "float"}
        ]
    }'
    ```

## Add a foreign key

In the corresponding timeseries table, set the `foreign` property of a column to the table and column name of a primary key column in the reference data. This indicates that the values of the column are a foreign key into a column in another table, and is given in the form `table.column`, where the table is another table in the same schema.

Both are checked when the table is created:

- the column type must match the type of the column in the other table, or the request is rejected with `foreign keys refer to columns of mismatching type`
- the referenced table must already exist, or the request is rejected with `foreign keys refer to invalid table`

So create the reference table first, then the table that points at it.

The `quote` table of the [example create](manage-tables.md#example-create) points its own `sym` column at the reference data by adding `foreign`, leaving the rest of the definition unchanged:

=== "q"
    ```q
    `name`type`foreign`attrMem`attrDisk`attrOrd!("sym";"symbol";"master.sym";"grouped";"parted";"parted")
    ```

=== "Python"
    ```python
    {"name": "sym", "type": "symbol", "foreign": "master.sym", "attrMem": "grouped", "attrDisk": "parted", "attrOrd": "parted"},
    ```

=== "REST"
    ```json
    {"name": "sym", "type": "symbol", "foreign": "master.sym", "attrMem": "grouped", "attrDisk": "parted", "attrOrd": "parted"}
    ```

A [describe](manage-tables.md#describe) of `quote` reports the foreign key back on the column.

A foreign key also constrains the order in which the two tables can be removed: a reference table cannot be [dropped](manage-tables.md#drop) while another table still references it.

## Import reference data

Reference data is imported like any other data, with either a [file import](import.md#file-import) or an [API data import](import.md#api-data-import). Because the table is keyed, records with matching keys update the existing records rather than being appended, so a later import of the same key refreshes it.

=== "q"
    ```q
    job:session.importFiles["master";"master.csv"]
    ```

=== "Python"
    ```python
    job = session.import_files(table="master", path="master.csv")
    ```

=== "REST"
    ```bash
    curl -s -X POST "http://localhost:8080/api/v1/imports/files" \
    -H "Accept: application/json" \
    -H "Content-Type: application/json" \
    -d '{"table": "master", "path": "master.csv"}'
    ```

An unkeyed table can still be the target of a foreign key, and a query resolves the key either way. It accumulates a duplicate row on every re-import of the same key, however, and a query that resolves the foreign key then returns the value from one of those rows without reporting the ambiguity.

## Query reference data

A query reads columns of the reference table by naming them in `table.column` form, and the service resolves the foreign key and joins the reference data for you. These [reference columns](query.md#reference-columns) can be used to select, group and filter.

The query below returns quotes with the security type of each instrument taken from `master`, keeping only the exchange-traded funds (ETFs):

=== "q"
    ```q
    session.querySimple[`quote;([
        startTS:2026.03.02D09:30:00;
        endTS:2026.03.02D09:35:00;
        agg:("time";"sym";"bid";"ask";"master.securityType");
        filter:enlist[("=";"master.securityType";"ETF")];
        sortCols:enlist "time"])]
    ```

=== "Python"
    ```python
    session.query_simple(table='quote',
        startTS='2026.03.02D09:30:00',
        endTS='2026.03.02D09:35:00',
        agg=['time', 'sym', 'bid', 'ask', 'master.securityType'],
        filter=[['=', 'master.securityType', 'ETF']],
        sortCols=['time'],
        return_as="pandas")
    ```

=== "REST"
    ```bash
    curl -X POST "http://localhost:8080/api/v1/query/simple" \
        -H "Content-Type: application/json" \
        -d '{"table":"quote",
            "startTS":"2026.03.02D09:30:00",
            "endTS":"2026.03.02D09:35:00",
            "agg":["time","sym","bid","ask","master.securityType"],
            "filter":[["=","master.securityType","ETF"]],
            "sortCols":["time"]}'
    ```

The result carries the reference column under its dotted name:


```json
{"time":"2026-03-02T09:30:04.439951076","sym":"VDE","bid":126.01,"ask":126.09,"master.securityType":"ETF"}
```

Not every query API resolves reference columns; refer to [reference columns](query.md#reference-columns) for the APIs that support them, and for the equivalent form in a q query.

## Next steps

- Refer to [manage tables](manage-tables.md) for table types, keyed tables, and column definitions.
- Refer to [import](import.md) for the full set of import options.
- Refer to [query](query.md) for the query APIs.
