Order status, payment method, gender, document type: almost every business screen has a field where the user picks one value from a fixed list. It is tempting to put those values in an enum, a CHECK constraint, or a switch in the application. That works until someone asks for a new status, or the same list starts to drift across screens.
A common-codes table moves those values into data. An administrator can add, rename, or deactivate a code on a screen, and dropdowns, radio buttons, and search filters all read from the same place. The design looks simple. What usually goes wrong later is how the rest of the system refers to it.
Below is the two-table starting point I use, along with its trade-off and a short checklist for a design review.
Two tables: the group and the item
A code never lives alone. PAID belongs to order status; CARD belongs to payment method. Put the information that applies to the whole set — the group name, a description, and an active flag — in code_group. Put the values themselves in code_item.
code_group code_item +-----------------------+ +----------------------------+ | PK group_code | 1 N | PK,FK group_code | | group_name |------<| PK item_code | | group_description| | code_name | | is_active | | sort_order | +-----------------------+ | parent_item_code FK | | attribute_1 .. 5 | | is_active | +----------------------------+ | ^ | | self-reference, +--------------+ same group only The relationship is identifying: because group_code is part of the item's primary key, an item cannot exist without its group. A shortened form, written with generic SQL types and usable on PostgreSQL, is below. I have left out the audit columns (created_at, created_by, updated_at, updated_by) that I would add to every table. Lengths are examples.
CREATE TABLE code_group ( group_code VARCHAR(30) NOT NULL, group_name VARCHAR(100) NOT NULL, group_description VARCHAR(1000), is_active BOOLEAN DEFAULT TRUE NOT NULL, CONSTRAINT pk_code_group PRIMARY KEY (group_code) ); CREATE TABLE code_item ( group_code VARCHAR(30) NOT NULL, item_code VARCHAR(30) NOT NULL, code_name VARCHAR(100) NOT NULL, code_description VARCHAR(1000), sort_order INTEGER NOT NULL, parent_item_code VARCHAR(30), attribute_1 VARCHAR(100), -- and attribute_2 .. attribute_5 is_active BOOLEAN DEFAULT TRUE NOT NULL, CONSTRAINT pk_code_item PRIMARY KEY (group_code, item_code), CONSTRAINT fk_code_item_group FOREIGN KEY (group_code) REFERENCES code_group (group_code), CONSTRAINT fk_code_item_parent FOREIGN KEY (group_code, parent_item_code) REFERENCES code_item (group_code, item_code) ); The second foreign key is what makes the hierarchy safe. parent_item_code lets items in the same group form a tree — for example, a document type with subtypes. Because the key is (group_code, parent_item_code) pointing at (group_code, item_code), the database rejects a parent from another group.
Why the primary key is a composite
The primary key is (group_code, item_code), not the item code alone, so the same item code can be reused in different groups. A numeric style such as 10, 20, 30 can appear in more than one group. In examples I prefer to ship, I use mnemonic values such as PAID and CARD, which are easier to read in SQL. Either style is fine if you use it consistently.
The spare columns attribute_1 through attribute_5 cover operational extras without a program change: a payment-gateway mapping code, or a flag that controls whether the value appears on a particular screen. The column name itself tells you nothing, so record what each spare column means, per group, in group_description or in the table specification. A year later, nobody will remember otherwise.
The convention you must not lose
Here is the part that is easy to miss. A business table almost never stores both parts of that key. The order_status_code column on a sales order stores only the item code — PAID, SHIPPED, and so on. The developer already knows which group the column uses. The values live in the database; which group they come from is a convention in the specification and in the program.
That usual practice has a clear cost. You cannot declare a proper foreign key from sales_order.order_status_code to code_item, and every join has to name the group:
SELECT o.sales_order_id, o.order_status_code, i.code_name AS order_status_name FROM sales_order o LEFT JOIN code_item i ON i.item_code = o.order_status_code AND i.group_code = 'ORDER_STATUS' ORDER_STATUS is, in effect, hard-coded — or, better, a shared constant. If you forget the group condition, a join can pick up a row from the wrong group.
To drop the group condition from the join, you either make item codes unique across the whole system, or you store a reference key — a surrogate such as code_item_id, or the group and the item together. The first reduces reuse. The second makes the business tables a little heavier, and a surrogate alone no longer tells you what the code means when you look at a row.
I still store the item code, because it is readable in queries and in logs. The group convention then has to live somewhere reliable. The most dependable place I have found is the table specification: in the remarks for order_status_code, write the group and the expected values — for example, "Common code (ORDER_STATUS: PAID, PROCESSING, SHIPPED, …)". Keep that same group in one shared constant, and use it for dropdowns, validation, and joins.
When a single table, or no table, is better
You can fold the two tables into one, with top-level rows acting as groups and child rows as items. The code value itself is then the primary key, so values must be unique across the system. If order status already uses 10, payment method cannot; you prefix them, ORD_10 and PAY_10. The gain is that a business table can store one code and declare a real foreign key.
| Comparison | Two tables (code_group + code_item) | Single table |
|---|---|---|
| Reusing item codes | Yes (10 can appear in several groups) | No (values must be unique system-wide) |
| Foreign key from a business table | Not possible if only the item code is stored | Possible with a single code |
| Group-level information | Lives on code_group | Lives on the top-level row |
| A better fit when | Many groups, and the group/item split is already familiar | A new system that wants database-level referential integrity first |
A shared table that holds many kinds of codes is often called a One True Lookup Table (OTLT), and it is widely treated as an anti-pattern. The criticism is fair: you give up a proper foreign key when you store only the item code, every code shares the same type and length, and spare attributes hide their meaning. I accept those costs for a specific reason. In business systems, administrators add and deactivate codes on a screen, and many screens build a dropdown with one generic query. Creating a new table and shipping a release for every new set of codes usually costs more. The safeguards below are what make that trade-off safe.
Use a dedicated lookup table when a set of codes has its own attributes or rules, or when business tables must have a real foreign key — for example, a job_grade table, not a row in code_group. For a small, almost unchanging set that nobody will manage on an admin screen, a CHECK constraint or an enum is enough.
Pitfalls that show up in production
- Do not change a code value that is already in use. Business tables store that value. If you change
PAIDtoSETTLEDincode_item, existing orders are left with a code that no longer has a name. You may change the display name; leave the code itself alone. - Do not physically delete a code. Set
is_activetoFALSE. Dropdowns should list only active codes; lookups for existing rows should still resolve the name. If you can, revokeDELETEon the code tables from the application's database account. - Do not rely on a foreign key from the business table to reject bad codes. When the table stores only the item code, that foreign key is usually not there. The save service — the service-layer logic that writes the row — should check that the group and item exist and that the item is active. The same check should cover APIs, file uploads, and batch jobs, not only the screen.
- Make
is_activeandsort_orderNOT NULL, withis_activedefaulting toTRUE. A null flag is a common reason a code disappears from a dropdown and nobody can see why.
A short design checklist
- [ ] Group and item are split when you need to reuse item codes; a single table is used when you do not.
- [ ] A dedicated lookup table is used when a code set has its own attributes or rules, or needs a real foreign key.
- [ ] Every code column in a business table has its group recorded in the table specification (and in one shared constant in the program).
- [ ] The meaning of each spare attribute is recorded per group.
- [ ] A parent item is allowed only within the same group (enforced by the composite foreign key).
- [ ] Item codes in use are not changed; only display names are.
- [ ] Codes are deactivated with
is_active = FALSE, never physically deleted. - [ ] The save service checks that the group and item exist and that the item is active.
The structure is not complicated. What keeps it safe is the convention you write down, and the checks you run when a row is saved.
I wrote more on this pattern, and on the table designs that usually come after it — role-based permissions, effective-dated prices, HR, purchasing, and e-commerce — in Before You CREATE TABLE: Database Design Patterns for Business Systems, from Codes and Permissions to HR, Purchasing, and E-commerce. The English edition is on Gumroad.
True Rows writes from 20+ years of work on enterprise system integration (SI) projects in Korea, focused on business analysis and database design.


