Skip to content

Use string-keyed lookup data ​

A string lookup can live in a table when entries should be queried, constrained and updated independently. Use a primary key to make each string resolve to at most one stored row.

sql
CREATE TABLE country_names(code TEXT PRIMARY KEY, name TEXT NOT NULL);
INSERT INTO country_names VALUES ('FR', 'France'), ('JP', 'Japan');

SELECT name FROM country_names WHERE code = 'JP';

Choose an explicit normalization rule when callers may supply different spellings or letter cases. For example, normalize incoming codes in the application and store that same representation consistently. Do not silently assume that all string comparisons ignore case.

For a small collection stored inside a single row or expression, a native Map(String, String) is another option:

sql
SELECT map_element(map('FR', 'France', 'JP', 'Japan'), 'JP');

Maps retain pair order and duplicate keys. Lookup selects the first matching pair, while a missing key returns the value type's default. Use map_contains when a stored default and a missing key must be distinguished. A map is therefore not the same contract as a unique-key lookup table.

See map functions, Map and table constraints.