Data Types, Constraints and Nullability

A type is part of a data contract, not just a storage setting.

BeginnerIntermediateExpert · 4 min read · Last updated:

In brief

Data types describe what values a field can represent. Constraints define additional rules. Nullability says whether a value may be absent; it does not explain why it is absent.

Choose types for meaning

Use a type that matches the domain: dates for calendar dates, timestamps for instants, decimal types for exact currency amounts, and bounded categories for controlled statuses. Avoid storing values as free text when the business rule depends on numeric or temporal behavior.

Clarify null and missing

A null value may mean unknown, not yet supplied, not applicable, withheld or unavailable. These meanings are not equivalent. If users need to distinguish them, model an explicit status or reason rather than relying on a single null.

Use constraints deliberately

  • Required: a value must be present.
  • Unique: values or combinations must not repeat within a scope.
  • Reference: a value must correspond to an allowed related record.
  • Range or domain: a value must satisfy a rule.

Database behavior differs by platform. PostgreSQL, for example, documents how null interacts with check and unique constraints in its constraints reference.

Document the field contract

Field detailExample
DefinitionNet line amount after item discount, before tax
Type and unitDecimal amount in transaction currency
Null reasonUnknown until the order is invoiced
Allowed valuesStatus code set with definitions
Owner and testSales operations; value reconciles to source event
Implementation depth Examples, failure cases and review points

Select types from meaning and range

Choose a data type by the values and operations the business needs, not only by what a source happens to send. Decide whether an identifier is an opaque label or a number used in arithmetic; postal codes and account numbers often look numeric but should not be summed. For amounts, specify currency, precision and rounding behavior. For measurements, specify unit, scale and conversion rules.

For temporal values, distinguish a date, a local wall-clock time, an instant and a duration. A timestamp without a zone can be ambiguous when systems cross daylight-saving or geographic boundaries. Define the source timezone and conversion rule, and preserve original event time if operationally necessary. Do not compare strings that merely look like dates unless the format is guaranteed and validated.

Use constraints to express invariants

A uniqueness constraint states that a key cannot repeat within its defined scope. A foreign key or equivalent quality rule states that a reference must resolve. A check constraint can limit a quantity to an allowed range or a status to known values. These rules protect consumers only when they are enforced in the write path or consistently tested in a pipeline.

RuleExampleFailure response to define
Required valueEvery invoice has an issue dateReject, quarantine or route for remediation
UniquenessOne active record per scoped external identifierDetect duplicate and identify source owner
RangeQuantity is positive for a shipment eventReject invalid event or represent a reversal explicitly
ReferenceOrder line refers to a known orderHold unresolved row and monitor age
DomainState belongs to approved lifecycle valuesHandle new values through contract review

Distinguish null, unknown and not applicable

A null can mean missing, not yet received, withheld, not applicable or unknown depending on system conventions. These meanings should not be conflated if users or rules need to treat them differently. Use an explicit status, separate reason code or controlled member where needed. Avoid fake sentinel values such as “1900-01-01” unless a documented interface requires them; sentinels can be mistaken for real observations and distort calculations.

Optionality should follow the business lifecycle. A shipment date may be absent before shipment and required after shipment. A single static not-null constraint might not express the full rule; validate state-dependent requirements in application logic, workflow validation or a quality test.

Evolve types and constraints safely

Changing an integer identifier to text, tightening nullability or narrowing decimal precision can break producers and consumers. Profile existing data first, identify incompatible values, stage the migration and provide a clear failure route. When a new enum value appears, consumers should either handle unknown values safely or reject them visibly according to contract behavior.

  • Do types match the real domain range and semantics?
  • Are units, precision, time zone and rounding explicit?
  • Are constraints enforced or monitored where data enters?
  • Can null states be interpreted consistently?
  • Have type changes been checked against existing data and consumers?

For identity constraints, continue to Keys, Identifiers and Referential Integrity. For interface changes, see Data Contracts Between Producers and Consumers.

Review nullability and defaults

Distinguish unknown, not applicable, not yet supplied, and intentionally blank. A default should represent a confirmed business rule, not conceal missing data. Consider whether an explicit status or reason is clearer than null for important lifecycle distinctions.

Test inserts, updates, migration backfills, and API serialization. Document how consumers interpret nulls and defaults. Monitor unexpected null growth after deployment.