-- Example Address: operational tables (PostgreSQL 17). -- Source: entities/address.md (existence: independent, mutability: reference). CREATE TABLE example_address.address ( address_id uuid PRIMARY KEY DEFAULT gen_random_uuid(), address_type text CHECK (address_type IN ('Street Address', 'PO Box', 'Registered Office', 'Overseas Address')), -- enum:Address Type address_line_1 text, address_line_2 text, suburb text, city text, state_or_region text, postcode text, country text NOT NULL, -- constraint: Country Required created_at timestamptz NOT NULL DEFAULT now(), updated_at timestamptz NOT NULL DEFAULT now(), CONSTRAINT street_address_requires_address_line_1 -- constraint of the same name in the model CHECK (address_type <> 'Street Address' OR address_line_1 IS NOT NULL) ); COMMENT ON TABLE example_address.address IS 'The canonical record of a physical or postal location. Reference data shared across parties: a single Address record is referenced by all Contact Addresses at that location.'; COMMENT ON COLUMN example_address.address.address_id IS 'Globally unique surrogate identifier for this physical location record. Immutable once assigned.'; COMMENT ON COLUMN example_address.address.address_type IS 'The structural type of the address. Determines which fields are applicable.'; COMMENT ON COLUMN example_address.address.address_line_1 IS 'The primary street address line, typically including street number and street name. For PO Box addresses, the PO Box number.'; COMMENT ON COLUMN example_address.address.address_line_2 IS 'Secondary address line for suite, level, unit, or building name where applicable.'; COMMENT ON COLUMN example_address.address.suburb IS 'The suburb or locality component of the address.'; COMMENT ON COLUMN example_address.address.city IS 'The city or town.'; COMMENT ON COLUMN example_address.address.state_or_region IS 'The state, territory, or region.'; COMMENT ON COLUMN example_address.address.postcode IS 'The postal code, stored as a string to preserve leading zeros and support international formats.'; COMMENT ON COLUMN example_address.address.country IS 'The country in which the address is located, as an ISO 3166-1 alpha-2 country code.'; CREATE TRIGGER address_set_updated_at BEFORE UPDATE ON example_address.address FOR EACH ROW EXECUTE FUNCTION example_address.set_updated_at();